C语言实现停车场
static_queue.hstruct park_infor
{
int park_no;
int park_time;
};
struct park_sqstack
{
struct park_infor data[10];
int top;
};
struct wait_infor
{
int wait_time;
struct wait_infor *next;
};
struct wait_linksqueue
{
struct wait_infor *front;
struct wait_infor *rear;
};
struct park_sqstack *Creat_sqstack();
struct wait_linksqueue *Creat_linksqueue();
void Push_sqstack(struct park_sqstack *p);
void Insert_linksqueue(struct wait_linksqueue *p,int data);
void Sort_linksqueue(struct wait_linksqueue *p);
void Pop_sqstack(struct park_sqstack *p);
int Gethead_linksqueue(struct wait_linksqueue *p);
void Delete_linksqueue(struct wait_linksqueue *p);
int Gettop_sqstack_no(struct park_sqstack *p);
int Gettop_sqstack_time(struct park_sqstack *p);
void Sort_sqstack(struct park_sqstack p);
int Check_park_no(struct park_sqstack p);
static_queue.c
#include "stdio.h"
#include "stdlib.h"
#include "stack_queue.h"
struct park_sqstack *Creat_sqstack()
{
struct park_sqstack *p;
p=(struct park_sqstack *)malloc(sizeof(struct park_sqstack));
p->top=-1;
return p;
}
struct wait_linksqueue *Creat_linksqueue()
{
struct wait_infor *p;
struct wait_linksqueue *q;
p=(struct wait_infor *)malloc(sizeof(struct wait_infor));
q=(struct wait_linksqueue *)malloc(sizeof(struct wait_linksqueue));
p->next=NULL;
q->front=q->rear=p;
return q;
}
void Push_sqstack(struct park_sqstack *p)
{
p->top++;
}
void Insert_linksqueue(struct wait_linksqueue *p,int data)
{
struct wait_infor *q;
q=(struct wait_infor *)malloc(sizeof(struct wait_infor));
q->wait_time=data;
q->next=NULL;
p->rear=q;
p->rear=p->rear->next;
}
void Sort_linksqueue(struct wait_linksqueue *p)
{
struct wait_infor *temp;
temp=(struct wait_infor *)malloc(sizeof(struct wait_infor));
temp=p->front;
temp=temp->next;
while(temp->next!=NULL)
{
printf("%d\t",temp->wait_time);
temp=temp->next;
}
printf("\n");
}
void Pop_sqstack(struct park_sqstack *p)
{
p->top--;
}
int Gethead_linksqueue(struct wait_linksqueue *p)
{
return p->front->next->wait_time;
}
void Delete_linksqueue(struct wait_linksqueue *p)
{
struct wait_infor *q;
q=p->front->next;
p->front->next=q->next;
free(q);
}
int Gettop_sqstack_no(struct park_sqstack *p)
{
int park_no;
park_no=p->data[p->top].park_no;
return park_no;
}
int Gettop_sqstack_time(struct park_sqstack *p)
{
int park_time;
park_time=p->data[p->top].park_time;
return park_time;
}
void Sort_sqstack(struct park_sqstack p)
{
int temp;
temp=p.top;
printf("停车编号");
while(p.top!=-1)
{
printf("%d\t",p.data[p.top].park_no);
p.top--;
}
printf("\n");
p.top=temp;
printf("停车时间");
while(p.top!=-1)
{
printf("%d\t",p.data[p.top].park_time);
p.top--;
}
printf("\n");
}
int Check_park_no(struct park_sqstack p)
{
int park_no,temp,i;
int data[10]={0,1,2,3,4,5,6,7,8,9};
for(i=0;i<10;i++)
{
temp=p.top;
while(p.top!=-1)
{
if(data[i]!=(p.data[p.top]).park_no)
{
p.top--;
}
else
break;
}
if(p.top==-1)
{
park_no=data[i];
break;
}
p.top=temp;
}
return park_no;
}
main.c
#include "stdio.h"
#include "stdlib.h"
#include "stack_queue.h"
int main()
{
struct park_sqstack *inner,*make_way;
struct wait_linksqueue *outer;
inner=Creat_sqstack();
outer=Creat_linksqueue();
int choice,park_time,park_no;
int enable_no;
int park_time1;
int park_no1;
printf("***************本停车场共有10个停车位***************\n");
while(1)
{
printf("***************Welcome come to Parking**************\n");
printf("目前停车场共停放%d车,剩余%d车位\n",inner->top+1,9-inner->top);
printf(
"\n"
"\t1.停车\n"
"\t2.离开\n"
"\t3.查看车场停车情况\n"
"\t4.退出\n"
);
printf("Please input your choice(1~4):");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("请告知您的停车时间\n");
scanf("%d",&park_time);
if(inner->top!=9)
{
if(inner->top==-1)
{
Push_sqstack(inner);
inner->data[0].park_time=park_time;
inner->data[0].park_no=0;
}
else
{
enable_no=Check_park_no(*inner);
Push_sqstack(inner);
inner->data[inner->top].park_time=park_time;
inner->data[inner->top].park_no=enable_no;
}
}
else
{
Insert_linksqueue(outer,park_time);
}
printf("您的停车编号为\n%d",inner->data[inner->top].park_no);
printf("\n");
break;
}
case 2:
{
printf("请出示您的停车位编号\n");
scanf("%d",&park_no);
if(park_no==inner->data[inner->top].park_no)
{
if(outer->front!=outer->rear)
{
Pop_sqstack(inner);
park_time=Gethead_linksqueue(outer);
Delete_linksqueue(outer);
Push_sqstack(inner);
inner->data[inner->top].park_no=park_no;
inner->data[inner->top].park_time=park_time;
}
else
{
Pop_sqstack(inner);
}
}
else
{
while(park_no!=inner->data[inner->top].park_no)
{
park_no1=Gettop_sqstack_no(inner);
park_time1=Gettop_sqstack_time(inner);
Pop_sqstack(inner);
Push_sqstack(make_way);
make_way->data[make_way->top].park_no=park_no1;
make_way->data[make_way->top].park_time=park_time1;
}
Pop_sqstack(inner);
if(outer->front!=outer->rear)
{
park_time=Gethead_linksqueue(outer);
Delete_linksqueue(outer);
Push_sqstack(inner);
inner->data[inner->top].park_no=park_no;
inner->data[inner->top].park_time=park_time;
}
while(make_way->top!=-1)
{
park_no1=Gettop_sqstack_no(make_way);
park_time1=Gettop_sqstack_time(make_way);
Pop_sqstack(make_way);
Push_sqstack(inner);
inner->data[inner->top].park_no=park_no1;
inner->data[inner->top].park_time=park_time1;
}
}
break;
}
case 3:
Sort_sqstack(*inner);
break;
case 4:
exit(1);
}
}
}