停车场管理程序实验报告一实验题目:停车场管理程序二实验要求:编写一个程序实现停车场的管理功能.并且,以栈模拟停车场,以队列模拟车场外到达便道的过程,按照从终端读入的输入数据序列进行模拟管理.栈以顺序结构实现,队列顺序循环结构实现.用户输入的命令有以下5种:(1)汽车到达;(2)汽车离去输出停车场中的所有汽车牌号;(3)输出候车场中的所有汽车牌号;(4)退出系统运行.三实验内容:3.1栈的抽象数据类型:ADTStack{数据对象:D={ai|ai∈ElemSet,i=1,2,⋯,n,n≥0}数据关系:R1={
|ai-1,ai∈D,i=1,2,⋯,n}约定an端为栈顶,a1端为栈底.基本操作:{InitStack(&S)操作结果:构造一个空栈S.DestroyStack(&S)初始条件:栈S已存在.操作结果:销毁栈S.ClearStack(&S)初始条件:栈S已存在.操作结果:将S清为空栈.StackEmpty(S)初始条件:栈S已存在.操作结果:若S为空栈,则返回TRUE,否则返回FALSE.StackLength(S)初始条件:栈S已存在.操作结果:返回S的数据元素个数,即栈的长度.GetTop(S,&e)初始条件:栈S已存在且非空.操作结果:用e返回S的栈顶元素.Push(&S,e)初始条件:栈S已存在.操作结果:插入元素e为新的栈顶元素.Pop(&S,&e)初始条件:栈S已存在且非空.操作结果:删除S的栈顶元素,并用e返回其值.StackTraverse(S,visit())初始条件:栈S已存在且非空.操作结果:从栈底到栈顶依次对S的每个数据元素调用函数visit().一旦visit()失败,则操作失败.}ADTStack3.2存储结构的定义;#defineN3#defineM4#defineprice2typedefstruct{intcarno[N];intcartime[N];inttop;}SqStack;typedefstruct{intcarno[M];intfront,rear;}SqQueue;3.3基本操作实现:/*创建栈*/voidInitStack(SqStack*&s){s=(SqStack*)malloc(sizeof(SqStack));s->top=-1;}/*摧毁栈*/voidDestroyStack(SqStack*&s){free(s);}/*查看栈是否为空*/boolStackEmpty(SqStack*s){returns->top==-1;}/*进栈*/boolPush(SqStack*&s,inte1,inte2){if(s->top==N-1){returnfalse;}s->top++;s->carno[s->top]=e1;s->cartime[s->top]=e2;//printf(">>停车场中位置:%d\n",e1);returntrue;}boolStackFull(SqStack*s){returns->top==N-1;}/*出栈*/boolPop(SqStack*&s,int&e1,int&e2){if(s->top==-1)returnfalse;e1=s->carno[s->top];e2=s->cartime[s->top];s->top--;returntrue;}voidDispStack(SqStack*s){printf(">>停车场中的车辆为:");inti;for(i=s->top;i>=0;--i){printf("%d",s->carno[i]);}printf("\n");}/*****************以下为队列*****************//*初始化队列*/voidInitQueue(SqQueue*&q){q=(SqQueue*)malloc(sizeof(SqQueue));q->front=q->rear=0;}/*释放队列*/voidDestroyQueue(SqQueue*&q){free(q);}/*查看队列是否为空*/boolQueueEmpty(SqQueue*q){returnq->front==q->rear;}boolQueueFull(SqQueue*q){return(q->rear+1)%M==q->front;}/*进队*/boolenQueue(SqQueue*&q,inte){if((q->rear+1)%M==q->front)returnfalse;q->rear=(q->rear+1)%M;q->carno[q->rear]=e;returntrue;}/*出队*/booldeQueue(SqQueue*&q,int&e){if(q->front==q->rear)returnfalse;q->front=(q->front+1)%M;e=q->carno[q->front];returntrue;}3.4解题思路:1.通过栈模拟停车场2.通过队列模拟候车场3.然后全程模拟即可.3.5解题过程:实验源代码如下:#include#include#defineN3#defineM4#defineprice2typedefstruct{intcarno[N];intcartime[N];inttop;}SqStack;typedefstruct{intcarno[M];intfront,rear;}SqQueue;/*创建栈*/voidInitStack(SqStack*&s){s=(SqStack*)malloc(sizeof(SqStack));s->top=-1;}/*摧毁栈*/voidDestroyStack(SqStack*&s){free(s);}/*查看栈是否为空*/boolStackEmpty(SqStack*s){returns->top==-1;}/*进栈*/boolPush(SqStack*&s,inte1,inte2){if(s->top==N-1){returnfalse;}s->top++;s->carno[s->top]=e1;s->cartime[s->top]=e2;//printf(">>停车场中位置:%d\n",e1);returntrue;}boolStackFull(SqStack*s){returns->top==N-1;}/*出栈*/boolPop(SqStack*&s,int&e1,int&e2){if(s->top==-1)returnfa...