
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete a node in a Doubly Linked List in C++
In this tutorial, we are going to learn how to delete a node in doubly linked list.
Let's see the steps to solve the problem.
Write struct with data, prev and next pointers.
Write a function to insert the node into the doubly linked list.
Initialize the doubly linked list with dummy data.
Take a node to delete.
Write a function to delete the node. Consider the following three cases while deleting the node.
If the node is head node, then move the head to next node.
If the node is middle node, then link the next node to the previous node
If the node is end node, then remove the previous node link. Let's see the code.
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *prev, *next; }; void deleteNode(Node** head_ref, Node* del) { if (*head_ref == NULL || del == NULL) { return; } if (*head_ref == del) { *head_ref = del->next; } if (del->next != NULL) { del->next->prev = del->prev; } if (del->prev != NULL) { del->prev->next = del->next; } free(del); return; } void insertNode(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->prev = NULL; new_node->next = (*head_ref); if ((*head_ref) != NULL) { (*head_ref)->prev = new_node; } (*head_ref) = new_node; } void printLinkedList(Node* node) { while (node != NULL) { cout << node->data << " -> "; node = node->next; } } int main() { Node* head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); insertNode(&head, 4); insertNode(&head, 5); cout << "Linked List before deletion:" << endl; printLinkedList(head); deleteNode(&head, head->next); cout << "\nLinked List after deletion:" << endl; printLinkedList(head); return 0; }
Output
If you execute the above program, then you will get the following result.
Linked List before deletion: 5 -> 4 -> 3 -> 2 -> 1 -> Linked List after deletion: 5 -> 3 -> 2 -> 1 ->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Delete a Doubly Linked List node at a given position in C++
- Delete Node in a Linked List in Python
- Python program to delete a new node from the beginning of the doubly linked list
- Python program to delete a new node from the end of the doubly linked list
- Python program to delete a new node from the middle of the doubly linked list
- Delete all Prime Nodes from a Doubly Linked List in C++
- Find the largest node in Doubly linked list in C++
- Delete a Linked List node at a given position in C++
- Delete all the even nodes from a Doubly Linked List in C++
- Delete a Node from linked list without head pointer in C++
- Golang Program to delete the first node from a linked list.
- Golang Program to delete the last node from a linked list.
- C++ Program to Delete the First Node in a given Singly Linked List
- Delete a tail node from the given singly Linked List using C++
- Python program to find the maximum and minimum value node from a doubly linked list
