【poj解题】1028

时间:2021-02-14 18:06:19

stack的应用

#include<iostream>
#include<stack>
#include<stdio.h>
#include<stdlib.h>
#include<string.h> #define MAX 128 using namespace std; stack<char *> back_stack;
stack<char *> forward_stack; int main() {
char raw[MAX];
char * now;
char * s;
now = (char *)malloc(sizeof(char) * 128);
strcpy(now, "http://www.acm.org/");
while(1) {
cin >> raw;
if(raw[0] == 'Q') {
break;
}
if(raw[0] == 'V') {
cin >> raw;
back_stack.push(now);
s = (char *)malloc(sizeof(char) * MAX);
strcpy(s, raw);
now = s;
printf("%s\n", s);
while(!forward_stack.empty()) {
forward_stack.pop();
}
}
else if(raw[0] == 'B') {
if(back_stack.size() == 0) {
printf("Ignored\n");
}
else {
s = back_stack.top();
back_stack.pop();
printf("%s\n", s);
forward_stack.push(now);
now = s;
}
}
else if(raw[0] == 'F') {
if(forward_stack.size() == 0) {
printf("Ignored\n");
}
else {
s = forward_stack.top();
forward_stack.pop();
printf("%s\n", s);
back_stack.push(now);
now = s;
}
}
else {
;
}
}
while(back_stack.size() != 0) {
s = back_stack.top();
free(s);
back_stack.pop();
}
while(forward_stack.size() != 0) {
s = forward_stack.top();
free(s);
forward_stack.pop();
}
return 0;
}