汽车在停车场内进出是按照栈的运算方式来实现的,先到的先进停车场;停车场的汽车离开停车场时,汽车场内其它汽车为该辆汽车让路,也是按栈的方式进行;汽车在便道上等候是按队列的方式进行的。因此,将停车场设计成一个栈,汽车让路也需要另一个栈来协助完成,汽车进出便道用队列来实现。
本设计,栈采用顺序栈结构,队列用链式存储结构。
#include <bits/stdc++.h>
using namespace std;
#define stacksize 10
typedef struct sqstack
{
int data[stacksize];
int top;
} SqStackTp;
typedef struct linked_queue
{
int data;
struct linked_queue * next;
} LqueueTp;
typedef struct
{
LqueueTp *Front, *Rear ;
} QueptrTp;
QueptrTp Q;
void InitStack(SqStackTp &s)
{
s.top=-1;
}
void InitQueue(QueptrTp &Q)
{
Q.Front=Q.Rear=new LqueueTp;
Q.Front->next=NULL;
}
void EnQueue(QueptrTp &Q,int num)//进便道
{
LqueueTp *p=new LqueueTp;
p->data=num;
p->next=NULL;
Q.Rear->next=p;
Q.Rear=p;
cout<<num<<" 号车进入便道"<<endl;
system("pause");
}
int OutQueue(QueptrTp &Q)//出便道
{
LqueueTp *p;
int num;
if(Q.Front==Q.Rear)
return -1;//便道无车
p=Q.Front->next;
num=p->data;
Q.Front->next=p->next;
if(Q.Rear==p)
Q.Rear=Q.Front;
delete p;
return num;
}
void InPark(SqStackTp &Park,int num)//进停车场
{
if(Park.top==stacksize-1)
{
cout<<"进库失败!停车场车辆已满,请进入便道等候"<<endl;
system("pause");
EnQueue(Q,num);
return;
}
Park.top++;
Park.data[Park.top]=num;
cout<<"车辆 "<<num<<" 号已经进入停车场"<<endl;
cout<<"停车场还剩余 "<<stacksize-Park.top-1<<" 个位置"<<endl;
system("pause");
}
void InTPark(SqStackTp &Park,int num)//进临时停车场
{
if(Park.top==stacksize-1)
{
cout<<"进库失败!临时停车场车辆已满,请进入便道等候"<<endl;
system("pause");
return;
}
Park.top++;
Park.data[Park.top]=num;
cout<<"车辆 "<<num<<" 号已经进入临时停车场"<<endl;
cout<<"临时停车场还剩余 "<<stacksize-Park.top-1<<" 个位置"<<endl;
system("pause");
}
void OutPark(SqStackTp &Park,SqStackTp &TPark,int num)//出临时停车场
{
int flag=0;
if(Park.top==-1)
{
cout<<"ERRO";
system("pause");
return;
}
int n=Park.top;
for(n; n>-1; n--)
if(Park.data[n]==num)
flag=1;
if(!flag)
{
cout<<"出库失败!车库无"<<num<<"号车!"<<endl;
system("pause");
return;
}
for(Park.top; Park.top>-1; Park.top--)
{
if(Park.data[Park.top]==num)
{
Park.top--;
break;
}
else
InTPark(TPark,Park.data[Park.top]);
}
cout<<"\n"<<num<<"号车辆出库完成,其他车辆回车库"<<endl<<endl;
system("pause");
for(TPark.top; TPark.top>-1; TPark.top--)
InPark(Park,TPark.data[TPark.top]);
for(Park.top; Park.top<stacksize;)//便道车进入停车场
{
int Qnum=OutQueue(Q);
if(Qnum==-1)//便道无车
return;
InPark(Park,Qnum);
if(Park.top==stacksize-1)//停车场满
return;
}
}
void SeePark(SqStackTp Park)
{
if(Park.top==-1)
cout<<"暂无车辆!"<<endl;
for(Park.top; Park.top>-1;)
{
cout<<"->"<<Park.data[Park.top]<<"号车";
Park.top--;
}
cout<<"\n";
system("pause");
}
void SeeQueue(QueptrTp Q)
{
if(Q.Front==Q.Rear)
{
cout<<"暂无车辆!"<<endl;
system("pause");
return;
}
LqueueTp *p=Q.Front->next;
while(p!=NULL)
{
cout<<"->"<<p->data<<"号车";
p=p->next;
}
cout<<endl;
system("pause");
}
void Order()
{
int x,num;
SqStackTp Park,TPark;
InitQueue(Q);
InitStack(Park);
InitStack(TPark);
while(1)
{
cout<<"*说明:\n*0-退出\n*1-进停车场\n*2-出停车场\n*3-查看停车场车辆情况\n*4-查看便道车辆情况\n";
cout<<"请输入指令:";
cin>>x;
switch(x)
{
case 0:
return;
break;
case 1:
cout<<"请输入入库车号:";
cin>>num;
InPark(Park,num);
system("cls");
break;
case 2:
cout<<"请输入出库车号:";
cin>>num;
OutPark(Park,TPark,num);
system("cls");
break;
case 3:
SeePark(Park);
system("cls");
break;
case 4:
SeeQueue(Q);
system("cls");
break;
default:
cout<<"请输入有效数字(0-4)!!!!!"<<endl;
system("pause");
system("cls");
}
}
}
int main()
{
Order();
return 0;
}
//小绵杨Yeanling