-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathqueue_using_array.cpp
More file actions
74 lines (64 loc) · 1.6 KB
/
queue_using_array.cpp
File metadata and controls
74 lines (64 loc) · 1.6 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
72
73
74
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Queue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
Queue() {
front = rear = -1;
}
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
cout << "Queue is full. Cannot enqueue more elements." << endl;
return;
}
if (front == -1)
front = 0;
arr[++rear] = value;
cout << value << " has been enqueued." << endl;
}
void dequeue() {
if (front == -1) {
cout << "Queue is empty. Cannot dequeue from an empty queue." << endl;
return;
}
cout << arr[front] << " has been dequeued." << endl;
if (front == rear) {
front = rear = -1;
} else {
front++;
}
}
bool isEmpty() {
return front == -1;
}
};
int main() {
Queue queue;
int choice, value;
while (true) {
cout << "Queue Operations:" << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter a value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
exit(0);
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}