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
| #include <iostream> #include <stack> using namespace std;
int getDownItem(stack<int>& s); void rStack(stack<int>& s);
void printStack(stack<int> s) { if (s.empty()){ return; } cout << s.top() << endl; s.pop(); printStack(s); }
void test() { stack<int> mys; mys.push(4); mys.push(3); mys.push(2); mys.push(1); printStack(mys);
cout << "逆序之后" << endl; rStack(mys); printStack(mys); }
int getDownItem(stack<int>& s) { int result = s.top(); s.pop(); if (s.empty()) { return result; } int last = getDownItem(s); s.push(result); return last; }
void rStack(stack<int>& s) { if (s.empty()) { return; } int ret = getDownItem(s); rStack(s); s.push(ret); }
int main() { test(); cout << "hello world!" << endl; system("pause"); return 0; }
|