• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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

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,代码风格也比较不错,另外二叉树的后续遍历也使用了双栈法

 






 

posted @ 2013-09-15 19:45  Class Xman  阅读(214)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3