-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearqueue.c
More file actions
71 lines (56 loc) · 1.36 KB
/
linearqueue.c
File metadata and controls
71 lines (56 loc) · 1.36 KB
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
#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
}
}
void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
} else {
printf("Dequeued: %d\n", queue[front]);
if (front == rear)
front = rear = -1;
else
front++;
}
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter value: ");
scanf("%d", &value);
enqueue(value);
} else if (choice == 2) {
dequeue();
} else if (choice == 3) {
display();
} else if (choice == 4) {
printf("Exiting...\n");
break;
} else {
printf("Invalid choice\n");
}
}
return 0;
}