9/24/25, 12:23 PM DEqueue_using_linked_list
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 // Node structure
5 struct Node {
6 int data;
7 struct Node* prev;
8 struct Node* next;
9 };
10
11 struct Node* front = NULL;
12 struct Node* rear = NULL;
13
14 // Function to check if the deque is empty
15 int isEmpty() {
16 return (front == NULL);
17 }
18
19 // Insert at front
20 void insertFront(int value) {
21 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
22 newNode->data = value;
23 newNode->prev = NULL;
24 newNode->next = front;
25
26 if (isEmpty()) {
27 front = rear = newNode;
28 } else {
29 front->prev = newNode;
30 front = newNode;
31 }
32 printf("%d inserted at front.\n", value);
33 }
34
35 // Insert at rear
36 void insertRear(int value) {
37 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
38 newNode->data = value;
39 newNode->next = NULL;
40 newNode->prev = rear;
41
42 if (isEmpty()) {
43 front = rear = newNode;
44 } else {
45 rear->next = newNode;
46 rear = newNode;
47 }
48 printf("%d inserted at rear.\n", value);
49 }
50
51 // Delete from front
52 void deleteFront() {
53 if (isEmpty()) {
54 printf("Deque is empty! Cannot delete.\n");
55 return;
[Link] 1/3
9/24/25, 12:23 PM DEqueue_using_linked_list
56 }
57 struct Node* temp = front;
58 printf("%d deleted from front.\n", temp->data);
59
60 front = front->next;
61 if (front == NULL) rear = NULL;
62 else front->prev = NULL;
63
64 free(temp);
65 }
66
67 // Delete from rear
68 void deleteRear() {
69 if (isEmpty()) {
70 printf("Deque is empty! Cannot delete.\n");
71 return;
72 }
73 struct Node* temp = rear;
74 printf("%d deleted from rear.\n", temp->data);
75
76 rear = rear->prev;
77 if (rear == NULL) front = NULL;
78 else rear->next = NULL;
79
80 free(temp);
81 }
82
83 // Display deque
84 void display() {
85 if (isEmpty()) {
86 printf("Deque is empty!\n");
87 return;
88 }
89 struct Node* temp = front;
90 printf("Deque elements: ");
91 while (temp != NULL) {
92 printf("%d ", temp->data);
93 temp = temp->next;
94 }
95 printf("\n");
96 }
97
98 // Main menu
99 int main() {
100 int choice, value;
101
102 while (1) {
103 printf("\n--- DEQUE MENU ---\n");
104 printf("1. Insert at Front\n");
105 printf("2. Insert at Rear\n");
106 printf("3. Delete from Front\n");
107 printf("4. Delete from Rear\n");
108 printf("5. Display\n");
109 printf("6. Exit\n");
110 printf("Enter choice: ");
111 scanf("%d", &choice);
112
[Link] 2/3
9/24/25, 12:23 PM DEqueue_using_linked_list
113 switch (choice) {
114 case 1:
115 printf("Enter value: ");
116 scanf("%d", &value);
117 insertFront(value);
118 break;
119 case 2:
120 printf("Enter value: ");
121 scanf("%d", &value);
122 insertRear(value);
123 break;
124 case 3:
125 deleteFront();
126 break;
127 case 4:
128 deleteRear();
129 break;
130 case 5:
131 display();
132 break;
133 case 6:
134 exit(0);
135 default:
136 printf("Invalid choice! Try again.\n");
137 }
138 }
139 return 0;
140 }
141
142
[Link] 3/3