Home > front end >  Why doesn't my compiler show my input operation about the linked list and with returned value 3
Why doesn't my compiler show my input operation about the linked list and with returned value 3

Time:09-29

I create the code to achieve a doubly linked list structure.However, I don't know why it doesn't show my expected output, even not print anything. Here is the output of the compiler: Process exited after 0.2849 seconds with return value 3221225477 But it shows 0 error and 0 warning. Do you know anyting about the issue? Thank you in advance!

here are my codes

#include <stdio.h>
#include <stdlib.h>
//define a node
typedef struct ListNode
{
    int val;
    struct ListNode* left;
    struct ListNode* right;
} ListNode;
//define a function to insert node on the right
void insert_right(ListNode* p)
{
    ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
    new_node->val = 0;
    new_node->right = p->right;
    p->right->left = new_node;
    p->right = new_node;
    new_node->left = p;
}
//define a function to insert node on the left
void insert_left(ListNode* p)
{
    ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
    new_node->val = 0;
    new_node->left = p->left;
    p->left->right = new_node;
    p->left = new_node;
    new_node->right = p;
}
//move the p to the right by X
void move_right(int X, ListNode* p)
{
    int i; 
    for(i = 0; i < X; i  )
    {
        if (p->right != NULL)
        {
            p = p->right;
        }
        else
        {
            printf("Right");
            break;
        }
    }
}
//move the p to the left by X
void move_left(int X, ListNode* p)
{
    int i;
    for(i = 0; i < X; i  )
    {
        if (p->left != NULL)
        {
            p = p->left;
        }
        else
        {
            printf("Left");
            break;
        }
    }
}

//delete the node on the right of p
void remove_right(ListNode* p)
{
    if(p->right != NULL)
    {
        p->right->right->left = p;
        p->right = p->right->right;
    }
    else printf("Right");
}
//delete the node on the left of p
void remove_left(ListNode* p)
{
    if(p->left != NULL)
    {
        p->left->left->right = p;
        p->left = p->left->left;
    }
    else printf("Left");
}
//change the val of the node(p points to) to X
void set(int X, ListNode* p)
{
    p->val = X;
}
//show the value of the node which p points to
void show(ListNode* p)
{
    printf("%d", p->val);
}
int main()
{
    int i, j;
    //define the head_node and let p points to it
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    head->val = 0;
    head->left = NULL;
    head->right = NULL;
    ListNode* p = head;
    for(i = 0; i < 6; i  )
    {
    insert_left(p); 
    }
    for(i = 0; i < 6; i  )
    {
    insert_right(p); 
    }
    move_left(2, p);
    move_right(30, p);
    show(p);
    printf("ha"); 
}

CodePudding user response:

There is a problem of the first call of insert_left(p); as head->left = NULL; and, in insert_left() a NULL pointer is used in p->left->right = new_node;.

I suggest to use debugger and go line by line and check weather the variables follows your simple test data. It is very difficult to investigate memory overwriting in C or C . It is important to always check that a pointer is NULL or not. There is one exception: if you checked before.

  • Related