【栈】POJ 1028 Web Navigation

本文探讨了80%的程序员难以成为架构师的原因,通过模拟浏览器操作,利用栈结构实现前进和后退功能,深入解析了编程与架构之间的关键区别。

模拟题目所述的浏览器的各种操作,使用一个curWebsite变量来保存当前的Page。

/*
Problem: 1028		User: DragonKnight
Memory: 712K		Time: 79MS
Language: G++		Result: Accepted
*/
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
    string command, website;
    stack<string> back,forward;//前进栈,后退栈
    string curWebsite = "http://www.acm.org/";//初始载入的页面

    while(cin >> command)
    {
        if(command == "QUIT")
        {
            break;
        }
        else if(command == "VISIT")
        {
            cin >> website;
            back.push(curWebsite);
            curWebsite = website;
            cout << curWebsite << endl;
            while(!forward.empty()) forward.pop();
        }
        else if(command == "BACK")
        {

            if(!back.empty())
            {
                forward.push(curWebsite);
                curWebsite = back.top();
                back.pop();
                cout << curWebsite << endl;
            }
            else
            {
                cout << "Ignored" << endl;
            }
        }
        else if(command == "FORWARD")
        {
            if(!forward.empty())
            {
                back.push(curWebsite);
                curWebsite = forward.top();
                forward.pop();
                cout << curWebsite << endl;
            }
            else
            {
                cout << "Ignored" << endl;
            }
        }
    }
    return 0;
}

转载于:https://my.oschina.net/lee24/blog/74471

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值