-
Notifications
You must be signed in to change notification settings - Fork 18
/
Queue_Array-Imp.cpp
149 lines (131 loc) · 2.78 KB
/
Queue_Array-Imp.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#define SIZE 150
using namespace std;
int queue[SIZE];
int front = -1;
int rear = -1;
bool IsEmpty(){
return front == -1;
}
bool IsFull(){
return (rear + 1) % SIZE == front;
}
void enqueue(int val){
if (IsFull()){
cout << "Queue overflow!" << endl;
return;
}
else{
if (IsEmpty()){
front = rear = 0;
}
else
rear++;
queue[rear] = val;
cout << val << " enqueued!" << endl;
}
}
void dequeue(){
if (IsEmpty()){
cout << "Queue underflow!" << endl;
return;
}
else{
cout << queue[front] << " dequeued!" << endl;
if (front == rear)
rear = front = -1;
else
front++;
}
}
void FrontElement(){
if (IsEmpty()){
cout << "Queue underflow!" << endl;
}
else{
cout << "Front: " << queue[front] << endl;
}
}
void RearElement(){
if (IsEmpty()){
cout << "Queue underflow!" << endl;
}
else{
cout << "Rear: " << queue[rear] << endl;
}
}
void QueueSize(){
int length;
if ((rear == -1) && (front == -1)){
length = 0;
}
else{
length = (rear + SIZE - front) % SIZE + 1;
}
cout << "Size: " << length << endl;
}
void display(){
cout << "Queue: ";
if ((rear == -1) && (front == -1)){
cout << "NULL";
}
else{
int length = (rear + SIZE - front) % SIZE + 1;
for (int i = 0; i < length; i++){
cout << queue[(front + i) % SIZE] << " ";
}
}
cout << endl;
}
int main(){
int q = 1;
while(q != 7){
cout << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Front" << endl;
cout << "4. Rear" << endl;
cout << "5. Size" << endl;
cout << "6. Display" << endl;
cout << "7. Exit" << endl;
cout << "Enter your query: ";
cin >> q;
switch(q){
case 1:{
int val;
cout << "Enter the value: ";
cin >> val;
enqueue(val);
break;
}
case 2:{
dequeue();
break;
}
case 3:{
FrontElement();
break;
}
case 4:{
RearElement();
break;
}
case 5:{
QueueSize();
break;
}
case 6:{
display();
break;
}
case 7:{
exit(0);
break;
}
default:{
cout << "Enter a valid operation!" << endl;
}
}
}
return 0;
}