-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubleEndedQueue.c
173 lines (150 loc) · 4.41 KB
/
doubleEndedQueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<stdlib.h>
// Structure to represent a queue
struct queue {
int* a; // Array to store elements of the queue
int front; // Front of the queue
int rear; // Rear of the queue
} q;
int size; // Size of the queue
// Function to enqueue an element at the rear of the queue
void renqueue() {
int item, i;
printf("Enter element to enqueue: ");
scanf("%d", &item);
// Check if the queue is full and reallocate memory if necessary
if (q.rear == size - 1) {
printf("Queue is full, reallocating memory...\n");
size++;
q.a = (int*)realloc(q.a, size * sizeof(int));
}
// If queue is empty, set front and rear to 0 and insert the element
if (q.front == -1) {
q.front = 0;
q.rear = 0;
*(q.a + q.rear) = item;
return;
}
// Increment rear and insert the element
q.rear++;
*(q.a + q.rear) = item;
}
// Function to enqueue an element at the front of the queue
void fenqueue() {
int item, i;
printf("Enter element to enqueue: ");
scanf("%d", &item);
// Check if the queue is full and reallocate memory if necessary
if (q.front == 0 && q.rear == size - 1) {
printf("Queue is full, reallocating memory...\n");
size++;
q.a = (int*)realloc(q.a, size * sizeof(int));
}
// If queue is empty, set front and rear to 0 and insert the element
if (q.front == -1) {
q.front = 0;
q.rear = 0;
*(q.a + q.rear) = item;
return;
}
// If front is at the beginning, shift elements to make space for the new element
if (q.front == 0) {
for (i = q.rear; i >= 0; i--) {
*(q.a + i + 1) = *(q.a + i);
}
// Insert the element at the front
*(q.a + 0) = item;
q.rear++;
return;
} else {
// Decrement front and insert the element
q.front--;
*(q.a + q.front) = item;
}
}
// Function to dequeue an element from the front of the queue
void fdequeue() {
// Check if the queue is empty
if (q.front == -1) {
printf("Queue is empty\n");
return;
}
// Print the dequeued element and update front
printf("Dequeued element: %d\n", *(q.a + q.front));
// If queue has only one element, set front and rear to -1
if (q.front == q.rear) {
q.front = q.rear = -1;
} else {
// Increment front
q.front++;
}
}
// Function to dequeue an element from the rear of the queue
void rdequeue() {
// Check if the queue is empty
if (q.rear == -1) {
printf("Queue is empty\n");
return;
}
// Print the dequeued element and update rear
printf("Dequeued element: %d\n", *(q.a + q.rear));
// If queue has only one element, set front and rear to -1
if (q.front == q.rear) {
q.front = q.rear = -1;
} else {
// Decrement rear
q.rear--;
}
}
// Function to display the elements of the queue
void display() {
// Check if the queue is empty
if (q.front == -1) {
printf("Queue is empty\n");
return;
}
// Print the elements of the queue
printf("Queue elements: ");
for (int i = q.front; i <= q.rear; i++) {
printf("%d ", *(q.a + i));
}
printf("\n");
}
// Main function
void main() {
q.front = q.rear = -1;
int choice;
printf("Enter the size of the queue: ");
scanf("%d", &size);
q.a = (int*)malloc(size * sizeof(int));
printf("Menu:\n1. Insert at front\n2. Insert at rear\n3. Delete from front\n4. Delete from rear\n5. Display\n6. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
fenqueue();
display();
break;
case 2:
renqueue();
display();
break;
case 3:
fdequeue();
display();
break;
case 4:
rdequeue();
display();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("Enter a valid choice\n");
}
}
}