数据结构实验之栈与队列十一:refresh的停车场

本文介绍了一个停车场管理系统的设计与实现,该系统采用栈和队列数据结构来处理车辆进出停车场的逻辑,支持车辆排队等待等功能。
Problem Description

 refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先

进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,

Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.

Input

 输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。

Output

 输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。

Example Input
2 6
Add 18353364208
Add 18353365550
Add 18353365558
Add 18353365559
Del
Out
Example Output
18353365558
18353364208
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STACKINITSIZE 10000
#define INCREMENT 10
#define OVERFLOW -2
#define ERROR -1
#define OK 1

typedef long long int ElemType;
typedef int Statu;
typedef struct                      //栈的定义
{
    ElemType *base;
    ElemType *top;
    int stacksize;
} Stack;
typedef struct node                  //队列定义
{
    ElemType data;
    struct node *next;
} Lnode;
typedef struct
{
    Lnode *front, *rear;
} Lqueue;

Statu InitStack(Stack &S);             //初始化
Statu Push(Stack &S, ElemType n);          //入栈
Statu Pop(Stack &S, ElemType &n);          //出栈
Statu Getop(Stack &S, ElemType &n);      //获得栈顶元素
Statu Initqueue(Lqueue &Q);           //初始化
Statu Enqueue(Lqueue &Q, ElemType n);      //入队
Statu Dequeue(Lqueue &Q, ElemType &n);  //出队

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        Stack S;
        InitStack(S);
        Lqueue Q;
        Initqueue(Q);
        int t = 0;
        long long int x;
        char a[10];
        while(m--)
        {
            scanf("%s", a);
            if(strcmp(a, "Add") == 0)
            {
                scanf("%lld", &x);
                if(S.top - S.base >= n)
                {
                    Enqueue(Q, x);
                }
                else
                {
                    Push(S, x);
                }
            }
            else if(strcmp(a, "Del") == 0)
            {
                if(S.base == S.top)
                {
                    t = 1;
                }
                else
                {
                    ElemType o;
                    Pop(S, o);
                    if(Q.front != Q.rear)
                    {
                        ElemType o;
                        Dequeue(Q, o);
                        Push(S, o);
                    }
                }
            }
            else if(strcmp(a, "Out") == 0)
            {
                if(Q.front == Q.rear)
                {
                    t = 1;
                }
                else
                {
                    ElemType o;
                    Dequeue(Q, o);
                }
            }
        }
        if(t)
            printf("Error\n");
        else
            while(S.base != S.top)
            {
                ElemType o;
                Pop(S, o);
                printf("%lld\n", o);
            }
    }
    return 0;
}
Statu InitStack(Stack &S)             //初始化
{
    S.base = (ElemType *)malloc(STACKINITSIZE * sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACKINITSIZE;
    return OK;
}
Statu Push(Stack &S, ElemType n)           //入栈
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base=(ElemType *)realloc(S.base,(S.stacksize + INCREMENT) * sizeof(ElemType));
        if(!S.base) exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += INCREMENT;
    }
    *(S.top++)=n;
    return OK;
}

Statu Pop(Stack &S, ElemType &n)           //出栈
{
    if(S.base == S.top)
        exit(OVERFLOW);
    n = *(--S.top);
    return OK;
}
Statu Initqueue(Lqueue &Q)            //初始化
{
    Q.front = Q.rear = (Lnode *)malloc(sizeof(Lnode));
    if(!Q.front) exit(OVERFLOW);
    Q.front -> next = NULL;
    return OK;
}

Statu Enqueue(Lqueue &Q, ElemType n)       //入队
{
    Lnode *p;
    p = (Lnode *)malloc(sizeof(Lnode));
    if(!p)
        exit(OVERFLOW);
    p -> data = n;
    p -> next = NULL;
    Q.rear -> next = p;
    Q.rear = p;
    return OK;
}
Statu Dequeue(Lqueue &Q, ElemType &n)  //出队
{
    if(Q.front == Q.rear) return ERROR;
    Lnode *p;
    p = Q.front -> next;
    n = p -> data;
    Q.front -> next = p->next;
    if(Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return OK;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值