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