数据结构停车场
#include "stdio.h"
#include "stdlib.h"
#define MAX 3
#define price 2
/*创建停车场堆栈的结构体类型*/
typedef struct{
char num[10];//车牌
int hour,minute;//车辆进入的时间
}information;
typedef struct{
int top;//栈顶指针
information stack[MAX];
// int stacksize;//当前已分配的存储空间,以元素为单元
}Sqstack;
/*创建便道的表现形式,队列的链式存储结构*/
typedef struct QNode{
information now;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
void Initstack(Sqstack *s)
//先构造一个空堆栈
{
s->top = 0;
}
int InitQueue(LinkQueue *Q)
//构造一个空的队列
{
Q->front = Q->rear = (QNode*)malloc(sizeof(QNode));
if(!Q->front) return 0;//队头指针无效,存储分配失败
Q->front->next = NULL;
return 1;
}
void push(Sqstack *s,information data)//停车场车辆进入,将基本信息压入堆栈
{
if(s->top > MAX) printf("OVERLOW!");
else
{
s->stack[s->top] = data;//压入基本信息
s->top ++;
}
}
void parkinglist(Sqstack *s)
//显示停车场内的所有信息
{
int i;
if(s->top > 0)
{
printf("\t\tParking lot: \n");
printf("\t\tPosition\tReaching time\tCar number: \n");
for(i = 0;i top;i ++)
{
printf("\t\t%d\t\t%d:%d\t\t%s\n",i+1,s->stack[i].hour,s->stack[i].minute,s->stack[i].num); puts("");
}
}
else
{
printf("\t\tThere are no cars in the parking lot!\n");
}
}
void makeshiftlist(LinkQueue *Q)
//显示便道上的所有信息
{
QueuePtr p;
p = Q->front->next;
if(Q->front != Q->rear)
{
printf("\t\tMakeshift road: \n");
printf("\t\tCar number: \n");
while(p!= NULL)
{
printf("\t\t%s \n",p->now.num);
p = p->next;
puts("");
}
}
else
{
printf("\t\tThere are no cars at the makeshift road!\n");
}
}
void List(Sqstack *s,LinkQueue *Q)
{
int choice,flag;
flag = 1;
while(flag)
{
printf("\t\t1.Parking lot\t2.Makeshift road\t3.Return main menu\n");
printf("\t\tPlease choose 1|2|3 : ");
while(1)
{
scanf("%d",&choice);
puts("");
if(choice 3)
{
printf("\t\tERROR!Please enter the right choice: ");
}
else
{
break;
}
}
switch(choice)
{
case 1 : parkinglist(s);break;
case 2 : makeshiftlist(Q);break;
case 3 : flag=0;break;
default : break;
}
}
}
int EnQueue(LinkQueue *Q,information data)
//便道的插入
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if(!p) return 0;
p->now = data;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return 1;
}
void arrive(Sqstack *s,LinkQueue *Q,information data)
//当有车辆要进入停车场时
{
printf("\t\tPlease enter your car number: ");
scanf("%s",&data.num);
if(s->top
{
while(1)
{
printf("\t\tPlease enter the reaching time: ");
scanf("%d:%d",&data.hour,&data.minute);
if(data.hour >= 0 && data.hour = 0 && data.minute
else
{
printf("\t\tplease enter the right time: ");
scanf("%d:%d",&data.hour,&data.minute);
break;
}
}
push(s,data);//把车辆的信息压入堆栈
}
else//停车场已满,要在便道上等待
{
printf("\n\tSorry!The parking lot is full,please wait at the makeshift road!!\n"); EnQueue(Q,data);
}
}
int pop(Sqstack *s,information data)//车辆出栈
{
if(s->top == 0) return 0;
else
{
s->top --;
}
}
int DeQueue(LinkQueue *Q)
//当便道有车离开的时候,车辆结点的删除
{
QueuePtr p;
if(Q->front == Q->rear) return 0;
p = Q->front->next;
Q->front->next = p->next;
if(Q->rear == p) Q->rear = Q->front;
free(p);
// return 1;
}
void leave(Sqstack *s,Sqstack *r,LinkQueue *Q,information data)//有车辆离开
{
int temp;//要离开车辆的位置
if(s->top > 0)
{
while(1)
{
printf("\t\tThere are %d cars in the parking lot.\n",s->top);
printf("\t\tplease enter the position of the car which is going to leave : "); scanf("%d",&temp);
if(temp >= 1 && temp top) break;
else
{
printf("\t\tplease enter the right position: ");
scanf("%d",&temp);
break;
}
}
while(s->top > temp)//让路车辆出栈并进入到暂存栈中
{
s->top --;
r->stack[r->top] = s->stack[s->top];
r->top ++;
}//显示车辆信息
while(1)
{
//s->top ++;
printf("\t\tplease enter the leaving time: ");
scanf("%d:%d",&data.hour,&data.minute);
if(data.hour >= 0 && data.hour = 0 && data.minute = s->stack[s->top-1].hour)) break;
else
{
printf("\t\tplease enter the right time: ");
scanf("%d:%d",&data.hour,&data.minute);
break;
}
}
printf("\t\tThe number of the leaving car is %s\n",s->stack[s->top-1].num);
printf("\t\tThe reaching time is %d:%d\n",s->stack[s->top-1].hour,s->stack[s->top-1].minute);
printf("\t\tThe leaving time is %d:%d\n",data.hour,data.minute);
if(data.hour == s->stack[s->top-1].hour)
{
printf("\t\tYou totally spend %d dollars!\n",price);
}
else
{
printf("\t\tYou totally spend %d dollars!\n",(data.hour - s->stack[s->top-1].hour)*price);
}
while(r->top >= 1)//放回停车场中
{
r->top --;
// s->top --;//有问题,如果是两辆车那么减掉就变成0了就相当于堆栈里面没有东西了
s->stack[s->top-1] = r->stack[r->top];
s->top ++;
}
s->top --;
//如果便道上有车并且停车场未满的情况下
if(s->top front != Q->rear))
{
s->top ++;
s->stack[s->top-1] = Q->front->next->now;
puts("");
printf("\t\tNow you can park!Please enter the reaching time: ");
scanf("%d:%d",&data.hour,&data.minute);
s->stack[s->top-1].hour = data.hour;
s->stack[s->top-1].minute = data.minute;
DeQueue(Q);
}
else
{
printf("\t\tThere are no cars at the makeshift road!\n");
}
}
else
{
printf("\t\tThere are no cars in the parking lot!\n");
}
}
void main()
{
information car;
int choice;
Sqstack s;
Sqstack r;
} LinkQueue Q; Initstack(&s); Initstack(&r); InitQueue(&Q); while(1) { puts(""); printf("\t\t\tParking Management System\n"); puts(""); puts(""); printf("\t\t1.Car Arrival.\t\tPlease enter 1\n"); printf("\t\t2.Car Departure.\tPlease enter 2\n"); printf("\t\t3.Car Information.\tPlease enter 3\n"); printf("\t\t4.Exit the program.\tPlease enter 4\n"); puts(""); puts(""); printf("\t\tPlease choose the above information: "); while(1) { scanf("%d",&choice); puts(""); if(choice 4) { printf("\t\tERROR!Please enter the right choice: "); } else { break; } } switch(choice) { case 1 : arrive(&s,&Q,car);break; case 2 : leave(&s,&r,&Q,car);break; case 3 : List(&s,&Q);break; case 4 : exit(0); default : break; } }