-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack&queue_functions.cpp
70 lines (55 loc) · 1.24 KB
/
Stack&queue_functions.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
#include <iostream>
#include <stack>
#include <queue>
std::stack<int> reverse_stack(std::stack<int> s) {
std::stack<int> reversed_s;
int tmp;
while (!s.empty()) {
tmp = s.top();
s.pop();
reversed_s.push(tmp);
}
return reversed_s;
}
std::queue<int> reverse_queue(std::queue<int> q) {
std::queue<int> reversed_q;
std::stack<int> s;
while (!q.empty()) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
reversed_q.push(s.top());
s.pop();
}
return reversed_q;
}
void print_stack(std::string name, std::stack<int> s) {
std::cout << "stack " << name << ": ";
while (!s.empty()) {
std::cout << s.top() << " ";
s.pop();
}
std::cout << std::endl;
}
void print_queue(std::string name, std::queue<int> q) {
std::cout << "queue " << name << ": ";
while (!q.empty()) {
std::cout << q.front() << " ";
q.pop();
}
std::cout << std::endl;
}
int main() {
std::stack<int> s, rs;
std::queue<int> q, rq;
s.push(1); s.push(2); s.push(3); s.push(4); s.push(5);
print_stack("s",s);
rs = reverse_stack(s);
print_stack("reversed s",rs);
q.push(1); q.push(2); q.push(3); q.push(4); q.push(5);
print_queue("q",q);
rq = reverse_queue(q);
print_queue("reversed q",rq);
return 0;
}