-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_endedq.c
132 lines (121 loc) · 2.37 KB
/
double_endedq.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int f = 0, r = -1, q[MAX], ele;
void insert_rear()
{
if (r == MAX - 1)
{
printf("Queue overflow\n");
return;
}
printf("Enter the element to insert:");
scanf("%d", &ele);
r++;
q[r] = ele;
return;
}
void delete_front()
{
if (f > r)
{
printf("Queue underflow\n");
return;
}
ele = q[f];
f++;
printf("Element deleted is %d\n", ele);
if (f > r)
{
f = 0, r = -1;
}
return;
}
void insert_front()
{
if (f == 0 && r == -1)
{
printf("Enter element to insert:");
scanf("%d", &ele);
q[++r] = ele;
return;
}
if (f != 0)
{
printf("Enter element to insert:");
scanf("%d", &ele);
q[--f] = ele;
return;
}
printf("Element insertion not possible\n");
return;
}
void delete_rear()
{
if (f > r)
{
printf("Queue underflow\n");
return;
}
ele = q[r];
r--;
printf("Element deleted is %d\n", ele);
if (f > r)
{
f = 0, r = -1;
}
return;
}
void display()
{
if (f > r)
{
printf("Queue underflow\n");
}
printf("Elements of queue:\n");
for (int i = f; i <= r; i++)
{
printf("%d\t", q[i]);
}
printf("\n");
}
int main()
{
int choice;
do
{
printf("\n----- Queue Menu -----\n");
printf("1. Insert Rear\n");
printf("2. Delete Front\n");
printf("3. Insert Front\n");
printf("4. Delete Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
insert_front();
break;
case 4:
delete_rear();
break;
case 5:
display();
break;
case 6:
printf("Exiting the program. Bye!\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 6);
return 0;
}