顺序栈 顺序队列编写的小停车场

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


#define TRUE  1
#define FALSE 0


#define SIZE 10
#define Price 2


//停车场  
typedef int ParkData;
typedef struct _park
{
ParkData CarNo[SIZE];     //车牌号
ParkData CarTime[SIZE];   //时间
int top;
}Park;


int InitPark(Park *p)
{
if(p==NULL)
{
return FALSE;
}
p->top=-1;
}


int ParkEmpty(Park *p)
{
if(p==NULL)
{
return FALSE;
}
return p->top==-1;
}


int ParkFull(Park *p)
{
if(p==NULL)
{
return FALSE;
}
return p->top==(SIZE-1);
}


int Push(Park *p,ParkData x,ParkData y)
{
if(p==NULL)
return FALSE;
if(ParkFull(p))
   return FALSE;

p->CarNo[(p->top)+1]=x;
p->CarTime[(p->top)+1]=y;
p->top++;

return TRUE;
}


int Pop(Park *p,ParkData *x,ParkData *y)
{
if(p==NULL)
return FALSE;

if(ParkEmpty(p))
return FALSE;

*x=p->CarNo[p->top];
*y=p->CarTime[p->top];
p->top--;

return TRUE;
}


void DisPark(Park *p)
{
int i;
for(i=p->top;i>=0;i--)
{
printf("%d\t",p->CarNo[i]);
}
printf("\n");
}


//候车列
typedef int QueueData;
typedef struct _queue
{
QueueData CarNo[SIZE];  车号
int front;
int rear;
}Queue;


int InitQueue(Queue *q)
{

if(q==NULL)
return FALSE;
q->front=q->rear=0;

return TRUE;
}


int QueueEmpty(Queue *q)
{

if(q==NULL)
return FALSE;
return q->front==q->rear;
}


int QueueFull(Queue *q)
{

if(q==NULL)
return FALSE;
return q->front==(q->rear+1)%SIZE;
}


int EnQueue(Queue *q,QueueData x)
{   


if(q==NULL)
return FALSE;
if(QueueFull(q))
return FALSE;
q->rear=(q->rear+1)%SIZE;
q->CarNo[q->rear]=x;



return (int)q;
}


int DeQueue(Queue *q,QueueData *x)
{


if(q==NULL)
return FALSE;
if(QueueEmpty(q))
return FALSE;

q->front=(q->front+1)%SIZE;
*x=q->CarNo[q->front];

return TRUE;
}


int DisQueue(Queue *q)
{

if(q==NULL)
return FALSE;
if (QueueEmpty(q))
   return FALSE;

int i;
i=(q->front+1)%SIZE;
printf("%d",q->CarNo[i]);

while((q->rear-i+SIZE)%SIZE>0)
{
i=(i+1)%SIZE;
printf("%d\t",q->CarNo[i]);
}
printf("\n");

return TRUE;
}


int main()
{
int choice;
int carno,cartime;
int x,y;
int i,j,t;
Park p,p1;
Queue q;
InitPark(&p);
InitPark(&p1);
InitQueue(&q);
do
{
printf("请输入指令(1:停车 2:离开 3:显示停车场 4:显示候车场 0:退出):");
scanf("%d",&choice);
switch(choice)
{
case 1:  //汽车到达
printf("输入车号和时间:");
scanf("%d,%d",&carno,&cartime);
if(!ParkFull(&p))  //停车场不满
{
Push(&p,carno,cartime);
printf("  >>停车位置:%d\n",p.top+1);
}
else  //停车场满了

       if(!QueueFull(&q))  //候车不满
{
EnQueue(&q,carno);
printf("  >>候车位置:%d\n",q.rear);
}
else   //候车满了
printf("  >>候车场已满,不能停车\n");
}
break;
case 2:  //汽车离开
printf("输入车号和时间:");
scanf("%d,%d",&carno,&cartime);
for(i=0;i<=p.top&&p.CarNo[i]!=carno;i++);  //在栈中查找要离开的车号
if(i>p.top)
printf("  >>无该编号的汽车\n");
else
{
t=p.top-i;      //需要出栈的车辆的数目
for(j=0;j<t;j++)
{
Pop(&p,&x,&y);
Push(&p1,x,y);   //进让车栈
}
Pop(&p,&x,&y);       //汽车离开
printf("  >>%d汽车停车费用:%d\n",carno,(cartime-y)*Price);
while(!ParkEmpty(&p1))  //让车栈的车重回停车栈
{
Pop(&p1,&x,&y);
Push(&p,x,y);
}
if(!QueueEmpty(&q))     //候车列不空,队头进栈
{
DeQueue(&q,&x);
Push(&p,x,cartime);
}
}
break;
case 3:    //显示停车场情况
if(!ParkEmpty(&p))
{
printf("  >>停车场的车辆:");
DisPark(&p);
}
else
printf("  >>停车场没有车\n");
break;
case 4:    //显示候车场情况
if(!QueueEmpty(&q))
{
printf("  >>候车场的车俩:");
DisQueue(&q);
}
else
printf("  >>候车场没有车\n");
break;
case 0:  //结束
if(!ParkEmpty(&p))
{
printf("  >>停车场的车辆:");
DisPark(&p);
}
if(!QueueEmpty(&q))
{
printf("  >>候车场的车俩:");
DisQueue(&q);
}
break;
default:
printf("  >>输入的指令错误\n");
break;
}
}
while(choice!=0);
return 0;

}


不喜勿喷!!!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值