-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue using array
102 lines (89 loc) · 2.36 KB
/
queue using array
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
#include <iostream>
#include <vector>
using namespace std;
const int MAX_SIZE = 5;
void initializeQueue(int queue[], int& front, int& rear) {
front = rear = -1;
}
bool isEmpty(int front, int rear) {
return front == -1 || front > rear;
}
bool isFull(int rear) {
return rear == MAX_SIZE - 1;
}
void enqueue(int queue[], int& front, int& rear, int value) {
if (isFull(rear)) {
cout << "Overflow!" << endl;
} else {
if (front == -1) front = 0;
queue[++rear] = value;
cout << "Enqueued " << value << endl;
}
}
int dequeue(int queue[], int& front, int& rear) {
if (isEmpty(front, rear)) {
cout << "Underflow!" << endl;
return -1;
} else {
int dequeuedElement = queue[front++];
cout << "Dequeued " << dequeuedElement << endl;
return dequeuedElement;
}
}
int front(const int queue[], int front) {
if (isEmpty(front, front)) {
cout << "Empty!" << endl;
return -1;
} else {
return queue[front];
}
}
void display(const int queue[], int front, int rear) {
if (isEmpty(front, rear)) {
cout << "Empty!" << endl;
} else {
cout << "Queue elements:" << endl;
for (int i = front; i <= rear; ++i) {
cout << queue[i] << endl;
}
}
}
int main() {
int queue[MAX_SIZE];
int front, rear;
initializeQueue(queue, front, rear);
int choice;
do {
cout << "\nQueue Operations:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Front\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int value;
cout << "Enter value: ";
cin >> value;
enqueue(queue, front, rear, value);
break;
case 2:
dequeue(queue, front, rear);
break;
case 3:
cout << "Front: " << front(queue, front) << endl;
break;
case 4:
display(queue, front, rear);
break;
case 5:
cout << "Exiting.\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}