Doubly Linked List
What is a Doubly Linked List (DLL)?
A doubly linked list is a type of linked list where each node has three parts:
-
Data (the value)
-
A pointer to the next node (next)
-
A pointer to the previous node (prev)
This allows movement in both directions — forward and backward.
Each node is like this:
[ prev | data | next ]
📘 Algorithms for Doubly Linked List
1. Insert at Beginning
5. Delete from End
6. Delete at Given Position
8. Traversal Backward
(First move to last node, then back using prev
)
📌 Important Notes
-
Always adjust both
prev
andnext
pointers carefully. -
Check NULL conditions before dereferencing.
-
Free the nodes after deletion to avoid memory leaks.
C program Doubly Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head = NULL;
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(0);
}
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Insert at beginning
void insertAtBeginning(int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("%d inserted at beginning\n", value);
}
// Insert at end
void insertAtEnd(int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("%d inserted at end\n", value);
}
// Insert at given position
void insertAtPosition(int value, int position) {
if (position == 1) {
insertAtBeginning(value);
return;
}
struct Node* newNode = createNode(value);
struct Node* temp = head;
int i;
for (i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of bounds\n");
free(newNode);
return;
}
newNode->next = temp->next;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
printf("%d inserted at position %d\n", value, position);
}
// Delete at beginning
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
printf("Deleted %d from beginning\n", temp->data);
free(temp);
}
// Delete at end
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = head;
if (temp->next == NULL) {
printf("Deleted %d from end\n", temp->data);
free(temp);
head = NULL;
return;
}
while (temp->next != NULL)
temp = temp->next;
temp->prev->next = NULL;
printf("Deleted %d from end\n", temp->data);
free(temp);
}
// Delete at given position
void deleteAtPosition(int position) {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (position == 1) {
deleteAtBeginning();
return;
}
struct Node* temp = head;
int i;
for (i = 1; i < position && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of bounds\n");
return;
}
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
printf("Deleted %d from position %d\n", temp->data, position);
free(temp);
}
// Traverse forward
void traverseForward() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = head;
printf("List (forward): ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Traverse backward
void traverseBackward() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
printf("List (backward): ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
}
// Main function with menu
int main() {
int choice, value, position;
while (1) {
printf("\n--- Doubly Linked List Operations ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete at Beginning\n");
printf("5. Delete at End\n");
printf("6. Delete at Position\n");
printf("7. Traverse Forward\n");
printf("8. Traverse Backward\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value and position to insert: ");
scanf("%d%d", &value, &position);
insertAtPosition(value, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(position);
break;
case 7:
traverseForward();
break;
case 8:
traverseBackward();
break;
case 9:
printf("Exiting program\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}
Comments
Post a Comment