Insert links in c: insert invalid values

I can’t understand what is wrong with my code, when I run the code, I have garbage output! It seems like I'm inserting some weird values. Thank you for your help:

My code is below:

#include <stdio.h> #include <stdlib.h> #include <sys/time.h> struct node { int number; struct node *next; struct node *prev; }; struct node *head = NULL; int number_of_cycles = 0; int arr[] = { 82, 13, 22, 61, 12, 52, 41, 75, 98, 20, 6, 9, 7, 1, 5, 4, 2, 8, 3 }; /* insert a node directly at the right place in the linked list */ void insert_node(int value); int main(void) { struct node *current = NULL; struct node *next = NULL; int i = 0; struct timeval tbegin,tend; double texec=0.; // Start timer gettimeofday(&tbegin,NULL); /* insert some numbers into the linked list */ for(i = 0; i < 19; i++) //insert_node(rand()); insert_node(arr[i]); /* print the list */ printf(" before after\n"), i = 0; while(head->next != NULL) { printf("%4d\t%4d\n", arr[i++], head->number); head = head->next; } /* free the list */ for(current = head; current != NULL; current = next) next = current->next, free(current); // End timer gettimeofday(&tend,NULL); // Compute execution time texec=((double)(1000*(tend.tv_sec-tbegin.tv_sec)+((tend.tv_usec-tbegin.tv_usec)/1000)))/1000.; printf("Elapsed time = %f\n", texec); printf("Number Of Cycles = %d\n", number_of_cycles); return 0; } void insert_node(int value) { struct node *new_node = NULL; struct node *cur_node = NULL; struct node *last_node = NULL; int found; new_node = (struct node *)malloc(sizeof(struct node *)); if(new_node == NULL) { printf("memory problem\n"); } new_node->number = value; /* If the first element */ if (head == NULL) { new_node->next = NULL; new_node->prev = NULL; head = new_node; } else if (new_node->number < head->number) { new_node->next = head; head->prev = new_node; head = new_node; new_node->prev = NULL; } else { cur_node = head; found = 0; while (( cur_node != NULL ) && ( found == 0 )) { if( new_node->number < cur_node->number ) { found = 1; } else { last_node = cur_node; cur_node = cur_node->next; } number_of_cycles++; } if( found == 1 ) { new_node->prev = cur_node->prev; new_node->next = cur_node; cur_node->prev->next = new_node; cur_node->prev = new_node; } else { last_node->next = new_node; new_node->next = NULL; new_node->prev = last_node; } number_of_cycles++; } } 

The output of this code is:

 before after 82 1916799424 13 1916799408 22 1916799392 61 1916799376 12 1916799360 52 1916799344 41 1916799328 75 1916799312 98 1916799296 20 1916799280 6 1916799264 9 1916799248 7 1916799232 1 1916799216 5 1916799200 4 1916799184 2 1916799168 8 1916799152 Elapsed time = 0.000000 Number Of Cycles = 0 
+4
source share
1 answer

Your problem is insert_node:

new_node = (struct node *) malloc (sizeof (struct node *));

What would you like:

new_node = (struct node *) malloc (sizeof (struct node));

The reason is because you want to allocate enough heap space for the struct node, not the struct node pointer.

+7
source

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


All Articles