C adding node to head of linked list

前端 未结 6 1098
小蘑菇
小蘑菇 2021-01-22 05:48

I have created a linked list struct in c

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

a method to add a node at the start of the list :

6条回答
  •  面向向阳花
    2021-01-22 06:39

    All is good, but in the void addFirst(struct node *list, int value) function the list is passed by value. Which means that a pointer is being copied and assignment of a new address to that pointer inside addFirst function is not visible to the caller of addFirst. To solve it, you have to either pass a pointer by pointer (struct node **) or make it a return value and require a caller to use it as a new "head".

    And don't forget ; after a struct declaration.

提交回复
热议问题