-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWithTwoStack.cpp
More file actions
71 lines (61 loc) · 1.16 KB
/
QueueWithTwoStack.cpp
File metadata and controls
71 lines (61 loc) · 1.16 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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
template< typename _Type>
class squeue{
public:
void push_back(_Type val) {
if( push_st.size() != 0) {
push_st.push(val);
}else {
while( pop_st.size() != 0) {
push_st.push(pop_st.top());
pop_st.pop();
}
push_st.push(val);
}
}
void pop_front() {
if( pop_st.size() != 0) {
pop_st.pop();
}else {
while( push_st.size() != 0) {
pop_st.push(push_st.top());
push_st.pop();
}
pop_st.pop();
}
}
_Type first() {
_Type ans;
if( pop_st.size() != 0) {
ans = pop_st.top();
}else {
while( push_st.size() != 0) {
pop_st.push(push_st.top());
push_st.pop();
}
ans = pop_st.top();
}
return ans;
}
private:
stack<_Type> push_st;
stack<_Type> pop_st;
};
int main(void) {
squeue<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
cout << q.first() << " ";
q.pop_front();
q.pop_front();
q.push_back(4);
cout << q.first() << " ";
q.pop_front();
cout << q.first() << " ";
cout << endl;
return 0;
}