- Data Structures & Algorithms
- DSA - Home
- DSA - Introduction
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Characteristics of Algorithms
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Single Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Tries
- DSA - Heap Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Subsequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
Data Structures & Algorithms - Double Linked List Algorithm
![]() Share with a Friend |
Double Linked List
A Doubly Linked List is a type of linked list in which each node contains three parts:
- Data
- Pointer to the next node (next)
- Pointer to the previous node (prev)
This allows traversal in both directions — forward and backward.
Example Representation:
NULL <- [prev | data | next] <-> [prev | data | next] -> NULL
✅ Operations on Doubly Linked List:
- Insertion at Beginning
- Insertion at End
- Insertion at Position
- Deletion
- Traversal (Forward & Backward)
1️⃣ Double Linked List Implementation
🔹 C Program: Create & Traverse DLL
🔹 C++ Program: Create & Traverse DLL
🔹 Java Program: Create & Traverse DLL
🔹 Python Program: Create & Traverse DLL
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *prev, *next;
};
void printList(struct Node* node) {
struct Node* last;
printf("Forward Traversal: ");
while (node != NULL) {
printf("%d ", node->data);
last = node;
node = node->next;
}
printf("\nBackward Traversal: ");
while (last != NULL) {
printf("%d ", last->data);
last = last->prev;
}
}
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->prev = NULL;
new_node->next = *head_ref;
if (*head_ref != NULL)
(*head_ref)->prev = new_node;
*head_ref = new_node;
}
int main() {
struct Node* head = NULL;
push(&head, 10);
push(&head, 20);
push(&head, 30);
printList(head);
return 0;
}
Output
Forward: 30 20 10
Backward: 10 20 30
#include <iostream>
using namespace std;
struct Node {
int data;
Node *prev, *next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void printList(Node* head) {
Node* tail = nullptr;
cout << "Forward: ";
while (head != nullptr) {
cout << head->data << " ";
tail = head;
head = head->next;
}
cout << "\nBackward: ";
while (tail != nullptr) {
cout << tail->data << " ";
tail = tail->prev;
}
cout << endl;
}
void push(Node*& head, int val) {
Node* newNode = new Node(val);
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
}
int main() {
Node* head = nullptr;
push(head, 10);
push(head, 20);
push(head, 30);
printList(head);
return 0;
}
Output
Forward: 30 20 10
Backward: 10 20 30
class Node {
int data;
Node prev, next;
Node(int data) {
this.data = data;
}
}
public class DLL {
Node head;
void push(int data) {
Node newNode = new Node(data);
newNode.next = head;
if (head != null)
head.prev = newNode;
head = newNode;
}
void printList() {
Node temp = head;
Node last = null;
System.out.print("Forward: ");
while (temp != null) {
System.out.print(temp.data + " ");
last = temp;
temp = temp.next;
}
System.out.print("\nBackward: ");
while (last != null) {
System.out.print(last.data + " ");
last = last.prev;
}
}
public static void main(String[] args) {
DLL list = new DLL();
list.push(10);
list.push(20);
list.push(30);
list.printList();
}
}
Output
Forward: 30 20 10
Backward: 10 20 30
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def push(self, data):
new_node = Node(data)
new_node.next = self.head
if self.head:
self.head.prev = new_node
self.head = new_node
def print_list(self):
temp = self.head
last = None
print("Forward:", end=" ")
while temp:
print(temp.data, end=" ")
last = temp
temp = temp.next
print("\nBackward:", end=" ")
while last:
print(last.data, end=" ")
last = last.prev
dll = DoublyLinkedList()
dll.push(10)
dll.push(20)
dll.push(30)
dll.print_list()
Output
Forward: 30 20 10
Backward: 10 20 30
🔧 Operations on Doubly Linked List
🔹 1. Insertion
- At Beginning
- At End
- At Specific Position
🔹 2. Deletion
- From Beginning
- From End
- At Specific Position
🔹 3. Traversal
- Forward
- Backward
✅ 1. Insertion Operations
🔸 A. Insertion at Beginning
Logic:
- Create a new node.
- Point its next to current head.
- If head exists, point its prev to new node.
- Make new node the head.
🔸 B. Insertion at End
Logic:
- Traverse to the last node.
- Create new node and set last.next to it.
- Set new node’s prev to the last node.
🔸 C. Insertion at Specific Position
Logic:
- Traverse to the position.
- Insert node by adjusting prev and next pointers of adjacent nodes.
✅ 2. Deletion Operations
🔸 A. Delete from Beginning
Logic:
- Point head to head.next.
- Set head.prev = NULL.
🔸 B. Delete from End
Logic:
- Traverse to last node.
- Set second_last.next = NULL.
🔸 C. Delete at Specific Position
Logic:
- Traverse to the position.
- Adjust next of previous node and prev of next node.
✅ 3. Traversal Operations
🔸 A. Forward Traversal
Start from head and move using next pointer.
🔸 B. Backward Traversal
Reach the last node, then move using prev pointer.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void display(struct Node* node) {
struct Node* last;
printf("Forward: ");
while (node != NULL) {
printf("%d ", node->data);
last = node;
node = node->next;
}
printf("\nBackward: ");
while (last != NULL) {
printf("%d ", last->data);
last = last->prev;
}
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
display(head);
return 0;
}
Output
Forward: 10 20
Backward: 20 10
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
Node(int d): data(d), prev(NULL), next(NULL) {}
};
void insertAtEnd(Node*& head, int data) {
Node* newNode = new Node(data);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void display(Node* head) {
Node* last = NULL;
cout << "Forward: ";
while (head) {
cout << head->data << " ";
last = head;
head = head->next;
}
cout << "\nBackward: ";
while (last) {
cout << last->data << " ";
last = last->prev;
}
}
int main() {
Node* head = NULL;
insertAtEnd(head, 10);
insertAtEnd(head, 20);
display(head);
return 0;
}
Output
Forward: 10 20
Backward: 20 10
class Node {
int data;
Node prev, next;
Node(int d) { data = d; }
}
class DoublyLinkedList {
Node head;
void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
void display() {
Node temp = head, last = null;
System.out.print("Forward: ");
while (temp != null) {
System.out.print(temp.data + " ");
last = temp;
temp = temp.next;
}
System.out.print("\nBackward: ");
while (last != null) {
System.out.print(last.data + " ");
last = last.prev;
}
}
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertAtEnd(10);
dll.insertAtEnd(20);
dll.display();
}
}
Output
Forward: 10 20
Backward: 20 10
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DLL:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
if self.head:
self.head.prev = new_node
self.head = new_node
def insert_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.prev = temp
def delete_from_beginning(self):
if not self.head:
return
self.head = self.head.next
if self.head:
self.head.prev = None
def delete_from_end(self):
if not self.head:
return
temp = self.head
while temp.next:
temp = temp.next
if temp.prev:
temp.prev.next = None
else:
self.head = None
def traverse_forward(self):
temp = self.head
print("Forward:", end=" ")
while temp:
print(temp.data, end=" ")
last = temp
temp = temp.next
print()
def traverse_backward(self):
temp = self.head
if not temp: return
while temp.next:
temp = temp.next
print("Backward:", end=" ")
while temp:
print(temp.data, end=" ")
temp = temp.prev
print()
dll = DLL()
dll.insert_at_end(10)
dll.insert_at_end(20)
dll.insert_at_beginning(5)
dll.traverse_forward()
dll.traverse_backward()
dll.delete_from_beginning()
dll.traverse_forward()
dll.delete_from_end()
dll.traverse_forward()
Output
Forward: 5 10 20
Backward: 20 10 5
Forward: 10 20
Forward: 10
✅ 1. Insertion Operations
- At Beginning
- At End
- At Specific Position
Insertion in Doubly Linked List - C / C++ / Java / Python
🔹 C Program to Insert in Doubly Linked List
🔹 C++ Program to Insert in Doubly Linked List
🔹 Java Program to Insert in Doubly Linked List
🔹 Python Program to Insert in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Insert at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL)
(*head)->prev = newNode;
*head = newNode;
}
// Insert at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Insert at a specific position
void insertAtPosition(struct Node** head, int data, int pos) {
if (pos == 1) {
insertAtBeginning(head, data);
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
struct Node* temp = *head;
for (int i = 1; i < pos - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
}
// Display the list
void display(struct Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
// Main function
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 30);
insertAtEnd(&head, 50);
insertAtPosition(&head, 40, 2); // Insert 40 at position 2
insertAtBeginning(&head, 20);
insertAtEnd(&head, 60);
display(head);
return 0;
}
Output
Doubly Linked List: 20 30 40 50 60
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
// Function to insert a node at the beginning
void insertAtBeginning(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr)
head->prev = newNode;
head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
newNode->prev = nullptr;
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
// Function to insert a node at a specific position
void insertAtPosition(Node*& head, int value, int position) {
Node* newNode = new Node();
newNode->data = value;
if (position == 1) {
insertAtBeginning(head, value);
return;
}
Node* temp = head;
int count = 1;
while (temp != nullptr && count < position - 1) {
temp = temp->next;
count++;
}
if (temp == nullptr) {
cout << "Position out of bounds!" << endl;
return;
}
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
// Function to display the list
void display(Node* head) {
Node* temp = head;
cout << "Doubly Linked List: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
Node* head = nullptr;
// Insert elements
insertAtBeginning(head, 10); // Insert at beginning
insertAtEnd(head, 20); // Insert at end
insertAtEnd(head, 30); // Insert at end
insertAtPosition(head, 25, 3); // Insert at position 3
insertAtBeginning(head, 5); // Insert at beginning
// Display the list
display(head);
return 0;
}
Output
Doubly Linked List: 5 10 20 25 30
public class DoublyLinkedList {
class Node {
int data;
Node prev;
Node next;
Node(int value) {
data = value;
}
}
Node head = null;
// Insert at the beginning
public void insertAtBeginning(int value) {
Node newNode = new Node(value);
newNode.next = head;
if (head != null)
head.prev = newNode;
head = newNode;
}
// Insert at the end
public void insertAtEnd(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
// Insert at a specific position
public void insertAtPosition(int value, int position) {
if (position <= 0) {
System.out.println("Invalid position!");
return;
}
if (position == 1) {
insertAtBeginning(value);
return;
}
Node newNode = new Node(value);
Node temp = head;
for (int i = 1; i < position - 1 && temp != null; i++)
temp = temp.next;
if (temp == null) {
System.out.println("Position out of bounds!");
return;
}
newNode.next = temp.next;
newNode.prev = temp;
if (temp.next != null)
temp.next.prev = newNode;
temp.next = newNode;
}
// Display the list
public void display() {
Node temp = head;
System.out.print("Doubly Linked List: ");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertAtBeginning(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
dll.insertAtPosition(25, 3);
dll.insertAtBeginning(5);
dll.display();
}
}
Output
Doubly Linked List: 5 10 20 25 30
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
# Insert at the beginning
def insert_at_beginning(self, value):
new_node = Node(value)
new_node.next = self.head
if self.head:
self.head.prev = new_node
self.head = new_node
# Insert at the end
def insert_at_end(self, value):
new_node = Node(value)
if not self.head:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.prev = temp
# Insert at a specific position
def insert_at_position(self, value, position):
if position <= 0:
print("Invalid position!")
return
if position == 1:
self.insert_at_beginning(value)
return
new_node = Node(value)
temp = self.head
for _ in range(position - 2):
if temp is None:
print("Position out of bounds!")
return
temp = temp.next
if temp is None:
print("Position out of bounds!")
return
new_node.next = temp.next
new_node.prev = temp
if temp.next:
temp.next.prev = new_node
temp.next = new_node
# Display the list
def display(self):
temp = self.head
print("Doubly Linked List:", end=" ")
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
# Example usage
dll = DoublyLinkedList()
dll.insert_at_beginning(10)
dll.insert_at_end(20)
dll.insert_at_end(30)
dll.insert_at_position(25, 3)
dll.insert_at_beginning(5)
dll.display()
Output
Doubly Linked List: 5 10 20 25 30
✅ 2. Deletion Operations
- Delete from the beginning
- Delete from the end
- Delete from a specific position
Deletion in Doubly Linked List - C / C++ / Java / Python
🔹 C Program to Delete in Doubly Linked List
🔹 C++ Program to Delete in Doubly Linked List
🔹 Java Program to Delete in Doubly Linked List
🔹 Python Program to Delete in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
// Function to create and insert node at the end
void insertEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Delete from beginning
void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
head = head->next;
if (head)
head->prev = NULL;
printf("Deleted: %d\n", temp->data);
free(temp);
}
// Delete from end
void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
if (temp->next == NULL) {
head = NULL;
printf("Deleted: %d\n", temp->data);
free(temp);
return;
}
while (temp->next)
temp = temp->next;
temp->prev->next = NULL;
printf("Deleted: %d\n", temp->data);
free(temp);
}
// Delete from specific position
void deleteFromPosition(int position) {
if (head == NULL || position <= 0) {
printf("Invalid position or empty list.\n");
return;
}
struct Node* temp = head;
int i = 1;
while (temp && i < position) {
temp = temp->next;
i++;
}
if (!temp) {
printf("Position out of bounds.\n");
return;
}
if (temp->prev)
temp->prev->next = temp->next;
else
head = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
printf("Deleted: %d\n", temp->data);
free(temp);
}
// Display list
void display() {
struct Node* temp = head;
printf("List: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Main function
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);
insertEnd(40);
insertEnd(50);
display();
deleteFromBeginning();
display();
deleteFromEnd();
display();
deleteFromPosition(2);
display();
return 0;
}
Output
List: 10 20 30 40 50
Deleted: 10
List: 20 30 40 50
Deleted: 50
List: 20 30 40
Deleted: 30
List: 20 40
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = nullptr;
// Insert node at the end
void insertEnd(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
newNode->prev = nullptr;
head = newNode;
return;
}
Node* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Delete from beginning
void deleteFromBeginning() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
head = head->next;
if (head)
head->prev = nullptr;
cout << "Deleted: " << temp->data << endl;
delete temp;
}
// Delete from end
void deleteFromEnd() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
if (temp->next == nullptr) {
cout << "Deleted: " << temp->data << endl;
delete temp;
head = nullptr;
return;
}
while (temp->next)
temp = temp->next;
temp->prev->next = nullptr;
cout << "Deleted: " << temp->data << endl;
delete temp;
}
// Delete from specific position
void deleteFromPosition(int position) {
if (head == nullptr || position <= 0) {
cout << "Invalid position or empty list.\n";
return;
}
Node* temp = head;
int i = 1;
while (temp && i < position) {
temp = temp->next;
i++;
}
if (!temp) {
cout << "Position out of bounds.\n";
return;
}
if (temp->prev)
temp->prev->next = temp->next;
else
head = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
cout << "Deleted: " << temp->data << endl;
delete temp;
}
// Display the list
void display() {
Node* temp = head;
cout << "List: ";
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Main function
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);
insertEnd(40);
insertEnd(50);
display();
deleteFromBeginning();
display();
deleteFromEnd();
display();
deleteFromPosition(2);
display();
return 0;
}
Output
List: 10 20 30 40 50
Deleted: 10
List: 20 30 40 50
Deleted: 50
List: 20 30 40
Deleted: 30
List: 20 40
class DoublyLinkedList {
class Node {
int data;
Node prev, next;
Node(int data) {
this.data = data;
}
}
Node head = null;
// Insert at end
void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
// Delete from beginning
void deleteFromBeginning() {
if (head == null) {
System.out.println("List is empty.");
return;
}
System.out.println("Deleted: " + head.data);
head = head.next;
if (head != null)
head.prev = null;
}
// Delete from end
void deleteFromEnd() {
if (head == null) {
System.out.println("List is empty.");
return;
}
if (head.next == null) {
System.out.println("Deleted: " + head.data);
head = null;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
System.out.println("Deleted: " + temp.data);
temp.prev.next = null;
}
// Delete from specific position
void deleteFromPosition(int position) {
if (head == null || position <= 0) {
System.out.println("Invalid position or list is empty.");
return;
}
Node temp = head;
int i = 1;
while (temp != null && i < position) {
temp = temp.next;
i++;
}
if (temp == null) {
System.out.println("Position out of bounds.");
return;
}
System.out.println("Deleted: " + temp.data);
if (temp.prev != null)
temp.prev.next = temp.next;
else
head = temp.next;
if (temp.next != null)
temp.next.prev = temp.prev;
}
// Display list
void display() {
Node temp = head;
System.out.print("List: ");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertEnd(10);
dll.insertEnd(20);
dll.insertEnd(30);
dll.insertEnd(40);
dll.insertEnd(50);
dll.display();
dll.deleteFromBeginning();
dll.display();
dll.deleteFromEnd();
dll.display();
dll.deleteFromPosition(2);
dll.display();
}
}
Output
List: 10 20 30 40 50
Deleted: 10
List: 20 30 40 50
Deleted: 50
List: 20 30 40
Deleted: 30
List: 20 40
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
# Insert at end
def insert_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.prev = temp
# Delete from beginning
def delete_from_beginning(self):
if not self.head:
print("List is empty.")
return
print("Deleted:", self.head.data)
self.head = self.head.next
if self.head:
self.head.prev = None
# Delete from end
def delete_from_end(self):
if not self.head:
print("List is empty.")
return
if not self.head.next:
print("Deleted:", self.head.data)
self.head = None
return
temp = self.head
while temp.next:
temp = temp.next
print("Deleted:", temp.data)
temp.prev.next = None
# Delete from specific position
def delete_from_position(self, position):
if not self.head or position <= 0:
print("Invalid position or empty list.")
return
temp = self.head
i = 1
while temp and i < position:
temp = temp.next
i += 1
if not temp:
print("Position out of bounds.")
return
print("Deleted:", temp.data)
if temp.prev:
temp.prev.next = temp.next
else:
self.head = temp.next
if temp.next:
temp.next.prev = temp.prev
# Display the list
def display(self):
temp = self.head
print("List:", end=" ")
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
# Main execution
dll = DoublyLinkedList()
dll.insert_end(10)
dll.insert_end(20)
dll.insert_end(30)
dll.insert_end(40)
dll.insert_end(50)
dll.display()
dll.delete_from_beginning()
dll.display()
dll.delete_from_end()
dll.display()
dll.delete_from_position(2)
dll.display()
Output
List: 10 20 30 40 50
Deleted: 10
List: 20 30 40 50
Deleted: 50
List: 20 30 40
Deleted: 30
List: 20 40
✅ 3. Traversal Operations
- Forward Traversal
- Backward Traversal
Traversal in Doubly Linked List - C / C++ / Java / Python
🔹 C Program to Traverse Operations in Doubly Linked List
🔹 C++ Program to Traverse Operations in Doubly Linked List
🔹 Java Program to Traverse Operations in Doubly Linked List
🔹 Python Program to Traverse Operations in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define Node
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Insert node at end
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Forward traversal
void traverseForward(struct Node* head) {
printf("Forward Traversal: ");
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
if (temp->next == NULL)
break;
temp = temp->next;
}
printf("\n");
}
// Backward traversal
void traverseBackward(struct Node* tail) {
printf("Backward Traversal: ");
struct Node* temp = tail;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
}
// Main
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
insertEnd(&head, 50);
traverseForward(head);
// To get the tail node
struct Node* tail = head;
while (tail && tail->next != NULL)
tail = tail->next;
traverseBackward(tail);
return 0;
}
Output
Forward Traversal: 10 20 30 40 50
Backward Traversal: 50 40 30 20 10
#include <iostream>
using namespace std;
// Node structure
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
// Class for Doubly Linked List
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
// Insert at the end
void insertEnd(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Forward traversal
void traverseForward() {
cout << "Forward Traversal: ";
Node* temp = head;
while (temp) {
cout << temp->data << " ";
if (!temp->next) break;
temp = temp->next;
}
cout << endl;
}
// Backward traversal
void traverseBackward() {
cout << "Backward Traversal: ";
if (!head) return;
Node* temp = head;
while (temp->next) // Move to last node
temp = temp->next;
while (temp) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
};
// Main function
int main() {
DoublyLinkedList dll;
dll.insertEnd(10);
dll.insertEnd(20);
dll.insertEnd(30);
dll.insertEnd(40);
dll.insertEnd(50);
dll.traverseForward();
dll.traverseBackward();
return 0;
}
Output
Forward Traversal: 10 20 30 40 50
Backward Traversal: 50 40 30 20 10
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
// Constructor
public DoublyLinkedList() {
head = null;
}
// Insert node at the end
public void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
// Forward traversal
public void traverseForward() {
System.out.print("Forward Traversal: ");
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
// Backward traversal
public void traverseBackward() {
System.out.print("Backward Traversal: ");
if (head == null) return;
Node temp = head;
while (temp.next != null) { // Traverse to the last node
temp = temp.next;
}
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.prev;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertEnd(10);
dll.insertEnd(20);
dll.insertEnd(30);
dll.insertEnd(40);
dll.insertEnd(50);
dll.traverseForward();
dll.traverseBackward();
}
}
Output
Forward Traversal: 10 20 30 40 50
Backward Traversal: 50 40 30 20 10
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
# Insert node at the end
def insert_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.prev = temp
# Forward traversal
def traverse_forward(self):
print("Forward Traversal: ", end="")
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
# Backward traversal
def traverse_backward(self):
print("Backward Traversal: ", end="")
if not self.head:
return
temp = self.head
while temp.next: # Traverse to last node
temp = temp.next
while temp:
print(temp.data, end=" ")
temp = temp.prev
print()
# Main function
if __name__ == "__main__":
dll = DoublyLinkedList()
dll.insert_end(10)
dll.insert_end(20)
dll.insert_end(30)
dll.insert_end(40)
dll.insert_end(50)
dll.traverse_forward()
dll.traverse_backward()
Output
Forward Traversal: 10 20 30 40 50
Backward Traversal: 50 40 30 20 10
✅ 4. Search, Sort, and Reverse in a Doubly Linked List
- C program to perform Search, Sort, and Reverse in a Doubly Linked List
- C++ program to perform Search, Sort, and Reverse in a Doubly Linked List
- Java program to perform Search, Sort, and Reverse in a Doubly Linked List
- Python program to perform Search, Sort, and Reverse in a Doubly Linked List
Search, Sort and Reverse in Doubly Linked List - C / C++ / Java / Python
🔹 C Program to Search, Sort and Reverse Operations in Doubly Linked List
🔹 C++ Program to Search, Sort and Reverse Operations in Doubly Linked List
🔹 Java Program to Search, Sort and Reverse Operations in Doubly Linked List
🔹 Python Program to Search, Sort and Reverse Operations in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *prev, *next;
};
struct Node* head = NULL;
// Function to insert at the end
void insertEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Function to traverse the list
void traverse() {
struct Node* temp = head;
printf("List: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to search an element
void search(int key) {
struct Node* temp = head;
while (temp) {
if (temp->data == key) {
printf("Element %d found in the list.\n", key);
return;
}
temp = temp->next;
}
printf("Element %d not found in the list.\n", key);
}
// Function to sort the list using Bubble Sort
void sortList() {
struct Node *i, *j;
int temp;
for (i = head; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
printf("List sorted.\n");
}
// Function to reverse the doubly linked list
void reverseList() {
struct Node *temp = NULL, *current = head;
while (current != NULL) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != NULL)
head = temp->prev;
printf("List reversed.\n");
}
// Main function to test
int main() {
insertEnd(40);
insertEnd(20);
insertEnd(10);
insertEnd(30);
traverse();
search(10);
search(99);
sortList();
traverse();
reverseList();
traverse();
return 0;
}
Output
List: 40 20 10 30
Element 10 found in the list.
Element 99 not found in the list.
List sorted.
List: 10 20 30 40
List reversed.
List: 40 30 20 10
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = nullptr;
// Function to insert at the end
void insertEnd(int data) {
Node* newNode = new Node{data, nullptr, nullptr};
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Function to traverse the list
void traverse() {
Node* temp = head;
cout << "List: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Function to search an element
void search(int key) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == key) {
cout << "Element " << key << " found in the list." << endl;
return;
}
temp = temp->next;
}
cout << "Element " << key << " not found in the list." << endl;
}
// Function to sort the list
void sortList() {
for (Node* i = head; i != nullptr; i = i->next) {
for (Node* j = i->next; j != nullptr; j = j->next) {
if (i->data > j->data) {
int temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
cout << "List sorted." << endl;
}
// Function to reverse the list
void reverseList() {
Node* current = head;
Node* temp = nullptr;
while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr)
head = temp->prev;
cout << "List reversed." << endl;
}
// Main function
int main() {
insertEnd(50);
insertEnd(20);
insertEnd(10);
insertEnd(40);
traverse();
search(10);
search(99);
sortList();
traverse();
reverseList();
traverse();
return 0;
}
Output
List: 50 20 10 40
Element 10 found in the list.
Element 99 not found in the list.
List sorted.
List: 10 20 40 50
List reversed.
List: 50 40 20 10
class DoublyLinkedList {
class Node {
int data;
Node prev, next;
Node(int data) {
this.data = data;
}
}
Node head;
// Insert node at the end
void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
// Traverse the list
void traverse() {
Node temp = head;
System.out.print("List: ");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
// Search a node
void search(int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) {
System.out.println("Element " + key + " found in the list.");
return;
}
temp = temp.next;
}
System.out.println("Element " + key + " not found in the list.");
}
// Sort the list using Bubble Sort
void sortList() {
for (Node i = head; i != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.data > j.data) {
int temp = i.data;
i.data = j.data;
j.data = temp;
}
}
}
System.out.println("List sorted.");
}
// Reverse the list
void reverseList() {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null)
head = temp.prev;
System.out.println("List reversed.");
}
// Main method to test
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertEnd(40);
dll.insertEnd(10);
dll.insertEnd(30);
dll.insertEnd(20);
dll.traverse();
dll.search(10);
dll.search(99);
dll.sortList();
dll.traverse();
dll.reverseList();
dll.traverse();
}
}
Output
List: 40 10 30 20
Element 10 found in the list.
Element 99 not found in the list.
List sorted.
List: 10 20 30 40
List reversed.
List: 40 30 20 10
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
# Insert at the end
def insert_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.prev = temp
# Traverse the list
def traverse(self):
temp = self.head
print("List:", end=" ")
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
# Search an element
def search(self, key):
temp = self.head
while temp:
if temp.data == key:
print(f"Element {key} found in the list.")
return
temp = temp.next
print(f"Element {key} not found in the list.")
# Sort the list
def sort_list(self):
if self.head is None:
return
i = self.head
while i:
j = i.next
while j:
if i.data > j.data:
i.data, j.data = j.data, i.data
j = j.next
i = i.next
print("List sorted.")
# Reverse the list
def reverse_list(self):
current = self.head
temp = None
while current:
temp = current.prev
current.prev = current.next
current.next = temp
current = current.prev
if temp:
self.head = temp.prev
print("List reversed.")
# Driver Code
dll = DoublyLinkedList()
dll.insert_end(30)
dll.insert_end(10)
dll.insert_end(20)
dll.insert_end(40)
dll.traverse()
dll.search(10)
dll.search(99)
dll.sort_list()
dll.traverse()
dll.reverse_list()
dll.traverse()
Output
List: 30 10 20 40
Element 10 found in the list.
Element 99 not found in the list.
List sorted.
List: 10 20 30 40
List reversed.
List: 40 30 20 10
