Gabriel Taets
Gabriel Taets

Reputation: 65

Deleting the only node of a linked list

I got a linked list with only one node. How do you remove that only node? You can't "skip" it by pointing the *next struct of the previous struct to the next structure because there are no next structures, nor previous structures. I've tried using free() but it didn't work either. So the question is: How do you remove the only node from a linked list? I've googled it, but no helpful links were found, unfortunately. Thank you for the help.

Upvotes: 1

Views: 1500

Answers (3)

RicHincapie
RicHincapie

Reputation: 4023

If you have the lasting node, then the head is that node. So, after you free(head) , make sure to declare head = NULL . This a good practice as long as I understand. And, let's say you have a function to delete nodes. At the beginning, don't forget to code

if (*head == NULL)
        return ("your error code");

This way, the programm won't try to free something that hasn't been allocated and get you a segmentation fault.

Upvotes: 0

supercat
supercat

Reputation: 81337

Because there are multiple ways in which linked lists are used, there's no single pattern which is best for every application. Nonetheless, a pattern which is very frequently helpful is to have one or two "special" and/or "pre-allocated" nodes which are used for the start and/or end of a linked list (sometimes it's helpful for doubly-linked lists to start and end with the same node; sometimes it's useful to have distinct "start" and "end" nodes). In a singly-linked list with one extra "start" node, the main reference to the list will typically identify the start node which will exist as long as the list does; that reference will never need to change. The fact that every "real" item in the list--even the very first one--will always have a node preceding it avoids the need to worry about special code to delete the first item or insert an item before it, since the first real item of the list may be treated just like any other.

Incidentally, when working with singly-linked lists, it is often a good idea to have each node contain a "deleted" flag. Generally in the process of finding a node, one will find the previous node as well; since deleting a node requires knowing which node preceded it, keeping track of the node which preceded a the most-recently-found node will often allow one to delete it without having to search for its predecessor. If, however, one finds multiple nodes and then performs actions upon them, it's possible that by the time one goes to perform an action on a node (e.g. deleting it), the node which used to precede it might no longer do so. Having a "deleted" flag in each node may make it easier to detect such conditions and handle them smoothly (if wants to delete a node and can't immediately identify the previous node, it may be best to simply set the "deleted" flag; if the invalidated node is later encountered by code which knows which node precedes it, that code can take care of actually removing the invalidated node from the list.

Upvotes: 1

ciphermagi
ciphermagi

Reputation: 748

Assuming you have

struct foo * head;

somewhere which actually does point at your node, you should be able to

free(head);

If you can't, then you should post your code so that we can look at what you're actually doing wrong.

Upvotes: 1

Related Questions