HDU - ACboy needs your help again!(链式队列&栈)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1702
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

ACboy was kidnapped!! 
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

Input

The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.

Output

For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.

Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3

Problem solving report:

Description: 有两种方式的输出,一种是先进先出(队列),一种就是先进后出(栈),求最终的输出顺序。
Problem solving: 直接模拟进栈出栈以及进队出队这个过程,为了方便,栈和队列放到了一个链表上,用的是双向链表,当然用单向的也可以,但为了清楚地模拟那个过程就用了双向的。

Accepted Code:

#include <bits/stdc++.h>
using namespace std;
typedef struct ListPtr {
    int data;
    ListPtr *next, *prior;
}*lists;
int len = 0;
lists head, tail;//链头链尾
void Alloc(lists &p) {//申请空间
    p = (ListPtr *)malloc(sizeof(ListPtr));
    p -> next = NULL;
    p -> prior = NULL;
}
void link() {//初始化
    len = 0;
    Alloc(head);
    tail = head;
}
void push(int e) {//进队,进栈
    lists p;
    Alloc(p);
    p -> data = e;
    p -> prior = tail;
    tail -> next = p;
    tail = p;
    len++;
}
void pop_h() {//出队
    len--;
    lists p = head -> next;
    head -> next = p -> next;
    if (p == tail)
        tail = head;
    else p -> next -> prior = head;
    free(p);
    head -> prior = NULL;
}
void pop_t() {//出栈
    len--;
    lists p = tail;
    tail = tail -> prior;
    free(p);
    tail -> next = NULL;
}
int front() {//访问队头元素
    return head -> next -> data;
}
int back() {//访问栈顶元素
    return tail -> data;
}
int main() {
    int n, t, e;
    char str[5], io[3];
    scanf("%d", &t);
    while (t--) {
        link();
        scanf("%d%s", &n, str);
        if (str[2] != 'L') {
            for (int i = 0; i < n; i++) {
                scanf("%s", io);
                if (io[0] != 'O') {
                    scanf("%d", &e);
                    push(e);
                }
                else {
                    if (!len)
                        printf("None\n");
                    else {
                        printf("%d\n", front());
                        pop_h();
                    }
                }
            }
        }
        else {
            for (int i = 0; i < n; i++) {
                scanf("%s", io);
                if (io[0] != 'O') {
                    scanf("%d", &e);
                    push(e);
                }    
                else {
                    if (!len)
                        printf("None\n");
                    else {
                        printf("%d\n", back());
                        pop_t();
                    }
                }
            }
        }
    }
    return 0;
}
上一篇:a technical problem with the server created an error.try again to continue what you were doing .if t


下一篇:Begin Again