http://www.geeksforgeeks.org/reverse-a-stack-using-recursion/

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. #include <map>
  9. #include <set>
  10. using namespace std;
  11.  
  12. void insertbottom(stack<int> &S, int top) {
  13. if (S.empty()) S.push(top);
  14. else {
  15. int tmp = S.top();
  16. S.pop();
  17. insertbottom(S, top);
  18. S.push(tmp);
  19. }
  20. }
  21.  
  22. void reversestack(stack<int> &S) {
  23. if (S.empty()) return;
  24. int top = S.top();
  25. S.pop();
  26. reversestack(S);
  27. insertbottom(S, top);
  28. }
  29.  
  30. int main() {
  31. stack<int> S;
  32. S.push();
  33. S.push();
  34. S.push();
  35. S.push();
  36. reversestack(S);
  37. while (!S.empty()) {
  38. cout << S.top() << endl;
  39. S.pop();
  40. }
  41. return ;
  42. }

Data Structure Stack: Reverse a stack using recursion的更多相关文章

  1. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  2. UVA 11995 I Can Guess the Data Structure!(ADT)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  3. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  4. uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)

    11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...

  5. UVA - 11995 - I Can Guess the Data Structure! STL 模拟

    There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...

  6. Kattis -I Can Guess the Data Structure!

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x1  ...

  7. [Algorithom] Stack Data Structure in JavaScript

    A stack is a collection of items that obeys the principle of "last in, first out". Like a ...

  8. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  9. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. Oracle LOB字段判空

    dbms_lob.getlength() dbms_lob.getlength(null) 会报错--- Oracle 默认为clob字段插入empty_clob()

  2. jdk1.6 升级到 jdk1.7

    将 jdk1.6 升级到 jdk1.7 下载jdk-7u11-linux-x64.tar.gz   tar zxvf  jdk-7u11-linux-x64.tar.gz mkdir   /usr/l ...

  3. 再议urlconnection和socket区别

    利用URL进行通信与利用socket进行通信有许多相似之处.它们都是利用建立连接.获取流来进行通信.那么,它们的区别在何处呢?    利用socket进行通信时,在服务器端运行一个socket通信程序 ...

  4. .Net Framework 与 SQL Server 2005 混乱的时间最大最小值

    Net Framewrok 中,DateTime.MinValue => 0001/01/01 00:00:00SqlDateTime.MinValue.Value  => 1753/01 ...

  5. JS 正则 钱

    function ValidateIsDecial(sValue) { return (!sValue && !!!sValue && /^[0-9]{1,10}(\. ...

  6. Foundation框架 - NSDictionary类、NSMutableDictionary类

    NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪 ...

  7. Java中synchronized用在静态方法和非静态方法上面的区别

    synchronized 修饰在 static方法和非static方法的区别   在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以sync ...

  8. CONTROLLER_NAME 常量 当前访问的控制器

    CONTROLLER_NAME 常量 当前访问的控制器

  9. ListView中加载大量的图片

    情况是这样的:我需要把大约四五十个车标在一个listView中展示出来,一般在用ListView的时候撑死十来个图标,按不同分类使用,这倒好办,在创建view的时候使用R.drawable.xxx指定 ...

  10. linux用一键安装包 禅道

    ----------------1.重启apache 和 mysql /opt/zbox/zbox restart -----------------2.问题1:发现端口被占用 apache启动报错( ...