We now know how to create data type for a node of a linked list and how to traverse linked list and to add nodes at various positions. In this post I am going to tell how to delete nodes at any position.
The code for the same is:Deleting root node of linked list
Deleting a root node from a linked list is really a simple deal if you have read my previous posts. All you need to to do is:
- Change the current node to the head node.
- Store the address of head node in a temporary variable of pointer type.
- Traverse to the immediate next node using current->next.
- Mark that node as head or root node.
- Free the memory space occupied by previous head node using free() function.
current=head;
struct linkedlist *address;
address=head;
head=current->next;
free(address);
- The First line of code assigns head node as current node.
- The second line of code creates a temporary pointer variable address of type linkedlist.
- Next head node address is assigned to the address variable which is needed as we have to later free up the space occupied by current head node
- Now we have changed the head node by immediate next node using current->next.
- Finally we free up the space occupied by previous head node.
Deleting any node from linked list
Deleting any node other than root node requires a couple of extra steps. The whole process to delete a node is explained in following steps:
- Change the current node to head node.
- Traverse to the node just before the node required to be deleted.
- Save the address of that node in some temporary variable (say 'address') of pointer of linkedlist structure type.
- Move to the next node which is the one to be deleted that means now the current node is the node to e deleted.
- Now change the pointer of next node with name 'address' to the pointer of the node pointed by current node. Now the address named node point to the node which was previously pointed out the the node to be deleted.
- Finally free up the space occupied by deleted node.
struct linkedlist *address;
int position=1;
current=head;
while(current!=NULL && position<x)
{
position++;
address=current;
current=current->next;
}
address->next=current->next;
free(current);
Explanation:
- The First line of code creates a temporary pointer variable address of type linkedlist.
- The second line of code initializes a position variable position to 1 for traversing.
- The second line of code assigns head node as current node.
- Next we loop till list is empty (Current !=NULL) or we reach position just before x.
- Inside the loop we change the address variable so that in the end address variable contains the address of node just before node to be deleted. We also set the current node as the node to be deleted using current=current->next expression.
- Finally we change the "next" pointer of node name address with the "next" pointer of current (node to be deleted) node.
- Then free up the space occupied by the deleted node.
Mail me if the link has expired or not working
No comments:
Post a Comment