Title Link: http://poj.org/problem?id=1028
Attention:
1, with two stacks to simulate, one to save can return, one to save can go forward.
2, visit method, you will be able to forward the stack empty.
3, back method, the current page to the stack can go forward, and can return the stack out of an element.
4, forward method, the current page is saved to the stack that can be returned, the stack can go forward to stack an element, into the current page.
#include <iostream>#include<stack>#include<string>using namespaceStd;stack<string>Bstack,fstack;stringCurrent ;voidvisit () {if(current!="") Bstack.push (current); CIN>>Current ; cout<<current<<Endl; while(!fstack.empty ())///to empty the previousFstack.pop (); return;}voidBack () {if(Bstack.empty ()) {cout<<"ignored\n"; return; } fstack.push (current); ///Put the current in the previous stackCurrent=bstack.top ();///get a new current pageBstack.pop (); cout<<current<<Endl;}voidforward () {if(Fstack.empty ()) {cout<<"ignored\n"; return; } bstack.push (current); ///current page to present stackCurrent=fstack.top ();///The current page becomes the previous pageFstack.pop (); cout<<current<<Endl;}intMain () {stringcmd; Current="http://www.acm.org/"; while(cin>>cmd&&cmd!="QUIT") { if(cmd=="VISIT") visit (); Else if(cmd=="FORWARD") forward (); Else if(cmd==" Back") back (); } return 0;}
View Code
Browse stack for simulated web pages (POJ1028)