Runtime Error: Singlely Link List program to insert value

I wrote C code for "A Single Link List". In this code, I want to insert elements at the end of the list. It is well composed. But at runtime, the expected result does not occur. I use gcc as a compiler. Whenever I do ./a.out in the terminal, it is just hung.
Here is the code:

 #include<stdio.h> #include<stdlib.h> struct list { int node; struct list *next; }; void insert(struct list *, int); void print(struct list *); int main() { struct list *mylist; insert(mylist, 10); insert(mylist, 20); insert(mylist, 30); insert(mylist, 40); insert(mylist, 50); insert(mylist, 60); print(mylist); return 0; } void print(struct list *head) { if(head==NULL) return; else { while(head->next!=NULL) { printf("%d\t",head->node); head=head->next; } } } void insert(struct list *head, int value) { struct list *new_node; new_node = (struct list *)malloc(sizeof(struct list)); //node Creation new_node->node=value; new_node->next=NULL; //Adding Node to list if(head==NULL) { head=new_node; } else { while(head->next!=NULL); { head=head->next; } head->next=new_node; } } 

Here insert() is a function that inserts elements into mylist linklist and print() is a function that prints all the values ​​in a link list. Please help. I can’t catch what mistake I made.

+4
source share
5 answers

I would suggest another change, i.e. function prototypes should look like

 void insert(struct list **, int); void print(struct list **); 

and the body must be modified accordingly. Since you made new memory allocations in the insert, and therefore you should not make a pass by value, but rather should pass to the address, then only it will work as intended.

In addition, in the print function, the loop end should be while (* head! = NULL) instead of while ((* head) β†’ next! = NULL), otherwise it will skip the last node.

You should also save the first node to the tmp pointer after you first call the insert function and that the tmp pointer must be passed to the print function at the end. In the code, you pass a pointer to the last node, which is incorrect. So it should be.

 int main() { struct list *mylist=NULL, *tmp = NULL; insert(&mylist, 10); tmp = mylist; /* here */ insert(&mylist, 20); insert(&mylist, 30); insert(&mylist, 40); insert(&mylist, 50); insert(&mylist, 60); /* At this point mylist is pointing to last node, so pass tmp which stores the first node */ print(&tmp); return 0; } 
+5
source

The problem is the following line,

  while(head->next!=NULL); 

Must be

  while(head->next!=NULL) 

Delete semicolon.

+4
source

There are several errors:

1) your code authors struct list *head; . Do you know that in C and C ++ such a variable is not initialized? You cannot count on it being NULL if it is not globally; instead, local variables must be initialized before use.

2) your insert function gets head by value, so when it changes it (in case the list is empty), it changes only the local copy, and not the head main variable. You must either pass head as a struct list ** , or you must return the new head value in main. In C ++, an alternative is to pass it as list *& (pointer reference).

3) In your while there is an additional semicolon in front of the body, so that the empty cycle and part of the body will always be executed (no matter what the condition is) and exactly once, because it is just a nested {...} block .

+2
source

Here is a nice endless loop. If you use a debugger, you could find it yourself ^^.

 while(head->next!=NULL); 

After you also have to initialize your list in your main function.

 struct list *mylist = NULL; 

You also need to change your insert argument as a double pointer (changing the value of the main header should not happen if you pass it a pointer only after it just copies the address value)

 void print(struct list *head) { while(head!=NULL) { printf("%d\t",head->node); head=head->next; } } void insert(struct list **head, int value) { struct list *new_node; new_node = (struct list *)malloc(sizeof(struct list)); //node Creation new_node->node=value; new_node->next=NULL; //Get the end of the list while((*head)->next!=NULL) { (*head)=(*head)->next; } // Add the node at the end of the list (*head)->next=new_node; } int main() { struct list *mylist = NULL; insert(&mylist, 10); insert(&mylist, 20); insert(&mylist, 30); insert(&mylist, 40); insert(&mylist, 50); insert(&mylist, 60); print(mylist); return 0; } 
+1
source

While it does not immediately solve your problem, also note that memory that is dynamically allocated is not freed in your application: your program is leaking.

It would be highly desirable to add another destroy function:

 void destroy(list * head) { while(head != NULL) { struct list * tmp = head->next; free(head); head = tmp; } } 

Here is a complete working example that does not flow: http://ideone.com/y2Fl7i

+1
source

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


All Articles