Skip to content
Advertisement

Deleting Linked List Elements

I’m curious what I have done wrong, since my void *DeleteDoneNodes(node * n) doesn’t do anything, no error but no output neighter, so could anyone help me finding the root of my problem thank you.

Scenario: cmd line ./s m n m number of nodes in list, n number of workingThreads should delete each nodes with node->value == 1, but it doesn’t do anything…

code:

JavaScript

PS: It compiles / works error /warningless – tested with valgrind also.

Advertisement

Answer

Here’s an alternative version

JavaScript

The main changes are:

  • Handling removal of an item from the middle of the list. The previous item needs to change its next pointer. Without this, it’ll keep pointing to freed memory.
  • Logic for updating root. This only needs to happen if we free root
  • Communicating possible change in root. If we remove the root (aka head) item from the list, this needs to be communicated to the caller. I’ve done this by returning the (possibly updated) value of root instead of NULL
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement