forked from JiauZhang/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_with_two_stack.cpp
143 lines (125 loc) · 2.83 KB
/
queue_with_two_stack.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
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
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stack>
using namespace std;
template <typename T>
class CQueue {
public:
CQueue();
~CQueue();
void appendTail(const T& value);
T& deleteHead();
void printQueue();
private:
stack<T> stack1;
stack<T> stack2;
};
template <typename T>
CQueue<T>::CQueue()
{
while(!stack1.empty())
stack1.pop();
while(!stack2.empty())
stack2.pop();
}
template <typename T>
CQueue<T>::~CQueue()
{
while(!stack1.empty())
stack1.pop();
while(!stack2.empty())
stack2.pop();
}
template <typename T>
void CQueue<T>::appendTail(const T& value)
{
stack1.push(value);
}
template <typename T>
T& CQueue<T>::deleteHead()
{
if(!stack2.empty()) {
// top 返回的是栈顶的引用
T& res = stack2.top();
stack2.pop();
return res;
} else if(!stack1.empty()) {
while(!stack1.empty()) {
T& res = stack1.top();
stack2.push(res);
stack1.pop();
}
T& res = stack2.top();
stack2.pop();
return res;
}
}
template <typename T>
void CQueue<T>::printQueue()
{
if(!stack2.empty()) {
T& ref = stack2.top();
size_t stack_size = stack2.size();
T* top = &ref;
while(stack_size) {
cout << *top << ' ';
stack_size--;
//windows中的栈是从低地址向高地址增长的
top--;
}
}
if(!stack1.empty()) {
size_t stack_size = stack1.size();
T& ref = stack1.top();
//引用可以看作是别名,对某一内存的直接使用,通过&提取地址
T* bottom = &ref;
//栈低需要在通过栈顶指针减去(stack_size-1)
bottom = bottom - stack_size + 1;
while(stack_size) {
stack_size--;
cout << *(bottom++) << ' ';
//cout << bottom++ << ' ';
}
}
cout << endl;
}
int main(int argc, char** argv)
{
CQueue<int> queue;
queue.appendTail(1);
queue.appendTail(2);
queue.appendTail(3);
queue.appendTail(4);
queue.appendTail(5);
queue.deleteHead();
queue.deleteHead();
queue.printQueue();
queue.appendTail(6);
queue.appendTail(7);
queue.appendTail(8);
queue.appendTail(9);
queue.printQueue();
queue.deleteHead();
queue.printQueue();
queue.deleteHead();
queue.deleteHead();
queue.deleteHead();
queue.deleteHead();
queue.printQueue();
return 0;
}