Home > Enterprise >  Append function of linked list doesn't work - C
Append function of linked list doesn't work - C

Time:01-07

Basically, I am trying to insert a node into the linked list via the append function I'm writing. Although in the function, it appears that my node header has the same value of the address of my first node, after the function closes it turns back to null.

This is my append function:




void append(node* header, node newNode) {
    
    //check if header is NULL
    //if not add first value

    if (header == NULL) {
        header = &newNode;
        printf("List val: %p\n", header);
        printf("node address: %p\n\n", &newNode);
    }
    
    else {
        
        //find last node
        node* tmp = header;
        while (tmp -> next != NULL) {
            tmp = tmp -> next;
        }
        
        // inser lastnode.next to be address of new node
        tmp -> next = &newNode;
    }
}

And then in main, I created a header and node and call this function, and ask to print value of list and address of node a:

node* list = NULL;
node a = {1, NULL};
append(list, a);
printf("%p\n", list);
printf("%p\n", &a);

The output is:

List val: 000000b6779ff900 node address: 000000b6779ff900

0000000000000000 000000b6779ff970

So for some reason, the value of the node* list after the function is still NULL. Still new to this pointer thing, so I am currently having trouble understanding where I am wrong.

Edit: Community bot tells me to provide enough code, so I bet it's something about the node type?

typedef struct node {
    int value;
    struct node* next;
} node;

CodePudding user response:

This is a normal behavior. Actually you are passing a pointer header to the function. When you assign it to a value, you only change the local reference..

You may rather want to pass a pointer to the pointer, expressed as struct node **header and then set it with *header = newNode.

CodePudding user response:

Problems in your code:

  1. Your list variable isn't getting updated. You are passing the value of the list (call by value) to the function. If you want to reflect the changes made to the value, you need to update it in the main function. So, drop the return type of append function from void to node* or make list global(the former is preferred).

  2. You are passing the value of the node as an argument to append and accessing its address in the function. This address isn't the address provided to the node at main, it is the address provided to it when append is called. So, pass &a instead of a.

Here is a code I wrote to help you out:

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node* next;
} node;

node* append(node* header, node* new_node){
    if(header == NULL){
        header = new_node;
        return header;
    }

    node* temp = header;
    while(temp -> next != NULL){
        temp = temp -> next;
    }
    temp -> next = new_node;
    return header;
}

int main(void){
    node* list = NULL; // header
    
    // first node
    node* new_node = malloc(sizeof(node));
    new_node -> data = 1;
    new_node -> next = NULL;

    // second node
    node* new_node2 = malloc(sizeof(node));
    new_node2 -> data = 2;
    new_node2 -> next = NULL;

    // third node
    node* new_node3 = malloc(sizeof(node));
    new_node3 -> data = 3;
    new_node3 -> next = NULL;

    // fourth node
    node a = {4, NULL}; // your style :)

    // the above can be squeezed to one 'create' function

    list = append(list, new_node);  // the if condition
    list = append(list, new_node2); // the else condition
    list = append(list, new_node3); // the else condition
    list = append(list, &a);

    printf("%d\n", list -> data);
    printf("new_node: %p\n", new_node);
    printf("%d\n", list -> next -> data);
    printf("new_node2: %p\n", new_node2);
    printf("%d\n", list -> next -> next ->  data);
    printf("new_node3: %p\n", new_node3);
    printf("%d\n", list -> next -> next -> next -> data);
    printf("a: %p\n", &a);


    return 0;
}
  • Related