-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueUsingTwoStack.cpp
72 lines (56 loc) · 1.02 KB
/
queueUsingTwoStack.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
#include<iostream>
//add stack std
#include<stack>
using namespace std;
// create Queue ADT
class queue {
//define two stacks
stack<int> s1;
stack<int> s2;
public:
//add elements in queue
void enQueue(int data) {
this->s1.push(data);
}
//remove elements from queue
int deQueue() {
this->shiftStackValues();
int value = this->s2.top();
this->s2.pop();
return value;
}
//check if list is empty or not
bool isEmpty() {
this->shiftStackValues();
return this->s2.empty();
}
//get the front value of queue
int front() {
this->shiftStackValues();
return this->s2.top();
}
//keep in sync s2 with s1
void shiftStackValues() {
while (!this->s1.empty()) {
this->s2.push(this->s1.top());
this->s1.pop();
}
}
};
int main() {
//create a queue object
queue Q;
//add some elements on it
Q.enQueue(1);
Q.enQueue(2);
Q.enQueue(3);
Q.enQueue(4);
Q.enQueue(5);
Q.enQueue(6);
Q.enQueue(7);
cout<< Q.front();
//display queue elements
while(!Q.isEmpty())
cout << Q.deQueue();
return 0;
}