栈和队列的应用——停车场模拟
内容要点
以栈模拟停车场,以队列模拟停车场外的便道,按照从终端读入的输入数据序列进行模拟管理。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间(单位是小时)和应交纳的费用(在便道上停留的时间不收费),假设停车费为每小时m元。等候在便道上的汽车可以直接从便道上开走,但此时排在它前面的汽车要先开走让路,然后再依次排到队尾。栈和队列均采用链表结构实现。
源码
#include<iostream>
using namespace std;
int e&#61;0;
class node
{
public:int number;int intime;int v;node *next;
};
class stack
{
public:node *top;stack(){top&#61;NULL;}~stack(){}void push(int x){node *s&#61;new node;s->number&#61;x;s->next&#61;top;top&#61;s;if(e<3){e&#43;&#43;;cout<<"请输入入场时间:";cin>>s->intime;}}int find(int num){node *p&#61;top;int i&#61;0;while(i<3&&p->number!&#61;num){i&#43;&#43;;p&#61;p->next;}if(i&#61;&#61;3){return 0;}else{return i&#43;1;}}void vei(int num,int r){node *p&#61;top;int i&#61;0;while(i<3){if(p->number&#61;&#61;num){p->v&#61;r;break;}else{i&#43;&#43;;p&#61;p->next;}}}int vv(int num){node *p&#61;top;int i&#61;0;while(i<3){if(p->number&#61;&#61;num)return p->v;else{i&#43;&#43;;p&#61;p->next;}}return 0;}int time(int num){node *p&#61;top;int i&#61;0;while(i<3){if(p->number&#61;&#61;num)return p->intime;else{i&#43;&#43;;p&#61;p->next;}}return 0;}void pop(int num){if(e<&#61;0)return ;node *p;if(top->number&#61;&#61;num){e--;p&#61;top;top&#61;top->next;delete p;return;}else if(top->next->number&#61;&#61;num){e--;p&#61;top->next;top->next&#61;p->next;top->v&#61;e;delete p;return;}else if(top->next->next->number&#61;&#61;num){e--;p&#61;top->next->next;top->v&#61;e;top->next->v&#61;e-1;delete p;return;}}
};
int ee&#61;0;
class node1
{
public:int number;int v;node1 *next;
};
class link
{
public:node1 *front,*rear;link(){node1 *s&#61;new node1; s->next&#61;NULL;front&#61;rear&#61;s;}~link(){}void enqueue(int num){ee&#43;&#43;;node1 *s&#61;new node1;s->number&#61;num;s->next&#61;NULL;rear->next&#61;s;rear&#61;s;s->v&#61;ee;}int vei(int num){node1 *s&#61;front;int k&#61;1;if(rear->number&#61;&#61;num){return rear->v;}while(s!&#61;rear){if(s->number&#61;&#61;num){k&#61;0;break;}else{s&#61;s->next;}}if(k&#61;&#61;0)return s->v;elsereturn 0;}int dequeue(){ee--;if(front&#61;&#61;rear)cout<<"下溢"<<endl;node1 *p;p&#61;front->next;int x&#61;p->number;front->next&#61;p->next;if(p->next&#61;&#61;NULL)rear&#61;front;delete p;return x;}
};
int n;stack m;int num;link mm;
void mean1()
{cout<<"请输入车牌号:";cin>>num;if(e<3){m.push(num);int d&#61;m.find(num);{cout<<"车牌号为"<<num<<"的车辆在停车场的第"<<e<<"位。"<<endl;m.vei(num,e);}}else{cout<<"当前停车场已满&#xff0c;将为您转入便道"<<endl;mm.enqueue(num);cout<<"车牌号为"<<num<<"的车辆在便道的第"<<ee<<"位。"<<endl;}
}
void mean2()
{int num;cout<<"请输入要取走车辆的车牌号&#xff1a;";cin>>num;if(m.find(num)){cout<<"车牌号为"<<num<<"的用户&#xff0c;请输入您的离开时间&#xff08;小时&#xff09;";int outtime;cin>>outtime;cout<<endl<<"您本次的停车场使用时间为"<<outtime-m.time(num)<<"小时,产生费用为"<<outtime-m.time(num)<<"*m元。"<<endl<<endl;m.pop(num);if(ee>0){int nu&#61;mm.dequeue();cout<<"车牌号为"<<nu<<"的用户您好&#xff01;现将您的车辆驶入停车场"<<endl;m.push(nu);}}else if(mm.vei(num)){cout<<"您未使用停车场&#xff0c;欢迎下次使用bye~"<<endl;int t&#61;mm.vei(num)-1;if(t&#61;&#61;0)mm.dequeue();else{while(t){ee--;int a&#61;mm.dequeue();mm.enqueue(a);ee&#43;&#43;;t--;}mm.dequeue();}}else if(m.find(num)&#61;&#61;0&&mm.vei(num)&#61;&#61;0){cout<<"该车辆既不在停车场也不在便道"<<endl;}
}
void mean3()
{cout<<"请输入要查找的车牌号&#xff1a;";cin>>num;int d&#61;m.find(num);if(m.vv(num)){cout<<"车牌号为"<<num<<"的车辆在停车场的第"<<m.vv(num)<<"位"<<endl;}else if(mm.vei(num)){cout<<"车牌号为"<<num<<"的车辆在便道的第"<<mm.vei(num)<<"位"<<endl;}else if(m.find(num)&#61;&#61;0&&mm.vei(num)&#61;&#61;0){cout<<"不存在该车辆"<<endl;}
}
int main()
{cout<<"功能选择:"<<endl;cout<<"1.停车&#xff08;显示停车位置&#xff09; 2.取车&#xff08;显示在停车场内时间和费用&#xff09; 3.查找车辆 0.退出系统"<<endl;while(cin>>n&&n){switch(n){case 1:mean1();
创新思维
在讨论课上提出过新的idea&#xff0c;例如将停车场和便道通过循环队列的形式实现&#xff0c;大概左右两边都是循环队列中间是一条过道 这个样子。
求点赞&#xff01;求关注&#xff01;一键三连&#xff01;&#xff01;&#xff01;