首页 > 其他 > 详细

【poj解题】1028

时间:2014-01-29 01:19:55      阅读:379      评论:0      收藏:0      [点我收藏+]

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);
    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;
}

  

【poj解题】1028

原文:http://www.cnblogs.com/igloo1986/p/3535898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!