Data structure training two-stack and queue

1. The purpose of the experiment:
(1) Master the realization of sequential stacks and chain queues;
(2) Use the basic operations of stacks and queues to solve practical problems;
2. Experimental requirements: The
parking lot is a long and narrow passage that can park n cars. And there is only one door for entry and exit (stack structure).
The cars are arranged in the parking lot in the order of arrival.
If the parking lot is full of cars, subsequent cars can only wait on the lane change outside the door. Once there is a car driving away in the parking lot
, the first car on the sidewalk can enter (queue structure) .
Each car parked in the parking lot shall be paid according to the length of time it has stayed in the parking lot when it leaves (here it is assumed that the car cannot drive directly on the sidewalk waiting outside the door).
Try to design a program to realize parking lot management, and simulate management according to the data sequence input from the terminal.
(1) Use sequential stack and chain queue to achieve;
(2) Each set of input data includes three data items: car license plate, arrival or departure mark and time. ;
(3) the output of each set of input data for the operation: the vehicle reaches: output of the vehicle parking position in the parking lot or the sidewalk; leaving vehicle: output residence time and costs (no charge on the sidewalk waiting)
3. Experimental code:
(Too lazy to change the handwritten stack and queue, stl is really happy)

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
struct node
{
    
    
    string id;///车牌号
    int flag;///表示是到达还是离开:到达是1,离开是0
    int time;///表示到达或离开的时间
};
map<string,bool>mp;///停车场是否有该车
int n,cost;
stack<node>stk;
queue<node>q;

void showmain()
{
    
    
    cout<<"**********菜单*************"<<endl;
    cout<<"1.修改停车场的容量"<<endl;
    cout<<"2.输入车辆信息"<<endl;
    cout<<"3.修改停车一小时的费用"<<endl;
    cout<<"4.查看停车场的车辆情况"<<endl;
    cout<<"5.查看变道的车辆情况"<<endl;
    cout<<"6.退出系统"<<endl;
}

void change_n()
{
    
    
    int tmp;
    cin>>tmp;
    if(tmp>n)
    {
    
    
        while(stk.size()<=n)
        {
    
    
            node t=q.front();
            t.time=t.time;
            q.pop();
            stk.push(t);
            mp[t.id]=1;
        }
    }
    else
    {
    
    
        while(stk.size()>n)
        {
    
    
            node t=stk.top();
            stk.pop();
            mp[t.id]=0;
            q.push(t);
        }
    }
    n=tmp;
}

void change_cost()
{
    
    
    cin>>cost;
}

void add_car()
{
    
    
    cout<<"请输入车辆的相关信息:"<<endl;
    node t;
    cin>>t.id>>t.flag>>t.time;
    if(t.flag) //到达
    {
    
    
        if(stk.size()==n)
        {
    
    
            q.push(t);///停车场满了,只在路上停留
            cout<<"车牌号为"<<t.id<<"的车在路上的"<<q.size()<<"号位置"<<endl;
        }
        else
        {
    
    
            stk.push(t);///直接将车停到停车场里
            cout<<"车牌号为"<<t.id<<"的车在停车场的"<<stk.size()<<"号位置"<<endl;
            mp[t.id]=1;
        }
    }
    else ///离开
    {
    
    
        if(!mp[t.id])
        {
    
    
            puts("停车场没有该车");
            return ;
        }
        mp[t.id]=0;
        node tt=stk.top();
        stk.pop();
        while(stk.size()<n&&!q.empty())
        {
    
    
            node tmp=q.front();
            tmp.time=t.time;
            q.pop();
            stk.push(tmp);
            mp[tmp.id]=1;
        }
        cout<<"车牌号为"<<t.id<<"的车的花费为"<<(t.time-tt.time)*cost<<endl;
    }
    cout<<stk.size()<<"*************"<<q.size()<<endl;
}

void show_park()
{
    
    
    ///先进后出
    vector<node>v;
    int cnt=stk.size();
    while(!stk.empty()){
    
    
        v.push_back(stk.top());
        cout<<"车牌号为"<<stk.top().id<<"的车在停车场的"<<cnt--<<"号位置"<<endl;
        stk.pop();
    }
    reverse(v.begin(),v.end());
    for(int i=0;i<v.size();i++){
    
    
        stk.push(v[i]);
    }
}

void show_road()
{
    
    
    vector<node>v;
    int cnt=1;
    while(!q.empty()){
    
    
        v.push_back(q.front());
        cout<<"车牌号为"<<q.front().id<<"的车在路上的"<<cnt++<<"号位置"<<endl;
        q.pop();
    }
    for(int i=0;i<v.size();i++){
    
    
        q.push(v[i]);
    }
}

int main()
{
    
    
    puts("请输入停车场的容量:");
    cin>>n;
    puts("请输入停车一小时的费用:");
    cin>>cost;
    while(1)
    {
    
    
        showmain();
        cout<<"请输入您想进行的操作:"<<endl;
        int op;
        cin>>op;
        if(op==1) change_n();
        else if(op==2) add_car();
        else if(op==3) change_cost();
        else if(op==4) show_park();
        else if(op==5) show_road();
        else if(op==6) return 0;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/111239895