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

来源:互联网 发布:淘宝美工助理工作内容 编辑:程序博客网 时间:2024/06/06 07:03

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

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

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 6Add 18353364208Add 18353365550Add 18353365558Add 18353365559DelOut

Example Output

1835336555818353364208
这是一个把队列和栈结合起来的题目,在通道里可以用队列模拟,而在停车场里可以用栈来模拟,不过要考虑好什么时候栈空,什么时候栈满,以及对应什么操作。
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef long long int elemtype;typedef long long int Qelemtype;typedef int status;//#define MAXSIZE 200010#define OVERFLOW -2#define another 10010#define QUEUESIZE 1000000#define true 1#define false 0#define OK 1int MAXSIZE;typedef struct Qnode{Qelemtype data;struct Qnode *next;}Qnode, *Queuenode;typedef struct {elemtype *base;elemtype *top;int stacksize;}Sqstack;typedef struct{Queuenode front;Queuenode rear;int queueLength;}linkQueue;//Stackvoid initStack(Sqstack &S){            //初始化栈S.base = new elemtype[MAXSIZE];S.top = S.base;S.stacksize = MAXSIZE;}status isEmpty(Sqstack &S){             //栈的判空if(S.top == S.base)return true;elsereturn false;}status isFull(Sqstack &S){if((S.top-S.base) >= MAXSIZE)return true;else return false;}elemtype getTop(Sqstack &S){           //获取栈顶元素if(S.base == S.top)return false;elsereturn *(S.top-1);}void cleanStack(Sqstack &S){                 //清空栈S.base = S.top;}void Push(Sqstack &S, elemtype e){      //压栈if(S.top-S.base >= S.stacksize){S.base = (elemtype *)realloc(S.base,(another+S.stacksize)*sizeof(elemtype));S.top = S.base + S.stacksize;S.stacksize += another;}*S.top++ = e;}status Pop(Sqstack &S, elemtype &e){       //出栈if(S.top == S.base) return false; e = * --S.top;}//Queuevoid initQueue(linkQueue &Q){         //初始化队列Q.front = new Qnode;Q.rear = Q.front;if(!Q.front) exit(OVERFLOW);Q.front->next = NULL;Q.queueLength = 0;}void cleanQueue(linkQueue &Q){//清空队列while(Q.front){Q.rear = Q.front->next;free(Q.front);Q.front = Q.rear;}}void enQueue(linkQueue &Q, Qelemtype e){//元素入队列Queuenode p;p = new Qnode;if(!p) exit(OVERFLOW);p->data = e;p->next = NULL;Q.rear->next = p;Q.rear = p;Q.queueLength++;}void deQueue(linkQueue &Q, Qelemtype &e){//元素出队列if(Q.front == Q.rear) return;Queuenode p;p = Q.front->next;e = p->data;Q.front->next = p->next;if(Q.rear == p)        Q.rear = Q.front;free(p);}int QueueEmpty(linkQueue &Q){//队列判空if(Q.rear  == Q.front)return true;elsereturn false;}//主函数int main(){int m;while(~scanf("%d %d", &MAXSIZE, &m)){Sqstack S;initStack(S);linkQueue Q;initQueue(Q);int flag = 1;char order[10];long long int num;while(m--){scanf("%s", order);if(strcmp(order, "Add") == 0){scanf("%lld", &num);if(isFull(S))enQueue(Q, num);elsePush(S, num);}else if(strcmp(order, "Del") == 0){if(isEmpty(S))flag = 0;else{    elemtype temp;    Pop(S, temp);    if(!QueueEmpty(Q)){    deQueue(Q, temp);    Push(S, temp);    }}}else if(strcmp(order, "Out") == 0){if(QueueEmpty(Q))flag = 0;else{elemtype temp;deQueue(Q, temp);}}}if(!flag)printf("Error\n");else{while(!isEmpty(S)){elemtype temp;    Pop(S, temp);printf("%lld\n", temp);}}}return 0;}


阅读全文
0 0
原创粉丝点击