STL除了给我们提供了一些容器(container)以外,还给我们提供了几个容器适配器(container adapters),stack便是其中之一

看过STL源码的人都知道,stack其实是内部封装了 deque给我们使用,所有的操作,在内部都是基于deque的实现,

在<stack> 中,class stack的定义:

unamespace std{

template <class T, class container = deque<T> >

class stack;

}

所以我们也可以自己定义它内部的容器(但是你通常不会这样做如果你没有看过源代码):

std::stack<int, std::vector<int> > st;

stack的接口很简单,就那么几个:

push();

pop();//不返回最后一个值

top();//返回最后一个值

empty();

size();

看我从网上找的一个竞赛题:

Description

Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this. 

The following commands need to be supported: 

BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored. 

FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored. 

VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied. 

QUIT: Quit the browser. 

Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input

Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output

For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input

  1. VISIT http://acm.ashland.edu/
  2. VISIT http://acm.baylor.edu/acmicpc/
  3. BACK
  4. BACK
  5. BACK
  6. FORWARD
  7. VISIT http://www.ibm.com/
  8. BACK
  9. BACK
  10. FORWARD
  11. FORWARD
  12. FORWARD
  13. QUIT

Sample Output

  1. http://acm.ashland.edu/
  2. http://acm.baylor.edu/acmicpc/
  3. http://acm.ashland.edu/
  4. http://www.acm.org/
  5. Ignored
  6. http://acm.ashland.edu/
  7. http://www.ibm.com/
  8. http://acm.ashland.edu/
  9. http://www.acm.org/
  10. http://acm.ashland.edu/
  11. http://www.ibm.com/
  12. Ignored

这里给出我自己的实现:

  1. #include <iostream>
  2. #include <stack>
  3. #include <string.h>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7. typedef enum COMMAND
  8. {
  9. VISIT,BACK,FORWARD,QUIT, END
  10. }COMMAND;
  11.  
  12. const char *cmd_list[] =
  13. {
  14. "VISIT", "BACK", "FORWARD","QUIT"
  15. };
  16.  
  17. int find(char *cmd)
  18. {
  19. for(int i = 0; i < END; i++)
  20. if(!strcmp(cmd_list[i], cmd))
  21. return i;
  22. return -1;
  23. }
  24. void prasecmd(const char *s, char *cmd, char *arg)
  25. {
  26. int i = 0;
  27. const char *p = s;
  28. while(*p != ' ' && *p != '\0')
  29. {
  30. p++;
  31. i++;
  32. }
  33. if(*p == '\0')
  34. {
  35. strcpy(cmd, s);
  36. return ;
  37. }
  38. strncpy(cmd, s, i);
  39. strncpy(arg, p+1, strlen(s)-i);
  40.  
  41. }
  42. int main()
  43. {
  44. stack<string> ss;
  45. ss.push("http://www.acm.org");
  46. stack<string> stemp;
  47. string s;
  48. char cmd[10], arg[20];
  49. int Cmd;
  50. while(getline(cin,s))
  51. {
  52. bzero(cmd,sizeof(cmd));
  53. bzero(arg, sizeof(arg));
  54. prasecmd(s.c_str(), cmd, arg);
  55. Cmd = find(cmd);
  56. if(Cmd == -1) break;
  57. switch(Cmd)
  58. {
  59. case VISIT:
  60. {
  61. if(*arg != '\0')
  62. {
  63. ss.push(arg);
  64. cout<<arg<<endl;
  65. break;
  66. }
  67. else
  68. return 0;
  69. }
  70.  
  71. case BACK:
  72. {
  73. stemp.push(ss.top());
  74. ss.pop();
  75. if(ss.empty())
  76. {
  77. cout<<"Ignored"<<endl;
  78. ss.push(stemp.top());
  79. stemp.pop();
  80. break;
  81. }
  82. cout<<ss.top()<<endl;
  83. break;
  84. }
  85. case FORWARD:
  86. {
  87. if(!stemp.empty())
  88. {
  89. ss.push(stemp.top());
  90. stemp.pop();
  91. cout<<ss.top()<<endl;
  92. break;
  93. }
  94. else
  95. {
  96. cout<<"Ignored"<<endl;
  97. break;
  98. }
  99. }
  100. case QUIT:cout<<"Ignored"<<endl;return 0;;
  101. }
  102.  
  103. }
  104.  
  105. return 0;
  106. }

