- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete a Doubly Linked List node at a given position in C++
In this tutorial, we are going to learn how to delete a node in doubly linked list with the given position.
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.
Initialize the position to delete the node.
Iterate over the linked list and find the node with the given position to delete the node.
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.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *prev, *next; }; void deleteNode(struct Node** head_ref, struct Node* del) { if (*head_ref == NULL || del == NULL) { return; } // head node if (*head_ref == del) { *head_ref = del->next; } // middle node if (del->next != NULL) { del->next->prev = del->prev; } // end node if (del->prev != NULL) { del->prev->next = del->next; } free(del); } void deleteNodeAtGivenPosition(struct Node** head_ref, int n) { if (*head_ref == NULL || n <= 0) { return; } struct Node* current = *head_ref; int i; for (int i = 1; current != NULL && i < n; i++) { current = current->next; } if (current == NULL) { return; } deleteNode(head_ref, current); } void insertNode(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct 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(struct Node* head) { while (head != NULL) { cout << head->data << "->"; head = head->next; } } int main() { struct Node* head = NULL; insertNode(&head, 5); insertNode(&head, 2); insertNode(&head, 4); insertNode(&head, 8); insertNode(&head, 10); cout << "Doubly linked list before deletion" << endl; printLinkedList(head); int n = 2; deleteNodeAtGivenPosition(&head, n); cout << "\nDoubly linked list after deletion" << endl; printLinkedList(head); return 0; }
Output
If you execute the above program, then you will get the following result.
Doubly linked list before deletion 10->8->4->2->5-> Doubly linked list after deletion 10->4->2->5->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Delete a Linked List node at a given position in C++
- Delete a node in a Doubly Linked List in C++
- Delete a tail node from the given singly Linked List using C++
- C++ Program to Delete the First Node in a given Singly Linked List
- C# Program to add a node at the last position in a Linked List
- C# Program to add a node at the first position in a Linked List
- Delete all Prime Nodes from a Doubly Linked List in C++
- 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 Node in a Linked List in Python
- C program to insert a node at any position using double linked list
- Find the largest node in Doubly linked list 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++
