stack的应用
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
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
Output
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的应用的更多相关文章
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Stack的三种含义
作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
随机推荐
- jQuery 源码分析和使用心得 - 关于源码
说到jQuery, 大家可能直觉的认为jQuery的源码应该就是一个jquery.xx.js这样的一个文件. 但是看到真正的源码的时候, 整个人都思密达了.jQuery的源码做的事远比你想象的多, 为 ...
- 初学Django
纵然有众多大牛写过这些简单入门文章,但作为记录,还是要自己动手写下来的比较靠谱,‘好脑筋不如烂笔头’啊! Python 安装 Django本身是纯Python编写的,所以安装框架的第一步是确保你已经安 ...
- jQuery二级联动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C语言基础03
1.随机数 :一个范围内随机数字的返回值. 格式为: arc4random() % ( num大值 -num小值 + 1 ) + num小值. int n,i= 0; //控制随机 ...
- org.springframework.context.event.AbstractApplicationEventMulticaster
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType e ...
- overload的一点思考
仅参数类型不同的重载方法,使用过程的一个困惑: 有没有必要使用instanceof方法? package overload.special; public class OverLoadTest { p ...
- Learn X in Y minutes(python一页纸代码)
一篇非常好的文章,解释了python基本语法的方方面面: # Single line comments start with a hash. """ Multiline ...
- redis.conf配置详解
http://www.2cto.com/database/201307/225113.html
- 顺便说一下$.each()函数:
$.each()函数不同于JQuery对象的each()方法,它是一个全局函数,不操作JQuery对象,而是以一个数组或者对象作为第1个参数,以一个回调函数作为第2个参数.回调函数拥有两个参数:第1个 ...
- iOS中通知传值
NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面. 思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...