自己实现的代码是有问题的:

1.当向前和向后的过程又重新VISIT了,对于这个问题,该程序是错误的

2.c和c++乱套了,代码风格不好

看了一个人写的,感觉还是不错的

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string current = "http://www.acm.org";
  8. string text;
  9. stack<string> backward;
  10. stack<string> forward;
  11. forward.push(current);
  12. while(getline(cin, text))
  13. {
  14. if(text == "QUIT") break;
  15. else if(text == "BACK")
  16. {
  17. if(backward.empty()) cout<<"Ignored"<<endl;
  18. else
  19. {
  20. forward.push(current);
  21. current = backward.top();
  22. backward.pop();
  23. cout<<current<<endl;
  24. }
  25. }
  26. else if(text == "FORWARD")
  27. {
  28. if(forward.empty()) cout<<"Ignored"<<endl;
  29. else
  30. {
  31. backward.push(current);
  32. current = forward.top();
  33. forward.pop();
  34. cout<<current<<endl;
  35. }
  36. }
  37. else
  38. {
  39. while(!forward.empty()) forward.pop();
  40. backward.push(current);
  41. current = text.substr(6);
  42. cout<<current<<endl;
  43. }
  44. }
  45. return 0;
  46. }

使用一个current作为当前的URL,然后前后访问两个不同的stack,代码风格也比较不错,另外二叉树的后续遍历也使用了双栈法

stack的应用的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  4. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  5. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  6. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Stack的三种含义

    作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...

  10. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

随机推荐

  1. windows批处理(.bat)

    转自http://www.cnblogs.com/shiney/archive/2011/07/04/2097236.html 本文在运行中有一些小小的问题,我修改了一下,将会在稳重标出 批处理文件是 ...

  2. Fsu0413's Qt builds

    http://fsu0413.github.io/QtCompile/#!index.md

  3. Python学习笔记1-搭建Python环境 和 Python Hello World!

    一.搭建Python开发环境 1.选择开发工具 首先要寻找一个Python的开发工具,Python的开发工具有很多,有pyCharm .Eclipse.Visual studio等等 ,使用最多的还是 ...

  4. MFC网页

    写网页, 选择MFC,MFC应用程序,其他默认,单击确定 项目类型,选Offce,其他默认,单击下一步 默认,单击下一步 文件拓展名,输入html,其他默认,单击下一步 数据库支持,默认,单击下一步 ...

  5. 怎样安装pip--python的包管理工具

    pip是python的包管理工具,使用它来安装python的模块很方便. pip支持的操作系统:Unix/Linux, OS X, and Windows. 支持的python版本号:2.6, 2.7 ...

  6. 从汇编来看i++与++i

    故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时由于要去上课没给他说,正好今晚有空就測试了一下例如以下代码: 编译环境:VS2010  语言:C++ #include <io ...

  7. swipe方法

    /** * @author zhousg * @Date 2016-02-04 * @Method 滑动方法 针对一个大容器内部的容器做滑动封装 * @param * args args.swipeD ...

  8. javascript高级知识点——继承

    代码信息来自于http://ejohn.org/apps/learn/. 继承是如何工作的 function Person(){} function Ninja(){} Ninja.prototype ...

  9. PHP自学1——简单表单提交

    最近自学PHP,顺便做个笔记记录一下自己的学习进度.选用的教程是<PHP and MySQL Web Development 4th Edition>,建议阅读英文教材(我能说英文网上免费 ...

  10. HBASE学习笔记--API

    HBaseConfiguration HBaseConfiguration是每一个hbase client都会使用到的对象,它代表的是HBase配置信息.它有两种构造方式: public HBaseC ...