pos1028 栈

来源:互联网 发布:咖啡入门 知乎 编辑:程序博客网 时间:2024/05/16 19:10

这是很明显的用栈的操作,主要了解栈的主要用法。

可以设置两个栈,第一个用于主要操作,另一个用于存储操作pop的链接

主要有四种操作:

初始页面http://www.acm.org/  因此要push到栈1中

1 visit + 链接 因此push到栈1 这是要求forward中全为空,也就是栈2为空

2 back 如果栈1中只有一个链接,back后出界,“ignored”

       否则,先将栈1的top push到栈2中,再pop,最后输出栈1新的top,即上一个页面

3 forward 如果栈2为空,说明没有下一个页面,出界,“ignored”

          否则,将栈2的top push到栈1中,完善操作链,后输出栈2的top,最后pop栈2

4 quit 直接退出操作

#include<iostream>

#include<string>

#include<stack>

using namespacestd;

int main()

{

    stack<string> s1, s2;

    string act, url;

    s1.push("http://www.acm.org/");

    while(cin >> act){

        if(act[0] =='Q'){

            break;

        }

        elseif(act[0] =='V'){

            cin >> url;

            s1.push(url);

            cout << url <<endl;

            while(!s2.empty()){

                s2.pop();

            }

        }

        elseif(act[0] =='B'){

            if(s1.size() >1){

                s2.push(s1.top());

                s1.pop();

                cout << s1.top() <<endl;

            }

            else{

                cout <<"Ignored" << endl;

            }

        }

        else{

            if(!s2.empty()){

                s1.push(s2.top());

                cout << s2.top() <<endl;

                s2.pop();

            }

            else{

                cout <<"Ignored" << endl;

            }

        }

    }

    return0;

}










原创粉丝点击