Delete a node before a given node in doubly linked list

Delete a node in a Doubly Linked List

Pre-requisite: Doubly Link List Set 1| Introduction and Insertion

Write a function to delete a given node in a doubly-linked list.
Original Doubly Linked List

Deletion in doubly linked list after the specified node

In order to delete the node after the specified data, we need to perform the following steps.

  • Copy the head pointer into a temporary pointer temp.
  • Traverse the list until we find the desired data value.
  • Check if this is the last node of the list. If it is so then we can't perform deletion.
  • Check if the node which is to be deleted, is the last node of the list, if it so then we have to make the next pointer of this node point to null so that it can be the new last node of the list.
  • Otherwise, make the pointer ptr point to the node which is to be deleted. Make the next of temp point to the next of ptr. Make the previous of next node of ptr point to temp. free the ptr.

In the given doubly linked list, delete a node

We can delete head node, middle node or last node.

Example

Algorithm

Time complexity : O[1]

Step 1 : create a function which takes a linked list and node that had to be deleted as arguments and delete the node.
Step 2 : If you want to delete a head node.
a] Change the head pointer to next of current node [head here].
b] Change the previous pointer of next node to current node previous.
Step 3 : If you want to delete middle node.
a] Change the previous pointer of next node to current node previous
b] Change the previous node next pointer to next of current node.
c] Delete the node. [current node]
d] If previous or next is NULL you need not delete them. [for deleting last node]
Step 4 : On the given linked list, call the function and give which node you want to delete.

Algorithm working Example

Doubly Linked In C++

As in the singly linked list, the doubly linked list also has a head and a tail. The previous pointer of the head is set to NULL as this is the first node. The next pointer of the tail node is set to NULL as this is the last node.

A basic layout of the doubly linked list is shown in the below diagram.

In the above figure, we see that each node has two pointers, one pointing to the previous node and the other pointing to the next node. Only the first node [head] has its previous node set to null and the last node [tail] has its next pointer set to null.

As the doubly linked list contains two pointers i.e. previous and next, we can traverse it into the directions forward and backward. This is the main advantage of doubly linked list over the singly linked list.

Declaration

In C-style declaration, a node of the doubly linked list is represented as follows:

struct node { struct node *prev; int data; struct node *next; };

Apart from the above declaration, we can also represent a node in the doubly linked list as a class in C++. A doubly linked list is represented as a class when we use STL in C++. We can implement a doubly linked list using a class in Java as well.

Basic Operations

Following are some of the operations that we can perform on a doubly linked list.

Insertion

Insertion operation of the doubly linked list inserts a new node in the linked list. Depending on the position where the new node is to be inserted, we can have the following insert operations.

  • Insertion at front – Inserts a new node as the first node.
  • Insertion at the end – Inserts a new node at the end as the last node.
  • Insertion before a node – Given a node, inserts a new node before this node.
  • Insertion after a node – Given a node, inserts a new node after this node.

Deletion

Deletion operation deletes a node from a given position in the doubly linked list.

  • Deletion of the first node – Deletes the first node in the list
  • Deletion of the last node – Deletes the last node in the list.
  • Deletion of a node given the data – Given the data, the operation matches the data with the node data in the linked list and deletes that node.

Traversal

Traversal is a technique of visiting each node in the linked list. In a doubly linked list, we have two types of traversals as we have two pointers with different directions in the doubly linked list.

  • Forward traversal – Traversal is done using the next pointer which is in the forward direction.
  • Backward traversal – Traversal is done using the previous pointer which is the backward direction.

Reverse

This operation reverses the nodes in the doubly linked list so that the first node becomes the last node while the last node becomes the first node.

Search

Search operation in the doubly linked list is used to search for a particular node in the linked list. For this purpose, we need to traverse the list until a matching data is found.

Insertion

Insert a node at the front

Insertion of a new node at the front of the list is shown above. As seen, the previous new node N is set to null. Head points to the new node. The next pointer of N now points to N1 and previous of N1 that was earlier pointing to Null now points to N.

Insert node at the end

Inserting node at the end of the doubly linked list is achieved by pointing the next pointer of new node N to null. The previous pointer of N is pointed to N5. The ‘Next’ pointer of N5 is pointed to N.

Insert node before/after given node

As shown in the above diagram, when we have to add a node before or after a particular node, we change the previous and next pointers of the before and after nodes so as to appropriately point to the new node. Also, the new node pointers are appropriately pointed to the existing nodes.

The following C++ program demonstrates all the above methods to insert nodes in the doubly linked list.

#include using namespace std; // A doubly linked list node struct Node { int data; struct Node* next; struct Node* prev; }; //inserts node at the front of the list void insert_front[struct Node** head, int new_data] { //allocate memory for New node struct Node* newNode = new Node; //assign data to new node newNode->data = new_data; //new node is head and previous is null, since we are adding at the front newNode->next = [*head]; newNode->prev = NULL; //previous of head is new node if [[*head] != NULL] [*head]->prev = newNode; //head points to new node [*head] = newNode; } /* Given a node as prev_node, insert a new node after the given node */void insert_After[struct Node* prev_node, int new_data] { //check if prev node is null if [prev_node == NULL] { coutnext = prev_node->next; //set next of prev node to newnode prev_node->next = newNode; //now set prev of newnode to prev node newNode->prev = prev_node; //set prev of new node's next to newnode if [newNode->next != NULL] newNode->next->prev = newNode; } //insert a new node at the end of the list void insert_end[struct Node** head, int new_data] { //allocate memory for node struct Node* newNode = new Node; struct Node* last = *head; //set last node value to head //set data for new node newNode->data = new_data; //new node is the last node , so set next of new node to null newNode->next = NULL; //check if list is empty, if yes make new node the head of list if [*head == NULL] { newNode->prev = NULL; *head = newNode; return; } //otherwise traverse the list to go to last node while [last->next != NULL] last = last->next; //set next of last to new node last->next = newNode; //set last to prev of new node newNode->prev = last; return; } // This function prints contents of linked list starting from the given node void displayList[struct Node* node] { struct Node* last; while [node != NULL] { cout2 --->3 with node structure as below:

struct node { int data; struct node *next; };

Video liên quan

Bài mới nhất

Chủ Đề