1

I'm implementing a singly linked list using C.

struct Node 
{
    int nodeValue;
    struct Node* pNext;
};

struct Node* head;

void createList()
{
    head = (struct Node*)malloc(sizeof(struct Node));
}

void insertNodeAtBeginning(int value)
{
    struct Node* newNode;
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->nodeValue = value;
    struct Node* auxNode;

    if(head->pNext == NULL)
    {
        head->pNext = newNode;        
    }

    else
    {
        auxNode = head->pNext;
        head->pNext = newNode;
        newNode->pNext = auxNode;    //breakpoint set here
    }
}

I've set a breakpoint on line marked by the comment. The value of auxNode is non-NULL:

(gdb) p auxNode
$4 = (struct Node *) 0x5555555551db <createList+18>

However the value of newNode->pNext to which the auxNode is assigned is NULL:

(gdb) p newNode->pNext
$5 = (struct Node *) 0x0

Could anyone clarify this behavior? Thanks.

4
  • 3
    The line hasn't executed if you are stopped on that line. step to run that line and then examine the values again. Commented Aug 13, 2021 at 20:44
  • (a) Edit the question to provide a minimal reproducible example. (b) createList allocates space for one node and sets head to point to it but does not fill in that space, nor does any of the other code shown. insertNodeAtBeginning may be using the contents of that space uninitialized. (c) When the code breaks at the line newNode->pNext = auxNode, the line has not been executed yet. Commented Aug 13, 2021 at 20:47
  • 2
    You should not cast the return value of malloc Commented Aug 13, 2021 at 20:47
  • 3
    head->pNext is not initialized in createList Commented Aug 13, 2021 at 21:03

2 Answers 2

1

For starters the function createList does not make a sense.

void createList()
{
    head = (struct Node*)malloc(sizeof(struct Node));
}

You already created an empty list

struct Node* head;

Within the function data members nodeValue and pNext were not initialized,

The function insertNodeAtBeginning also does not make a sense because at least it does not insert a node at beginning due to this code snippet

if(head->pNext == NULL)
{
    head->pNext = newNode;        
}

Moreover it invokes undefined behavior because the data member pNext of the node pointed to by the pointer head was not initialized. And again you forgot to initialize the data member pNext of the new node when head->pNext is equal to NULL.

Remove the function createList and define the function insertNodeAtBeginning the following way

int insertNodeAtBeginning(int value)
{
    struct Node* newNode = malloc( sizeof( struct Node ) );
    int success = newNode != NULL;

    if ( success )
    {
        newNode->nodeValue = value;
        newNode->pNext = head;
        head = newNode;        
    }

    return success;
}
Sign up to request clarification or add additional context in comments.

Comments

0

It appears that you want to maintain your head node, but insert between the head node and the node after the head node, correct? If that is the case, then aside from a misleading function name (perhaps insertNodeAfterHead would be more appropriate), you should initialize your head struct members after you allocate with malloc.

Should be...

void createList()
{
    head = (struct Node*)malloc(sizeof(struct Node));
    head->nodeValue = 0
    head->pNext = NULL;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.