1、停车厂管理系统数据结构与算法设计
队列代码:
#ifndef LinkedQueue_
#define LinkedQueue_
#include "node.h"
#include "xcept.h"
template
class LinkedQueue {
public:
LinkedQueue() {front = rear = 0,pos=0;}
~LinkedQueue();
LinkedQueue& Add(T& x,T& y);
LinkedQueue& Dele
2、te(T& x,T& y);
/********************************************/
int Position()
{
return pos;
} //得到车在队列中的位置
private:
Node *front;
Node *rear;
int pos;
/**************************************************/
};
template
LinkedQueu
3、e::~LinkedQueue()
{// Queue destructor. Delete all nodes.
Node *next;
while (front) {
next = front->link;
delete front;
front = next;
}
}
template
LinkedQueue& LinkedQueue::Add(T& x,T& y)
{
Node *p = new Node;
p->da
4、ta=x;
p->time=y;
p->link=0;
pos++;
if (front)
{
rear->link = p;
}
else front = p;
rear = p;
return *this;
}
template
LinkedQueue& LinkedQueue::Delete(T& x,T& y)
{
x=front->data;
y=front->time;
pos--;
Node *p = fro
5、nt;
front = front->link;
delete p;
return *this;
}
#endif
节点代码:
#ifndef Node_
#define Node_
template class LinkedQueue;
template
class Node {
friend LinkedQueue;
public:
T data;
T time;
Node *link;
};
#endif
栈的代码:
#ifndef St
6、ack_
#define Stack_
#include "xcept.h"
template
class Stack {
// LIFO objects
public:
Stack(int MaxStackSize = 10);
~Stack() {delete [] stack;}
bool IsEmpty() const {return top == -1;}
bool IsFull() const {return top == MaxTop;}
T Top() const;
Stack7、>& Add(const T& x);
Stack& Delete(T& x);
int top; // current top of stack
int MaxTop; // max value for top
T *stack; // element array
};
template
Stack::Stack(int MaxStackSize)
{// Stack constructor.
MaxTop = MaxStackSize - 1;
stack = new T[MaxSt
8、ackSize];
top = -1;
}
template
T Stack::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds(); // Top fails
return stack[top];
}
template
Stack& Stack::Add(const T& x)
{// Add x to stack.
if (IsFull()) throw NoMem(); // add fails
stac
9、k[++top] = x;
return *this;
}
template
Stack& Stack::Delete(T& x)
{// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds(); // delete fails
x = stack[top--];
return *this;
}
#endif
// 停车厂管理.cpp : Defines the entry point for the console application.
主要代
10、码:
#include "stdafx.h"
#include
#include "lqueue.h"
#include "stack.h"
#include "node.h"
void main()
{
LinkedQueue L; //便道停车队列
Stack>> S(2); //停车厂栈
Stack>> TemS(2); //临时栈
cout<<" --------------停车厂管理系统-------------
11、"<12、 --------------车辆到达请输入A-------------------"<13、1)
{
char in;
int num;
int time;
cin>>in;
/**************************业务处理区*******************************/
switch(in)
{
case 'A':
Node n;
cout<<"请输入车牌号和到达时间: ";
cin>>num;
cin>>time;
n.data=num;
n.time=time;
n.link=NULL;
if(S.to
14、p<1)
{
S.Add(n);
cout<<"车辆在停车厂内的位置为:第"< tem;
tem=S.Top();
cout<<"请输入车牌号和离开时间: ";
cin
15、>>num>>time;
if(tem.data==num)
{
S.Delete(tem);
cout<<"车牌为"<16、"<<"(备注:此时开始收费)"<=0)
{
S.Delete(tem);
if(tem.data==num)
{
cout<<"车牌为"<17、le(TemS.top>=0)
{
TemS.Delete(tem);
S.Add(tem);
}
//便道上的车进入停车厂
L.Delete(tem.data,tem.time);
tem.time=time;
S.Add(tem);
cout<<"便道上车牌为"<=0)
{
TemS.Delete(tem);
S.Add(tem);
}
}
break;
case 'E':
cout<<"谢谢您的使用!"<