分析:题目中说用栈实现,我觉得这样很麻烦,就用了一个数组+指针解决了.其实就是大模拟.
#include <stack> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; string s, ss; stack <string> s1, s2; string q[1010]; int cnt = 1, maxx = 1; int main() { q[1] = "http://www.acm.org/"; while (cin >> s && s != "QUIT") { if (s == "VISIT") { cin >> ss; q[++cnt] = ss; maxx = cnt; cout << ss << endl; } else if (s == "BACK") { if (cnt - 1 <= 0) printf("Ignored "); else { cnt--; cout << q[cnt] << endl; } } else { if (cnt + 1 > maxx) printf("Ignored "); else { cnt++; cout << q[cnt] << endl; } } } return 0; }