-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion2.cpp
114 lines (91 loc) · 2.27 KB
/
question2.cpp
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
#include<iostream>
using namespace std;
class Queue {
private:
int front, rear, maxSize;
int* arr;
public:
Queue(int size) {
maxSize = size;
arr = new int[maxSize];
front = rear = -1;
}
~Queue() {
delete[] arr;
}
bool isFull() {
return (rear == maxSize - 1);
}
bool isEmpty() {
return (front == -1 || front > rear);
}
void insert(int item) {
if (isFull()) {
cout << "Queue is full. Cannot insert." << endl;
return;
}
if (front == -1) {
front = 0;
}
arr[++rear] = item;
cout << "Element " << item << " inserted into the Queue." << endl;
}
void remove() {
if (isEmpty()) {
cout << "Queue is empty. Cannot delete." << endl;
return;
}
int deletedItem = arr[front++];
cout << "Element " << deletedItem << " removed from the Queue." << endl;
if (front > rear) {
front = rear = -1;
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
int maxSize;
cout << "Enter the size of the Queue: ";
cin >> maxSize;
Queue queue(maxSize);
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Insert\n";
cout << "2. Delete\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int item;
cout << "Enter the element to insert: ";
cin >> item;
queue.insert(item);
break;
case 2:
queue.remove();
break;
case 3:
queue.display();
break;
case 4:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 4);
return 0;
}