C adding node to head of linked list

前端 未结 6 1149
小蘑菇
小蘑菇 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:28

    I have learned @Vlad Lazarenko answer,and I made the code like this ,is it right?

    addFirst((struct node**)head,123);
    
    void addFirst(struct node **list,int value)
    {
        struct node *new_node=malloc(sizeof(struct node));
        new_node->value=value;
        new_node->next=*list;
        list=&new_node;
    }
    

提交回复
热议问题