Skip to main content

Linked List | Set 3 (Deleting a node)

 

Linked List | Set 3 (Deleting a node)

We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.
Let us formulate the problem statement to understand the deletion process. Given a ‘key’, delete the first occurrence of this key in the linked list
To delete a node from the linked list, we need to do the following steps. 
1) Find the previous node of the node to be deleted. 
2) Change the next of the previous node. 
3) Free memory for the node to be deleted.

linkedlist_deletion

Since every node of the linked list is dynamically allocated using malloc() in C, we need to call free() for freeing memory allocated for the node to be deleted.

// A complete working C program 

// to demonstrate deletion in 

// singly linked list

#include <stdio.h>

#include <stdlib.h>


// A linked list node

struct Node

{

int data;

struct Node *next;

};


/* Given a reference (pointer to pointer) to the head of a list

and an int, inserts a new node on the front of the list. */

void push(struct Node** head_ref, int new_data)

{

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

}


/* Given a reference (pointer to pointer) to the head of a list

and a key, deletes the first occurrence of key in linked list */

void deleteNode(struct Node **head_ref, int key)

{

// Store head node

struct Node* temp = *head_ref, *prev;


// If head node itself holds the key to be deleted

if (temp != NULL && temp->data == key)

{

*head_ref = temp->next; // Changed head

free(temp); // free old head

return;

}


// Search for the key to be deleted, keep track of the

// previous node as we need to change 'prev->next'

while (temp != NULL && temp->data != key)

{

prev = temp;

temp = temp->next;

}


// If key was not present in linked list

if (temp == NULL) return;


// Unlink the node from linked list

prev->next = temp->next;


free(temp); // Free memory

}


// This function prints contents of linked list starting from 

// the given node

void printList(struct Node *node)

{

while (node != NULL)

{

printf(" %d ", node->data);

node = node->next;

}

}


// Driver code

int main()

{

/* Start with the empty list */

struct Node* head = NULL;


push(&head, 7);

push(&head, 1);

push(&head, 3);

push(&head, 2);


puts("Created Linked List: ");

printList(head);

deleteNode(&head, 1);

puts("\nLinked List after Deletion of 1: ");

printList(head);

return 0;

}

Output: 

Created Linked List:
 2  3  1  7
Linked List after Deletion of 1:
 2  3  7






Comments

Popular posts from this blog

Doubly Linked List - Data Structure

                           Doubly Linked List A doubly linked list is a  linked list data structure that includes a link back to the previous node in each node in the structure . This is contrasted with a singly linked list where each node only has a link to the next node in the list. List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list. Link  − Each link of a linked list can store a data called an element. Next  − Each link of a linked list contains a link to the next link called Next. Prev  − Each link of a linked list contains a link to the previous link called Prev. LinkedList  − A Linked List contains the connection link to the first link called First and to the last link called Last. Doubly Linked List Representation As per the above illustration, following are the important points to be considered. Dou

Information Gathering Tool (System Analysis and Design)

  Information gathering tools... Introduction Information Gathering is a very key part of the feasibility analysis process. Information gathering is both an art and a science. It is a science because it requires a proper methodology and tools in order to be effective. It is an art too, because it requires a sort of mental dexterity to achieve the best results. In this article we will explore the various tools available for it, and which tool would be best used depending on the situation. Information Gathering Tools There is no standard procedures defined when it comes to the gathering of information. However, an important rule that must be followed is the following: information must be acquired accurately and methodically, under the right conditions and with minimum interruption to the individual from whom the information is sought. 1. Review of Procedural Forms These are a very good starting point for gathering information. Procedural manuals can give a good picture of the system to b

CPP Programming

  1. Program for declaring function inside the class, declaring the objects and calling functions //Simple Program for declaring functions inside the class #include<iostream.h> #include<conio.h> class TestSimple { int x,y,s; //Three member variables public: void getdata() // function to store values on member variables { cout<<"\nEnter two numbers "; cin>>x>>y; } void sum() { s=x+y; // adding the values ov x and y } void putdata() { cout<<"\nSum of "<<x<<" and "<<y<<" is "<<s; // displaying the values of variables } }; void main() { TestSimple t; //Object declaration t.getdata(); // Function Call t.sum(); t.putdata(); getdata(); } 2. Program for declaring function outside the class //Simple Program for functions outside the class #inclu