1. 程式人生 > >SDUT-2088_數據結構實驗之棧與隊列十一:refresh的停車場

SDUT-2088_數據結構實驗之棧與隊列十一:refresh的停車場

truct 要求 節點 ring std include return 假設 是否

數據結構實驗之棧與隊列十一:refresh的停車場

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

refresh最近發了一筆橫財,開了一家停車場。由於土地有限,停車場內停車數量有限,但是要求進停車場的車輛過多。當停車場滿時,要進入的車輛會進入便道等待,最先進入便道的車輛會優先

進入停車場,而且停車場的結構要求只出去的車輛必須是停車場中最後進去的車輛。現告訴你停車場容量N以及命令數M,以及一些命令(Add num 表示車牌號為num的車輛要進入停車場或便道,

Del 表示停車場中出去了一輛車,Out 表示便道最前面的車輛不再等待,放棄進入停車場)。假設便道內的車輛不超過1000000.

Input

輸入為多組數據,每組數據首先輸入N和M(0< n,m <200000),接下來輸入M條命令。

Output

輸入結束後,如果出現停車場內無車輛而出現Del或者便道內無車輛而出現Out,則輸出Error,否則輸出停車場內的車輛,最後進入的最先輸出,無車輛不輸出。

Sample Input

2 6
Add 18353364208
Add 18353365550
Add 18353365558
Add 18353365559
Del
Out

Sample Output

18353365558
18353364208

這道題集中考了棧和隊列的基本操作,停車場可以看作是棧,便道可以看作是隊列。
註意如果指令錯誤只要在最後輸出一個“Error”就可以了,不需要其他輸出。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
    char s[25];
    struct node *next;
}Node;

typedef struct stack
{
    Node *top,*base;
    int len;
}Stack;

typedef struct line
{
    Node *front,*back;
    int len;
}Line;

Node *newnode()//開辟新節點
{
    Node *t;
    t = (Node*)malloc(sizeof(Node));
    t->next = NULL;
    return t;
}

/************棧的基本操作************/
Stack *newstack()//建立新棧
{
    Stack *t;
    t = (Stack *)malloc(sizeof(Stack));
    t->top = newnode();
    t->base = t->top;
    t->len = 0;
    return t;
}

void push_stack(Stack *t,char x[])//入站
{
    Node *p = newnode();
    strcpy(p->s,x);
    p->next = t->top->next;
    t->top->next = p;
    t->base = p;
    t->len++;
}

void pop_stack(Stack *t)//出棧
{
    Node *p;
    p = t->top->next;
    t->top->next = t->top->next->next;
    free(p);
    t->len--;
}

int empty_stack(Stack *t)//詢問棧是否為空
{
    if(t->top->next==NULL)
        return 1;
    return 0;
}

void clear_stack(Stack *t)//清空棧
{
    while(!empty_stack(t))
        pop_stack(t);
}

/************隊列的基本操作************/
Line *newline()//新隊列
{
    Line *t;
    t = (Line*)malloc(sizeof(Line));
    t->front = newnode();
    t->back = t->front;
    t->len = 0;
    return t;
}

void push_line(Line *t,char x[])//入隊
{
    Node *p = newnode();
    strcpy(p->s,x);
    t->back->next = p;
    t->back = p;
    t->len++;
}

void pop_line(Line *t)//出隊
{
    Node *p;
    p = t->front->next;
    t->front->next = t->front->next->next;
    t->len--;
    free(p);
}

int empty_line(Line *t)//判斷隊列是否為空
{
    if(t->front->next==NULL)
        return 1;
    else
        return 0;
}

void clear_line(Line *t)//清空隊列
{
    while(!empty_line(t))
        pop_line(t);
}

void show(Node *t)
{
    Node *q;
    q = t->next;
    while(q)
    {
        printf("%s\n",q->s);
        q = q->next;
    }
}

int main()
{
    int n,m,f;
    Line *q;
    q = newline();
    Stack *p;
    p = newstack();
    char s[10],a[25];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        clear_line(q);
        clear_stack(p);
        f = 1;
        while(m--)
        {
            scanf("%s",s);
            if(strcmp(s,"Add")==0)//如果棧滿了就入隊,否則入棧。
            {
                scanf("%s",a);
                if(p->len<n)
                    push_stack(p,a);
                else
                    push_line(q,a);
            }
            else if(strcmp(s,"Del")==0)//可以看成出棧
            {
                if(empty_stack(p))
                    f = 0;
                else
                {
                    pop_stack(p);
                    if(!empty_line(q))//隊列不為空則出隊入棧,即便道中的車進入停車場。
                    {
                        strcpy(a,q->front->next->s);
                        pop_line(q);
                        push_stack(p,a);
                    }
                }
            }
            else if(strcmp(s,"Out")==0)//可以看成出隊
            {
                if(empty_line(q))
                    f = 0;
                else
                    pop_line(q);
            }
        }
        if(!f)
            printf("Error\n");
        else
            show(p->top);
    }
    return 0;
}

SDUT-2088_數據結構實驗之棧與隊列十一:refresh的停車場