Skip to main content
added 737 characters in body
Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

We don't have to preserve a specific order in those linked lists. It's much faster to insert a node at the beginning of the linked list.

Make the current list head the "next" of your new node, then make your new node the list head.

Edit: More "odd" findings, actually breaking, not just slowing down your programme:

Your unload can contain an infinite loop. Just try it with the small dictionary. The culprit is your continue; shortcut, which does not add value (the case is already covered by the for loop's condition), but means index++; is never reached if any of the linked lists is empty. I would suggest a for (int index = 0; index < N; index++) instead of distributing those things, that would have prevented this particular mistake from happening.

Also, use-after-free. You free(temp_ptr) and then temp_ptr = temp_ptr->next, using the next of what you just freed. Use another variable to get the "next" first, and then free the node.

We don't have to preserve a specific order in those linked lists. It's much faster to insert a node at the beginning of the linked list.

Make the current list head the "next" of your new node, then make your new node the list head.

We don't have to preserve a specific order in those linked lists. It's much faster to insert a node at the beginning of the linked list.

Make the current list head the "next" of your new node, then make your new node the list head.

Edit: More "odd" findings, actually breaking, not just slowing down your programme:

Your unload can contain an infinite loop. Just try it with the small dictionary. The culprit is your continue; shortcut, which does not add value (the case is already covered by the for loop's condition), but means index++; is never reached if any of the linked lists is empty. I would suggest a for (int index = 0; index < N; index++) instead of distributing those things, that would have prevented this particular mistake from happening.

Also, use-after-free. You free(temp_ptr) and then temp_ptr = temp_ptr->next, using the next of what you just freed. Use another variable to get the "next" first, and then free the node.

Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

We don't have to preserve a specific order in those linked lists. It's much faster to insert a node at the beginning of the linked list.

Make the current list head the "next" of your new node, then make your new node the list head.