-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path250.QueueUsing2STLStacksC++ (1).txt
64 lines (54 loc) · 1.22 KB
/
250.QueueUsing2STLStacksC++ (1).txt
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
#include <iostream>
#include <stack>
using namespace std;
class Queue{
private:
stack<int> e_stk;
stack<int> d_stk;
public:
Queue(){};
~Queue(){};
void enqueue(int x);
int dequeue();
};
void Queue::enqueue(int x) {
e_stk.push(x);
}
int Queue::dequeue() {
int x = -1;
if (d_stk.empty()){
if (e_stk.empty()){
cout << "Queue Underflow" << endl;
return x;
} else {
while (!e_stk.empty()){
d_stk.push(e_stk.top());
e_stk.pop();
}
}
}
x = d_stk.top();
d_stk.pop();
return x;
}
int main() {
int A[] = {1, 3, 5, 7, 9};
Queue q;
cout << "Enqueue: " << flush;
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
q.enqueue(A[i]);
cout << A[i] << flush;
if (i < sizeof(A)/sizeof(A[0])-1){
cout << " <- " << flush;
}
}
cout << endl;
cout << "Dequeue: " << flush;
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
cout << q.dequeue() << flush;
if (i < sizeof(A)/sizeof(A[0])-1){
cout << " <- " << flush;
}
}
return 0;
}