数据结构——停车场代码
2012-08-29 12:00
274 查看
初学,写的有点困难,改了一遍又一遍,终于改好了,VC2010编译通过~~代码没有优化过。
#include <iostream>
#include <stdio.h>
using namespace std;
const int STACK_INIT_SIZE = 2;
const int TRUE = 1;
const int FALSE = 0;
typedef struct Car //定义汽车类型
{
int num;
int time;
}Car;
typedef struct SqStack{
Car *base;
Car *top;
}SqStack;
void InitStack(SqStack &S)
{
S.base = (Car *)malloc(STACK_INIT_SIZE * sizeof(Car));
S.top = S.base;
}
int IsEmpty(SqStack &S)
{
return(S.top == S.base?TRUE:FALSE);
}
int IsFull(SqStack &S)
{
return(S.top - S.base >= STACK_INIT_SIZE?TRUE:FALSE);
}
void Push(SqStack &S,int n,int t)
{
S.top->num = n;
S.top->time = t;
S.top++;
}
void Pop(SqStack &S,Car &car)
{
S.top--;
car = * S.top;
}
typedef struct QNode {
Car data;
struct QNode *next;
}QNode;
typedef struct LinkQueue {
QNode *front;
QNode *rear;
}LinkQueue;
void InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QNode *)malloc(sizeof(QNode));
if(!Q.front) exit (-1);
Q.front->next = NULL;
}
void EnQueue(LinkQueue &Q,int n,int t)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if(!p) exit (-1);
p->data.num = n;
p->data.time = t;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
void DeQueue(LinkQueue &Q,Car &car)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if (Q.front != Q.rear)
{
p=Q.front->next;
car = p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
}
}
int IsInStack(SqStack &S,int n)
{
Car *p=S.top;
while(p!=S.base)
{
p--;
if(p->num == n) break;
}
if(p->num == n) return 1;
else return 0;
}
int main()
{
SqStack S,R;
LinkQueue Q;
InitStack(S);
InitStack(R);
InitQueue(Q);
int j=0,k=0; // 停车场计数器,便道计数器
int time;
while(1)
{
cout << "请输入车辆信息:";
char State;
int n,t;
cin >> State >> n >> t;
if(State == 'A') //车辆进入
{
if(IsFull(S)) // 先判断车库是否满了
{
EnQueue(Q,n,t); // 放入便道里
k++; // 便道里的位置计数器
cout << "在便道中位置:" << k << endl;
}
else
{
Push(S,n,t); // 放入车库中
j++; // 车库的位置计数器
cout << "在车库中位置:" << j << endl;
}
}
else if(State == 'D') //车辆离开
{
if(IsEmpty(S))
{
cout << "车库为空!" << endl;
continue;
}
if(IsInStack(S,n)) //是否在车库中
{
Car *f=S.top;
f--;
while(n != f->num) f--;
if(t <= f->time)
{
cout << "输入时间错误!" << endl;
continue;
}
Car car;
Car *p=S.top;
p--; //辅助查找指针
while(n != p->num) //查找要出去的车辆
{
Car car1;
Pop(S,car1);
Push(R,car1.num,car1.time);
p--;
}
Pop(S,car);
j--;
time=t-car.time;
cout << n << "号车停了" << time << "分钟,需要收费" << endl;
while(!IsEmpty(R))
{
Car car2;
Pop(R,car2);
Push(S,car2.num,car2.time);
}
if(Q.rear!=Q.front)
{
Car car1;
DeQueue(Q,car1);
Push(S,car1.num,t);
j++;
k--;
}
}
else //在便道中
{
QNode *r=Q.front;
while(r->next->data.num != n)
{
r=r->next;
}
if(t <= r->next->data.time)
{
cout << "输入时间错误!" << endl;
continue;
}
Car car;
int a=Q.front->next->data.num;
r=r->next;
while(Q.front->next!=r)
{
DeQueue(Q,car);
EnQueue(Q,car.num,car.time);
}
DeQueue(Q,car);
k--;
time = t - car.time;
cout << n << "号车停了" << time << "分钟,不需要收费" << endl;
while(Q.front != Q.rear &&Q.front->next->data.num !=a && n!=a)
{
DeQueue(Q,car);
EnQueue(Q,car.num,car.time);
}
}
}
else break;
}
system("pause");
return 0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
const int STACK_INIT_SIZE = 2;
const int TRUE = 1;
const int FALSE = 0;
typedef struct Car //定义汽车类型
{
int num;
int time;
}Car;
typedef struct SqStack{
Car *base;
Car *top;
}SqStack;
void InitStack(SqStack &S)
{
S.base = (Car *)malloc(STACK_INIT_SIZE * sizeof(Car));
S.top = S.base;
}
int IsEmpty(SqStack &S)
{
return(S.top == S.base?TRUE:FALSE);
}
int IsFull(SqStack &S)
{
return(S.top - S.base >= STACK_INIT_SIZE?TRUE:FALSE);
}
void Push(SqStack &S,int n,int t)
{
S.top->num = n;
S.top->time = t;
S.top++;
}
void Pop(SqStack &S,Car &car)
{
S.top--;
car = * S.top;
}
typedef struct QNode {
Car data;
struct QNode *next;
}QNode;
typedef struct LinkQueue {
QNode *front;
QNode *rear;
}LinkQueue;
void InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QNode *)malloc(sizeof(QNode));
if(!Q.front) exit (-1);
Q.front->next = NULL;
}
void EnQueue(LinkQueue &Q,int n,int t)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if(!p) exit (-1);
p->data.num = n;
p->data.time = t;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
void DeQueue(LinkQueue &Q,Car &car)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if (Q.front != Q.rear)
{
p=Q.front->next;
car = p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
}
}
int IsInStack(SqStack &S,int n)
{
Car *p=S.top;
while(p!=S.base)
{
p--;
if(p->num == n) break;
}
if(p->num == n) return 1;
else return 0;
}
int main()
{
SqStack S,R;
LinkQueue Q;
InitStack(S);
InitStack(R);
InitQueue(Q);
int j=0,k=0; // 停车场计数器,便道计数器
int time;
while(1)
{
cout << "请输入车辆信息:";
char State;
int n,t;
cin >> State >> n >> t;
if(State == 'A') //车辆进入
{
if(IsFull(S)) // 先判断车库是否满了
{
EnQueue(Q,n,t); // 放入便道里
k++; // 便道里的位置计数器
cout << "在便道中位置:" << k << endl;
}
else
{
Push(S,n,t); // 放入车库中
j++; // 车库的位置计数器
cout << "在车库中位置:" << j << endl;
}
}
else if(State == 'D') //车辆离开
{
if(IsEmpty(S))
{
cout << "车库为空!" << endl;
continue;
}
if(IsInStack(S,n)) //是否在车库中
{
Car *f=S.top;
f--;
while(n != f->num) f--;
if(t <= f->time)
{
cout << "输入时间错误!" << endl;
continue;
}
Car car;
Car *p=S.top;
p--; //辅助查找指针
while(n != p->num) //查找要出去的车辆
{
Car car1;
Pop(S,car1);
Push(R,car1.num,car1.time);
p--;
}
Pop(S,car);
j--;
time=t-car.time;
cout << n << "号车停了" << time << "分钟,需要收费" << endl;
while(!IsEmpty(R))
{
Car car2;
Pop(R,car2);
Push(S,car2.num,car2.time);
}
if(Q.rear!=Q.front)
{
Car car1;
DeQueue(Q,car1);
Push(S,car1.num,t);
j++;
k--;
}
}
else //在便道中
{
QNode *r=Q.front;
while(r->next->data.num != n)
{
r=r->next;
}
if(t <= r->next->data.time)
{
cout << "输入时间错误!" << endl;
continue;
}
Car car;
int a=Q.front->next->data.num;
r=r->next;
while(Q.front->next!=r)
{
DeQueue(Q,car);
EnQueue(Q,car.num,car.time);
}
DeQueue(Q,car);
k--;
time = t - car.time;
cout << n << "号车停了" << time << "分钟,不需要收费" << endl;
while(Q.front != Q.rear &&Q.front->next->data.num !=a && n!=a)
{
DeQueue(Q,car);
EnQueue(Q,car.num,car.time);
}
}
}
else break;
}
system("pause");
return 0;
}
相关文章推荐
- 看数据结构写代码(50)伙伴系统
- C语言学习历程(十九) 数据结构完成停车场项目
- 看数据结构写代码(51) 广义表的头尾链式存储
- 看数据结构写代码(21) 稀疏矩阵(十字链表方式)
- 数据结构之链表、栈和队列 java代码实现
- 【转载】MP4文件数据结构以及提取部分信息的代码
- 【数据结构线性表查找】——顺序查找和折半查找详解和代码
- 浅说代码风格 - 数据结构课程设计
- C++代码,数据结构-拓扑算法
- 数据结构之完全二叉树――顺序存储结构(php代码实现)
- 【数据结构与算法】【排序】堆排序的代码实现
- 看数据结构写代码(39) 图的遍历(深搜和广搜)
- 数据结构——树小结代码
- 【数据结构与算法】【排序】归并排序的代码实现
- 数据结构之栈的初始化、创建、入栈、出栈、销毁-c++代码实现及运行实例结果
- 数据结构与逻辑代码(一)
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 看数据结构写代码(15)链式队列的实现(总结篇)
- Unity之关于代码--数据结构
- 数据结构与算法学习笔记之写链表代码的正确姿势(下)