stack的应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#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);
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;
} |