To delete a node (this) in a linked list, first you need to find the address of the parent node (parent).
If you don't already have a reference to the node, there is no way to avoid traversing the list to find it.
Given a list and a node to delete, use the following algorithm: // Are we deleting the head node? if (node == list.head) { // Yes -- assign its next node as the new head list.head = node.next } else // The node is not the head node { // Point to the head node prev = list.head // Traverse the list to locate the node that comes immediately before the one we want to delete while (prev.next != node) { prev = prev.next; } end while // Assign the node's next node to the previous node's next node prev.next = node.next; } end if // Before deleting the node, reset its next node node.next = null; // Now delete the node. delete node;
A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.
examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node
If by 'head node' you simply mean the first node, then yes; but if 'head node' means the special element which is not supposed to ever be deleted (aka sentinel node), then no.
If you don't already have a reference to the node, there is no way to avoid traversing the list to find it.
Yes, with limitations... If you have the address of a node in the linklist, you can insert a node after that node. If you need to insert the node before that node, you need to traverse the list, unless the linklist is a doubly-linkedlist
In a circular linked list every node is connected to another node. In a non-circular linked list. There are definitely starting and ending nodes are lacking an incoming and outgoing link, respectively.
Given a list and a node to delete, use the following algorithm: // Are we deleting the head node? if (node == list.head) { // Yes -- assign its next node as the new head list.head = node.next } else // The node is not the head node { // Point to the head node prev = list.head // Traverse the list to locate the node that comes immediately before the one we want to delete while (prev.next != node) { prev = prev.next; } end while // Assign the node's next node to the previous node's next node prev.next = node.next; } end if // Before deleting the node, reset its next node node.next = null; // Now delete the node. delete node;
Three steps for deleting a node from a linked list: 1) set currentNode->prev->next to currentNode->next (i.e. the previous node's next pointer should be the current node's next pointer). 2) set currentNode->next->prev to currentNode->prev (i.e. the next node's previous pointer should be the current node's previous pointer). 3) Free the memory used by currentNode (using delete, for example).
A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.
To delete an node in a linked list...If the head node is empty, stop, and return failed.Interate thorugh each member of the list.Remember at each iteration, the address of the privious node.If the current node is the head node or not the desired node, continue to the next iteration.Move the current node's pointer value to the previous node's pointer.Delete the current node, and return success.Return failed..
examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node
If by 'head node' you simply mean the first node, then yes; but if 'head node' means the special element which is not supposed to ever be deleted (aka sentinel node), then no.
Lack of skill?
For a singly-linked list, only one pointer must be changed. If the node about to be deleted (let's call it node for the sake of argument) is the head of the list, then the head node pointer must be changed to node->next. Otherwise, the node that comes before the deleted node must change its next pointer to node->next. Note that given a singly-linked node has no knowledge of its previous node, we must traverse the list from the head in order to locate that particular node, unless the node is the head of the list: void remove (List* list, Node* node) { if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;} else {Node* prev = list->head;while (prev->next != node) prev = prev->next; // locate the node's previous nodeprev->next = node->next;}} Note that the remove function only removes the node from the list, it does not delete it. This allows us to restore the node to its original position, because the node itself was never modified (and thus still refers to its next node in the list). So long as we restore all removed nodes in the reverse order they were removed, we can easily restore the list. In order to delete a node completely, we simply remove it and then free it:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node);free (node);} For a doubly-linked list, either two or four pointers must be changed. If the node about to be deleted is the head node, then the head node pointer must be changed to n->next and n->next->prev must be changed to NULL, otherwise, n->prev->next becomes n->next. In addition, if the node about to be deleted is the tail node, then the tail node pointer must be changed to n->prev and n->prev->next must be changed to NULL, otherwise, n->next->prev becomes n->prev. Deletion from a doubly-linked list is generally quicker than deletion from a singly linked list because a node in a doubly-linked list knows both its previous node and its next node, so there's no need to traverse the list to locate the previous node to the one being deleted. void remove (List* list, Node* node) {if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;node->next->prev = NULL;} else {node->prev->next = node->next; }if (list->tail == node) {list->tail = node->prev;node->prev->next = NULL;} else {node->next->prev = node->prev; }} Again, to physically delete the node we simply remove and then free the node:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node); free (node); }
Use a recursive function. void delete_node (node* n) { /* ensure the given pointer is valid */ if (n==NULL) return; /* recursively delete the left child */ delete_node (n->left); n->left = NULL; /* recursively delete the right child */ delete_node (n->right); n->right = NULL; /* delete the data (assuming the data is a pointer)*/ free (n->data); n->data = NULL; /* finally, delete the node itself */ free (n); n=NULL; }