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

VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

Sample Output

http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored

这里给出我自己的实现:

#include <iostream>
#include <stack>
#include <string.h>
#include <stdio.h>
using namespace std; typedef enum COMMAND
{
VISIT,BACK,FORWARD,QUIT, END
}COMMAND; const char *cmd_list[] =
{
"VISIT", "BACK", "FORWARD","QUIT"
}; int find(char *cmd)
{
for(int i = 0; i < END; i++)
if(!strcmp(cmd_list[i], cmd))
return i;
return -1;
}
void prasecmd(const char *s, char *cmd, char *arg)
{
int i = 0;
const char *p = s;
while(*p != ' ' && *p != '\0')
{
p++;
i++;
}
if(*p == '\0')
{
strcpy(cmd, s);
return ;
}
strncpy(cmd, s, i);
strncpy(arg, p+1, strlen(s)-i); }
int main()
{
stack<string> ss;
ss.push("http://www.acm.org");
stack<string> stemp;
string s;
char cmd[10], arg[20];
int Cmd;
while(getline(cin,s))
{
bzero(cmd,sizeof(cmd));
bzero(arg, sizeof(arg));
prasecmd(s.c_str(), cmd, arg);
Cmd = find(cmd);
if(Cmd == -1) break;
switch(Cmd)
{
case VISIT:
{
if(*arg != '\0')
{
ss.push(arg);
cout<<arg<<endl;
break;
}
else
return 0;
} case BACK:
{
stemp.push(ss.top());
ss.pop();
if(ss.empty())
{
cout<<"Ignored"<<endl;
ss.push(stemp.top());
stemp.pop();
break;
}
cout<<ss.top()<<endl;
break;
}
case FORWARD:
{
if(!stemp.empty())
{
ss.push(stemp.top());
stemp.pop();
cout<<ss.top()<<endl;
break;
}
else
{
cout<<"Ignored"<<endl;
break;
}
}
case QUIT:cout<<"Ignored"<<endl;return 0;;
} } return 0;
}

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

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

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

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

#include <iostream>
#include <stack>
using namespace std; int main()
{
string current = "http://www.acm.org";
string text;
stack<string> backward;
stack<string> forward;
forward.push(current);
while(getline(cin, text))
{
if(text == "QUIT") break;
else if(text == "BACK")
{
if(backward.empty()) cout<<"Ignored"<<endl;
else
{
forward.push(current);
current = backward.top();
backward.pop();
cout<<current<<endl;
}
}
else if(text == "FORWARD")
{
if(forward.empty()) cout<<"Ignored"<<endl;
else
{
backward.push(current);
current = forward.top();
forward.pop();
cout<<current<<endl;
}
}
else
{
while(!forward.empty()) forward.pop();
backward.push(current);
current = text.substr(6);
cout<<current<<endl;
}
}
return 0;
}

使用一个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. jquery实现带左右箭头和数字焦点的图片轮播手写代码

    以前图片轮播经常用网上的插件,然后想说自己也要认真看看,一步一步弄明白,所以就自己研究写了个图片轮播的代码,我自己觉得还算是挺简单的.如有改进的地方,欢迎你能帮我指出,共同进步. (ps:博客园如何上 ...

  2. js查找元素

    1.className <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...

  3. Flink Program Guide (3) -- Event Time (DataStream API编程指导 -- For Java)

    Event Time 本文翻译自DataStream API Docs v1.2的Event Time ------------------------------------------------ ...

  4. MYSQL 二进制还原

    解决方法: mysqlbinlog bin_log_file_path_and_name | mysql -uroot -p 如: mysqlbinlog E:\DB\mysql_log\mysql_ ...

  5. WPF学习:绑定

    原文 http://www.cnblogs.com/SouthAurora/archive/2010/06/30/1768464.html 一.绑定到元素对象 1.元素和元素(XAML.代码) 1.1 ...

  6. jdk1.6,jdk1.7共存

    当然可以,安装的时候记得选择不同的安装目录,安装好以后,可以在开发工具(如eclipse)中切换不同的编译环境和运行环境.其实只要安装eclipse就自带了jdk1.3-1.6的编译环境了. Mac下 ...

  7. sparse autoencoder

    1.autoencoder autoencoder的目标是通过学习函数,获得其隐藏层作为学习到的新特征. 从L1到L2的过程成为解构,从L2到L3的过程称为重构. 每一层的输出使用sigmoid方法, ...

  8. [11-1] adaboost DTree

    main idea:用与$u_t$成正比的概率sampling生成的数据集$\widetilde{D}$训练DTree:用整个数据集$D$计算weighted$\epsilon_n$,计算$g_t$的 ...

  9. linux GUI程序开发

    1,C++ OOP中 class与C 面向过程开发中struct非常相似

  10. KMP模式匹配 三(弦)

    原文请訪问我的博客:xiaoshig.sinaapp.com KMP模式匹配 三(串) Time Limit:1000MS     Memory Limit:131072KB     64bit IO ...