-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_Circular_Array.c
More file actions
39 lines (33 loc) · 952 Bytes
/
Copy pathQueue_Circular_Array.c
File metadata and controls
39 lines (33 loc) · 952 Bytes
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
#include <stdio.h>
#define MAX 20
int queue[MAX];
int front = -1, rear = -1;
int isEmpty() { return front == -1; }
int isFull() { return (rear + 1) % MAX == front; }
void enqueue(int x) {
if (isFull()) return;
if (isEmpty()) front = rear = 0;
else rear = (rear + 1) % MAX;
queue[rear] = x;
}
void dequeue() {
if (isEmpty()) return;
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
}
int Front() { return isEmpty() ? -1 : queue[front]; }
int Rear() { return isEmpty() ? -1 : queue[rear]; }
int main() {
int ch, x;
while (1) {
scanf("%d", &ch);
if (ch == 1) { scanf("%d", &x); enqueue(x); }
else if (ch == 2) dequeue();
else if (ch == 3) printf("%d\n", isEmpty());
else if (ch == 4) printf("%d\n", isFull());
else if (ch == 5) printf("%d\n", Front());
else if (ch == 6) printf("%d\n", Rear());
else break;
}
return 0;
}