Skip to content

Commit a5adda8

Browse files
Create Doubly_linkedlist.c
This C program implements a Doubly Linked List (DLL), a linear data structure in which each node contains three parts — data, a pointer to the previous node, and a pointer to the next node. Unlike a singly linked list, a doubly linked list allows bidirectional traversal, enabling easier insertion and deletion from both ends of the list. The program provides a menu-driven interface that allows users to: Insert a node at the beginning of the list Insert a node at the end of the list Delete a specific node by its value Display the list in forward order (from head to tail) Display the list in reverse order (from tail to head) It uses dynamic memory allocation (malloc) for creating new nodes and ensures that node connections are properly updated during insertions and deletions. The program continues running until the user chooses to exit.
1 parent e5dad3f commit a5adda8

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// Define structure for a node
5+
struct Node {
6+
int data;
7+
struct Node* prev;
8+
struct Node* next;
9+
};
10+
11+
// Head pointer
12+
struct Node* head = NULL;
13+
14+
// Function to create a new node
15+
struct Node* createNode(int data) {
16+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
17+
newNode->data = data;
18+
newNode->prev = NULL;
19+
newNode->next = NULL;
20+
return newNode;
21+
}
22+
23+
// Insert at the beginning
24+
void insertAtBeginning(int data) {
25+
struct Node* newNode = createNode(data);
26+
if (head == NULL)
27+
head = newNode;
28+
else {
29+
newNode->next = head;
30+
head->prev = newNode;
31+
head = newNode;
32+
}
33+
printf("Inserted %d at the beginning.\n", data);
34+
}
35+
36+
// Insert at the end
37+
void insertAtEnd(int data) {
38+
struct Node* newNode = createNode(data);
39+
if (head == NULL) {
40+
head = newNode;
41+
return;
42+
}
43+
struct Node* temp = head;
44+
while (temp->next != NULL)
45+
temp = temp->next;
46+
temp->next = newNode;
47+
newNode->prev = temp;
48+
printf("Inserted %d at the end.\n", data);
49+
}
50+
51+
// Delete a node by value
52+
void deleteNode(int data) {
53+
if (head == NULL) {
54+
printf("List is empty.\n");
55+
return;
56+
}
57+
struct Node* temp = head;
58+
59+
// Traverse to find the node
60+
while (temp != NULL && temp->data != data)
61+
temp = temp->next;
62+
63+
if (temp == NULL) {
64+
printf("Node with value %d not found.\n", data);
65+
return;
66+
}
67+
68+
// Adjust pointers
69+
if (temp->prev != NULL)
70+
temp->prev->next = temp->next;
71+
else
72+
head = temp->next; // deleting head
73+
74+
if (temp->next != NULL)
75+
temp->next->prev = temp->prev;
76+
77+
free(temp);
78+
printf("Deleted node with value %d.\n", data);
79+
}
80+
81+
// Display the list forward
82+
void displayForward() {
83+
struct Node* temp = head;
84+
if (temp == NULL) {
85+
printf("List is empty.\n");
86+
return;
87+
}
88+
printf("List (Forward): ");
89+
while (temp != NULL) {
90+
printf("%d ", temp->data);
91+
temp = temp->next;
92+
}
93+
printf("\n");
94+
}
95+
96+
// Display the list backward
97+
void displayBackward() {
98+
struct Node* temp = head;
99+
if (temp == NULL) {
100+
printf("List is empty.\n");
101+
return;
102+
}
103+
while (temp->next != NULL)
104+
temp = temp->next;
105+
106+
printf("List (Backward): ");
107+
while (temp != NULL) {
108+
printf("%d ", temp->data);
109+
temp = temp->prev;
110+
}
111+
printf("\n");
112+
}
113+
114+
// Main menu
115+
int main() {
116+
int choice, data;
117+
while (1) {
118+
printf("\n--- Doubly Linked List Menu ---\n");
119+
printf("1. Insert at Beginning\n");
120+
printf("2. Insert at End\n");
121+
printf("3. Delete a Node\n");
122+
printf("4. Display Forward\n");
123+
printf("5. Display Backward\n");
124+
printf("6. Exit\n");
125+
printf("Enter your choice: ");
126+
scanf("%d", &choice);
127+
128+
switch (choice) {
129+
case 1:
130+
printf("Enter data: ");
131+
scanf("%d", &data);
132+
insertAtBeginning(data);
133+
break;
134+
case 2:
135+
printf("Enter data: ");
136+
scanf("%d", &data);
137+
insertAtEnd(data);
138+
break;
139+
case 3:
140+
printf("Enter data to delete: ");
141+
scanf("%d", &data);
142+
deleteNode(data);
143+
break;
144+
case 4:
145+
displayForward();
146+
break;
147+
case 5:
148+
displayBackward();
149+
break;
150+
case 6:
151+
printf("Exiting program.\n");
152+
exit(0);
153+
default:
154+
printf("Invalid choice. Try again.\n");
155+
}
156+
}
157+
return 0;
158+
}

0 commit comments

Comments
 (0)