-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-queueBystack.cpp
74 lines (64 loc) · 1.22 KB
/
06-queueBystack.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
/********************************
*@file:
*@author: Pan HU
*@data: 2015-8-16
*@version: 0.1
*@describe:
********************************/
#include <iostream>
#include <stack>
#include <stdexcept>
using namespace std;
//入队的数据全部压入到栈1中
//出队时,如果栈2不为空则弹出栈2的栈顶元素,如果栈2为空则将栈1全部出栈到栈2
class queueBystack
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty())
{
if(stack1.empty())
{
throw runtime_error("pop failed queue is empty!");
}
while(stack1.size() != 0)
{
stack2.push(stack1.top());
stack1.pop();
}
}
int tmp = stack2.top();
stack2.pop();
return tmp;
}
private:
stack<int> stack1;
stack<int> stack2;
};
int main()
{
int arr[] = {1,2,3,4,5,6,7};
queueBystack queue;
queue.push(-1);
try{
cout<<queue.pop()<<endl;
} catch (const runtime_error &e){
cerr<<e.what()<<endl;
}
for(int i = 0; i < sizeof(arr)/sizeof(*arr); ++i)
{
queue.push(arr[i]);
}
for(int i = 0; i < sizeof(arr)/sizeof(*arr); ++i)
{
try{
cout<<queue.pop()<<endl;
} catch (const runtime_error &e){
cerr<<e.what()<<endl;
}
}
return 0;
}