数据结构课程设计(二)停车场管理:便道-队列,停车场-栈,临时停车场-栈。

汽车在停车场内进出是按照栈的运算方式来实现的,先到的先进停车场;停车场的汽车离开停车场时,汽车场内其它汽车为该辆汽车让路,也是按栈的方式进行;汽车在便道上等候是按队列的方式进行的。因此,将停车场设计成一个栈,汽车让路也需要另一个栈来协助完成,汽车进出便道用队列来实现。
本设计,栈采用顺序栈结构,队列用链式存储结构。

#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
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小绵杨Yancy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值