-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleQueue.cpp
More file actions
169 lines (145 loc) · 2.99 KB
/
SimpleQueue.cpp
File metadata and controls
169 lines (145 loc) · 2.99 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Author: Hemant Tripathi
*/
#include <iostream>
using namespace std;
#define MAXQUEUESIZE 10
class Queue {
int front, rear;
public:
int myqueue[MAXQUEUESIZE];
Queue() {
front = 0;
rear = -1;
}
bool insertOp(int element);
bool removeOp();
int getFirstElement();
int getLastElement();
bool isEmpty();
bool isOverflow();
void showElements();
int totalCount();
};
bool Queue::insertOp(int element) {
if(isOverflow()) {
cout << "Queue is overflow. Cannot insert any element" << endl;
return false;
} else {
myqueue[++rear] = element;
cout << "Inserted element " << element << " into queue, at position "<< rear << endl;
return true;
}
}
bool Queue::removeOp() {
if(isEmpty()) {
cout << "Empty Queue" << endl;
return false;
} else {
int poppedElement = myqueue[front++];
cout << "Element " << poppedElement << " is popped from position << " << front << endl;
return true;
}
}
int Queue::getFirstElement() {
if(isEmpty()) {
cout << "Empty Queue" << endl;
return -1;
} else {
return myqueue[front];
}
}
int Queue::getLastElement() {
if(isEmpty()) {
cout << "Empty Queue" << endl;
return -1;
} else {
return myqueue[rear];
}
}
bool Queue::isEmpty() {
if(rear < front) {
return true;
} else {
return false;
}
}
bool Queue::isOverflow() {
if(rear < MAXQUEUESIZE-1) {
return false;
} else {
return true;
}
}
void Queue::showElements() {
if(isEmpty()) {
cout << "Empty Queue" << endl;
} else {
for(int i=front; i <= rear; i++) {
cout << "Element: " << myqueue[i] << "; Position: " << i << endl;
}
}
}
int Queue::totalCount() {
return rear - front + 1;
}
int main() {
class Queue queue;
bool exit = false;
cout << "########## Starting Simple Queue Operation #############" << endl;
while(1) {
if(exit) {
cout << "Aborting the program" << endl;
break;
}
cout << "Select an option:" << endl;
cout << "1. Insert an element" << endl;
cout << "2. Remove an element" << endl;
cout << "3. Get First Element of Queue" << endl;
cout << "4. Get Last Element of Queue" << endl;
cout << "5. Show All Element of Queue" << endl;
cout << "6. Exit"<<endl;
int choice = -1;
cin >> choice;
switch(choice) {
case 1:
int element;
cout << "Please enter a number to insert into the queue" << endl;
cin >> element;
queue.insertOp(element);
break;
case 2:
{
bool result = queue.removeOp();
if(result) {
cout << "Element removed from queue successfully!" << endl;
} else {
cout << "Cannot remove element from queue!" << endl;
}
}
break;
case 3:
{
int firstElement = queue.getFirstElement();
if(firstElement >= 0) {
cout << "First Element of Queue is: " << firstElement << endl;
}
}
break;
case 4:
{
int lastElement = queue.getLastElement();
if(lastElement >= 0) {
cout << "Last Element of Queue is: " << lastElement << endl;
}
}
break;
case 5:
queue.showElements();
break;
case 6:
exit = true;
break;
}
}
}