-
Notifications
You must be signed in to change notification settings - Fork 2
/
deque.c
168 lines (156 loc) · 3.18 KB
/
deque.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 5
struct queue
{
int front;
int rear;
int arr[max];
};
void enquefront(struct queue *en, int x)
{
if ((en->rear + 1) % max == en->front)
{
printf("the queue is full\n");
return;
}
else if (en->front == -1 && en->rear == -1)
{
en->front = 0;
en->rear = 0;
en->arr[en->front] = x;
}
else if(en->front==0)
{
en->front=4;
en->arr[(en->front)] = x;
}
else
{
en->front = (en->front - 1) % max;
en->arr[(en->front)] = x;
}
}
void enquerear(struct queue *en, int x)
{
if ((en->rear + 1) % max == en->front)
{
printf("the queue is full\n");
return;
}
else if (en->front == -1 && en->rear == -1)
{
en->front = 0;
en->rear = 0;
en->arr[en->rear] = x;
}
else
{
en->rear = (en->rear + 1) % max;
en->arr[(en->rear)] = x;
}
}
int dequefront(struct queue *en)
{
int temp;
if (en->front < 0)
{
printf("the queue is empty\n");
return -1;
}
else if (en->rear == en->front)
{
temp = en->arr[en->front];
en->front = en->rear = -1;
return temp;
}
else
{
temp = en->arr[en->front];
en->front = (en->front + 1) % max;
return temp;
}
}
int dequerear(struct queue *en)
{
int temp;
if (en->front < 0)
{
printf("the queue is empty\n");
return -1;
}
else if (en->rear == en->front)
{
temp = en->arr[en->front];
en->front = en->rear = -1;
return temp;
}
else if(en->rear==0)
{
temp = en->arr[en->rear];
en->rear=4;
return temp;
}
else
{
temp = en->arr[en->rear];
en->rear = (en->rear - 1) % max;
return temp;
}
}
void display(struct queue *q)
{
if (q->front < 0)
{
printf("the queue is empty\n");
}
else
{
int i = q->front;
printf("the elements in queue are\n");
while (i != q->rear)
{
printf("%d", q->arr[i]);
i = (i + 1) % max;
}
printf("%d", q->arr[i]);
}
}
int main()
{
struct queue s;
s.front = -1;
s.rear = -1;
int choice, n;
do
{
printf("enter your choice\n 1 FOR enqueuerear\n 2 FOR dequeuefront \n 3 FOR enqueuefront\n 4 FOR dequeuerear \n 5 FOR DISPLAY\n 0 FOR EXIT\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("enter the no. you want to push\n ");
scanf("%d", &n);
enquerear(&s, n);
break;
case 2:
n = dequefront(&s);
printf("the deleted no. is %d\n", n);
break;
case 3:
printf("enter the no. you want to push\n ");
scanf("%d", &n);
enquefront(&s, n);
break;
case 4:
n = dequerear(&s);
printf("the deleted no. is %d\n", n);
break;
case 5:
display(&s);
break;
}
} while (choice);
return 0;
}