POJ 1028

/*2018.8.28下午4点-2018.8.29 下午13:58
这题主要考察栈,本来用数组也能做,但是可能比较麻烦,加上我想练习一下栈
就用栈去写了
栈主要有这些操作
size、pop、push、top 
就这些够用了 
用两个栈去维护,当back或者forward的时候将一个栈里面的顶端给移到另一个栈的顶端
注意几个细节
1.back的那个栈最底层至少要有一个元素
2.forward在的那个栈是可以一个元素都没有的
3.考虑到现实意义,back的那个栈应该先pop再top输出而forward在的那个栈应该先top输出再pop 
   当然其实是一样的,我们可以考虑是全在back栈里面输出,就是forward先移到back然后再top输出
4.每次visit的时候,forward要清空。
*/ 
#include<stdio.h>
#include<string.h>
#include<stack>
#include <iostream>
using namespace std;
char p[100];
char a[100][100];
char v[100];
char back[100];
char forward[100];
char quit[100];
int main()
{
    stack <string> b,f;
    b.push("http://www.acm.org/");
    int i=0;
    while (1)
    {
        scanf("%s",p);
        if (!strcmp(p,"VISIT"))
        {
            while (f.size()!=0)
            f.pop();
            i++;
            scanf("%s",a[i]);
            printf("%s\n",a[i]);
            b.push(a[i]);
            continue;
        }
        if (!strcmp(p,"BACK"))
        {
            if (b.size()==1)
            {                
                printf("Ignored\n");
                continue;
            }
            f.push(b.top());
            b.pop();    
            cout<<b.top()<<endl;
            i--;            
        }    
        if (!strcmp(p,"FORWARD"))
        {

            if (f.size()==0)
            {
                printf("Ignored\n");
                continue;
            }
            cout<<f.top()<<endl;            
            b.push(f.top());
            f.pop();
            i++;        
        }
        
        if (!strcmp(p,"QUIT"))
        {
            return 0;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值