-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-18.cpp
More file actions
165 lines (161 loc) · 2.13 KB
/
7-18.cpp
File metadata and controls
165 lines (161 loc) · 2.13 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
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
//两个栈实现一个队列
template<class T>
class StoQueue
{
public:
//插入在s1中进行,删除在s2中进行
void Push(T x)
{
s1.push(x);
}
void Pop()
{
int val = 0;
if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
}
T Front()
{
if(s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
T val = s2.top();
return val;
}
T Back()
{
if(!s1.empty())
{
T val = s1.top();
return val;
}
if(!s2.empty())
{
while (!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
return s1.top();
}
}
size_t Size()
{
size_t sz1 = s1.size();
size_t sz2 = s2.size();
return sz1+sz2;
}
bool Empty()
{
return (s1.empty() && s2.empty());
}
private:
stack<T> s1;
stack<T> s2;
};
//两个队列实现一个栈
template<typename T>
class QtoStack
{
public:
void Push(T x)
{
q1.push(x);//将新插入的数据尾插到q1中---保证出栈顺序
}
void Pop()
{
//q1的数据插入到q2,当q1中的front和back执行同一个数时就不再向q2中插入
if(!q1.empty())
{
while (q1.front() != q1.back())
{
q2.push(q1.front()); //将前n-1个元素插入到q2中
q1.pop();
}
q1.pop();
}
else
{
if(!q2.empty())
while (q2.front() != q2.back())
{
q1.push(q2.front());
q2.pop();
}
q2.pop();
}
}
T Top()
{
if (!q1.empty())
{
while (!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
return q2.back();
}
else
{
if(!q2.empty())
return q2.back();
}
}
int Size()
{
return q1.size() + q2.size();
}
bool Empty()
{
return (q1.empty() && q2.empty());
}
private:
queue<T> q1;
queue<T> q2;
};
int main()
{
//StoQueue<int> q1;
//q1.Push(1);
//q1.Push(2);
//q1.Push(3);
//cout<<q1.Empty()<<endl;
//size_t sz = q1.Size();
//int val1 = q1.Front();
//int val2 = q1.Back();
//q1.Pop();
//q1.Pop();
//q1.Pop();
//cout<<q1.Empty()<<endl;
QtoStack<int> s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
size_t sz = s.Size();
int val = s.Top();
s.Pop();
s.Pop();
sz = s.Size();
val = s.Top();
s.Pop();
system("pause");
return 0;
}