C adding node to the beginning of the linked list

I created a linked list structure in c

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

method to add node to the top of the list:

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

I create a list (malloc and all), then call this method, it adds a new node inside the method, but when I return to my main one, my old list remains unchanged. Using a DDD debugger to test everything. How is this possible? I can’t change the method signature, so I need to do this.

+2
source share
6 answers

If you really need to do this, you need to re-map the pointer. Something like that:

 struct node *my_list = null; addFirst((struct node *)&my_list, 123); void addFirst(struct node *list, int value){ struct node **real_list = (struct node **)list; struct node *new_node = (struct node*) malloc (sizeof (struct node)); new_node->value = value; new_node->next = *real_list; *real_list = new_node; } 
+5
source

The node pointer cannot be changed into a function in this way. in a function, you can change the contents of the pointer, not the address of the pointer. To do this, you need to pass a pointer to a struct node **list pointer

here after doing this:

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

or you can do it this way

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

and in your cod you can get a head after calling this function

 head = addfirst(head,45); 
+5
source

In C, if you want the function to be able to change the value obtained in its arguments, you need to pass the address of that value. So, to change the value of the list pointer, you need to pass the address of the list pointer. Your addFirst () function should look like this:

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

And when you call this function, you call it like this:

 addFirst(&list, value); 

Now, if you want to keep the signature of the function, it is possible to change the way you consider the head of the node. If you state that your node head is only designed to hold a pointer to the first value, but does not contain a value in itself, you can do something like this:

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

Now you have only all your functions that work in the list so that they change, so they work the same way, assuming that the "head" points only to the real first node of the list, but the list itself is not a member. A "real" head, for all practical purposes, head-> next.

+3
source

I found out @Vlad Lazarenko's answer, and I made a code like this, 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; } 
+2
source
 void addFirst(struct node **list, int value){ struct node *new_node = (struct node*) malloc (sizeof (struct node)); new_node->value = value; new_node->next = *list; *list = new_node; } 

This is the correct code. the problem is that you cannot change the struct node *list transmitter, since this is a stack variable. If you change it to struct node **list , you will pass a pointer to the first node of the list. now you can change this to point to the new first node of the list.

+1
source

Everything is fine, but in void addFirst(struct node *list, int value) , the list function is passed by value. This means that the pointer is copied, and the assignment of a new address to this pointer inside the addFirst function addFirst not displayed to the addFirst . To solve this problem, you need to either pass a pointer to a pointer ( struct node ** ), or make it a return value and require that the caller use it as a new "head".

And do not forget ; after the declaration of the structure.

+1
source

Source: https://habr.com/ru/post/1484397/


All Articles