logo资料库

停车场管理问题堆栈和队列应用数据结构课程设计.doc

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
#include #include using namespace std; #define MAX_SIZE 2//停车场能够容纳的车的数量 #define FARE 5//表示停车场的收费为每小时 FARE 元 int CountForStack=0;// 此全局变量用来计数堆栈现有的车数 int CountForQueue=0;// 此全局变量用来计数队列现有的车数 typedef struct//这个节点用来保存每辆车的信息 { char Condition ;//用来表示“到达”或者“离开”的状态," "表示还没有到达,也 没有离开 int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达 int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开 int License;// 记录车牌号 }CarNode; typedef struct //栈的定义 { CarNode *base;//栈底指针,指向 0 CarNode *top;//栈顶指针,如果指向 0,说明栈为空 int stacksize;//栈的容量大小 }CarStack; typedef struct QNode//队列节点的定义 { char Condition ;//用来表示“到达”或者“离开”的状态," "表示还没有到达,也 没有离开 int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达 int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开 int License;// 记录车牌号 QNode *next;//指向下一个节点的指针 }QNode; typedef struct// 队列的定义 { QNode *front;//队头指针 QNode *rear;//队尾指针 }Queue; bool InitStack(CarStack &S)//此函数用来初始化栈 { S.base =(CarNode *)malloc(MAX_SIZE*sizeof(CarNode)); if(!S.base ) { cout<<"内存分配失败!"<
} S.top =S.base ; S.stacksize =MAX_SIZE; return true; } bool InitQueue(Queue &Q)//此函数用来初始化队列 { Q.front =(QNode *)malloc(sizeof(QNode)); if(!Q.front ) { cout<<"内存分配失败!"<next =0;//下一个节点指空 return true; } bool EnQueue(Queue &Q,QNode &qnode)//此函数用来入队一个节点 { QNode *p=(QNode *)malloc(sizeof(QNode)); if(!p) { cout<<"内存分配失败!"<ArriveTime =qnode.ArriveTime ; p->Condition =qnode.Condition ; p->LeaveTime =qnode.LeaveTime ; p->License =qnode.License ; p->next =0; Q.rear ->next =p; Q.rear =p; return true; } bool DeQueue(Queue &Q,QNode &t)//此函数用来出队 { if(Q.front ==Q.rear ) { cout<<"队列为空!"<next ; t.ArriveTime =p->ArriveTime ; t.Condition =p->Condition ;
t.LeaveTime =p->LeaveTime ; t.License =p->License ; Q.front ->next =p->next ; if(Q.rear ==p)//如果 P 是指向最后一个出队的元素 Q.rear =Q.front ; free(p); return true; } void InitCarNode(CarNode &C,char condition,int arrivetime,int leavetime,int license)//本函数用来初始化一个 CarNode 节点 { C.ArriveTime =arrivetime; C.Condition =condition; C.LeaveTime =leavetime; C.License=license; } bool Push(CarStack &S,CarNode &car)//此函数用来入栈一个 CarNode 节点 { if(S.top -S.base >=S.stacksize ) { cout<<"此栈已满,不能压入新的信息"<
return true; } bool IsStackFull(CarStack &S)//此函数用来判断堆栈是否已满 { if(S.top -S.base >=S.stacksize ) return true; else return false; } bool IsStackEmputy(CarStack &S)//此函数用来判断堆栈是否为空 { if(S.top ==S.base ) return true; else return false; } bool IsQueueEmputy(Queue &Q)//此函数用来判断队列是否为空 { if(Q.front ==Q.rear ) return true; else return false; } bool SearchInStack(CarStack&S,int a)//a 表示要查找的车牌号,如果在停车场里面, 就返回 true { bool tag=false; if(!IsStackEmputy(S)) { CarNode *p=S.top-1 ; while(p!=S.base ) { if((*p).License ==a) tag=true; --p; } if((*p).License ==a) tag=true; } return tag; } bool SearchInQueue(Queue &Q,int a)//a 表示要查找的车牌号,如果在通道里面,就 返回 true {
bool tag=false; if(!IsQueueEmputy(Q))//如果队列非空 { QNode *p=Q.front->next ; while(p!=Q.rear) { if((*p).License ==a) tag=true; }//退出此 while 循环时 p 指向最后一个元素 if((*p).License ==a) tag=true; } return tag; } void InCar(CarStack &S,Queue &Q,int a1,int a2)//此函数用来表示进入车辆, 参数 a1 用来表示到达时间,参数 a2 表示车牌号码 { if(SearchInStack(S,a2)) { cout<<"车号"<
carnode.License =a2; Push(S,carnode); ++CountForStack; cout<<"车号:"<
Push(tempstack ,temp);//进入暂存栈 } while(!IsStackEmputy(tempstack))//倒出的车再次进入停车场 { Pop(tempstack,temp); Push(S,temp); } QNode tempqnode;//用来暂时保存从通道出来的汽车 if(tag1==true&&tag2==false&&tag3==false)//如果出车前停车场已满,并且通道不 为空,并且离开没有失败 { DeQueue(Q,tempqnode); --CountForQueue; temp.ArriveTime =a1 ; temp.Condition =tempqnode.Condition ; temp.LeaveTime =tempqnode.LeaveTime ; temp.License =tempqnode.License ; Push(S,temp); } if(tag2==true&&tag3==false)// 如果停车场没有满,并且离开成功 --CountForStack; 选 择 菜 单 请 按 键 选 择 } void showmenu(CarStack &S,Queue &Q) { cout<<"****************************** *******************************"<>tag; while(tag!='A'&&tag!='D'&&tag!='E') cin>>tag; int a1; unsigned int a2; switch(tag) { case 'A': cout<<"请输入到达的车号"<>a1; cout<<"请输入到达时间"<>a2; InCar(S,Q,a2,a1);
break; case 'D': cout<<"请输入离开的车号"<>a1; cout<<"请输入离开的时间"<>a2; OutCar(S,Q,a2,a1); break; case 'E': return ; break; } char ch; cout<<"******************* 按 E/e 退 出 , 按 任 意 键 返 回 菜 单 **************************"<>ch; if(ch!='E'&&ch!='e') showmenu(S,Q); } void main() { CarStack carstack; InitStack(carstack);// 建立并且初始化用于停车场的堆栈 Queue carqueue; InitQueue(carqueue);//建立并且初始化用于通道的队列 showmenu(carstack,carqueue); }
分享到:
收藏