Delete A Node In Linked List using C++

Technology and generation are growing up together, and the younger generation users are somewhat connected with tech and the internet all the time. Not to mention, today the whole world in this time of crisis is working over the internet. But to make these technologies, software, etc a software developer must have excellent problem-solving skills. In this world where the internet is the new fuel, one needs to be pretty sharp. And by sharp, for software developers, it means knowing how to automate real-world problems using computer programs. Data structures help a lot in this journey of logic building. So today we’re going to write a simple data structure program to Delete A Node In Linked List using C++.

 

What is A Linked List?

 

A linked list is a basic list of nodes containing different elements for all algorithms in data structures. Also, it’s pretty easy to understand.

 

What’s The Approach?

 

  • The main objective of deleting a linked list node is to free the memory allocated to a node so that it gets used instead of being wasted.

 

  • To delete a given node firstly we’ll have to search for the node before it, as the previous node holds the address of the node we want to delete.

 

  • Once we find the previous node, change its address section to hold the address of our to be deleted node is currently holding in its address section.

 

  • Once the above instructions are done being executed, we’ll free the memory allocated to the deleted node.

 

Also Read: Divide Integers without Division Arithmetic Operator in C++

 

C++ Program To Delete A Node In Linked List

 

Input:

 

push(7)

 

push(8)

 

push(6)

 

push(1)

deleteNode(1)

 

Output:

 

Created Linked list is:

1 7 8 6

Linked List after Deletion :

7 8 6

 

//  C++ program to
// demonstrate deletion in
// linked list

#include <bits/stdc++.h>
using namespace std;

// A linked list node
class Node{
public:
    int data;
    Node* next;
};


// inserts a new node on the front of the
// list.
void push(Node** head_ref, int new_data)
{
    Node* new_node = new Node();
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

//  deletes the first occurrence of key in linked list

void deleteNode(Node** head_ref, int key)
{
    
    // Store head node
    Node* temp = *head_ref;
    Node* prev = NULL;
    
    // If head node itself holds
    // the key to be deleted
    if (temp != NULL && temp->data == key)
    {
        *head_ref = temp->next; // Changed head
        delete temp;		 // free old head
        return;
    }

    // Else Search for the key to be deleted
     
    else
    {
    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 memory
    delete temp;
    }
}

// This function prints contents of
// linked list starting from the
// given node
void printList(Node* node)
{
    while (node != NULL)
    {
        cout << node->data << " ";
        node = node->next;
    }
}

// Driver code
int main()
{
    
    // Start with the empty list
    Node* head = NULL;

    // Add elements in linked list
    push(&head, 6);
    push(&head, 8);
    push(&head, 7);
    push(&head, 1);

    puts("Created Linked List: ");
    printList(head);

    deleteNode(&head, 1);
    puts("\nLinked List after Deletion : ");
    
    printList(head);
    
    return 0;
}

 

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *