zoukankan      html  css  js  c++  java
  • SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一: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
    ",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
    ");
            else
                show(p->top);
        }
        return 0;
    }
    
  • 相关阅读:
    python之函数对象、函数嵌套、名称空间与作用域、装饰器
    python之函数
    python基础-小练习
    python基础之文件操作
    python基础之字符编码
    web开发-Django博客系统
    HotSpot的算法实现
    垃圾回收机制(GC)
    Java注意点...
    JVM内存区域及对象
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9748079.html
Copyright © 2011-2022 走看看