-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL082.cpp
67 lines (63 loc) · 1.08 KB
/
SCTDL082.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
/**
* @file SCTDL082.cpp
* @author long ([email protected])
* @brief Reverse stack using recursion algorithm
* @version 0.1
* @date 2023-04-02
*
* @copyright Copyright (c) 2023
*
*/
#include <iostream>
#include <stack>
using namespace std;
void insert_at_bottom(stack<int>& st, int x)
{
if (st.size() == 0) {
st.push(x);
}
else {
int a = st.top();
st.pop();
insert_at_bottom(st, x);
st.push(a);
}
}
void reverse(stack<int>& st)
{
if (st.size() > 0) {
int x = st.top();
st.pop();
reverse(st);
insert_at_bottom(st, x);
}
return;
}
int main()
{
int t;
cin >> t;
while(t--) {
int n;
stack<int> st, st2;
cin >> n;
for (int i = 1; i <= n; i++) {
int tmp;
cin >> tmp;
st.push(tmp);
}
st2 = st;
while (!st2.empty()) {
cout << st2.top() << " ";
st2.pop();
}
cout<<endl;
reverse(st);
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}
return 0;
}