#include <stdlib.h>
#include <stdio.h>
#define STACK_SIZE 3
enum boolean{ ERROR,OVERFLOW,OK };
typedef unsigned int STATUS;
//--------------------------各个结构体定义-----------------------------------------------//
struct SNode
{
int car_num;
int arr_time;
};
struct QNode
{
int car_num;
int arr_time;
QNode* next;
};
struct Stack
{
SNode* base;
SNode* top;
int stacksize;
};
struct Queue
{
QNode* front;
QNode* rear;
};
//---------------------------------结构体Stack操作函数定义-----------------------------------------//
STATUS initStack(Stack &s)
{
s.base=(SNode*)malloc(STACK_SIZE*sizeof(SNode));
if(!s.base) return ERROR;
s.top=s.base;
s.stacksize=STACK_SIZE;
return OK;
}
STATUS push(Stack &s,SNode &car)
{
*s.top++=car;
return OK;
}
STATUS isfull(Stack &s)
{
if(s.top-s.base>=s.stacksize)
return OK;
return ERROR;
}
STATUS pop(Stack &s,SNode &car)
{
if(s.top==s.base) return ERROR;
car=*--s.top;
return OK;
}
STATUS Sisempty(Stack &s)
{
if(s.top==s.base)
return OK;
return ERROR;
}
//-----------------------------------------结构体Queue操作函数定义--------------------------------------------------//
STATUS initQueue(Queue &q)
{
q.front=q.rear=(QNode*)malloc(sizeof(QNode));
if(!q.front) exit(OVERFLOW);
q.front->next=NULL;
return OK;
}
STATUS enQueue(Queue &q,SNode &car)
{
QNode* qcar = (QNode*)malloc(sizeof(QNode));
if(!qcar) exit(OVERFLOW);
qcar->car_num=car.car_num;
qcar->arr_time=car.arr_time;
qcar->next=NULL;
q.rear->next=qcar;
q.rear = qcar;
return OK;
}
STATUS deQueue(Queue &q,SNode &car,int leav_time)
{
if(q.front==q.rear) return ERROR;
car.arr_time = leav_time;
QNode* temp = q.front->next;
car.car_num = temp->car_num;
q.front->next=temp->next;
free(temp);
return OK;
}
STATUS Qisempty(Queue &q)
{
if(q.front->next==NULL) return OK;
return ERROR;
}
void main()
{
char fir,te;
int sec,thi;
int fee = 0;
struct SNode car;
struct Stack box;
struct Stack temp;
struct Queue wait;
initStack(box);
initStack(temp);
initQueue(wait);
printf("enter the car's infor:");
scanf("%c %d %d",&fir,&sec,&thi);
te = getchar();
car.car_num = sec;
car.arr_time = thi;
while(thi!='E')
{
if(fir == 'A')
{
if(isfull(box))
{
enQueue(wait,car);
}
else
{
push(box,car);
}
}
else if(fir == 'D')
{
struct SNode tem_car;
struct SNode tem_car2;
pop(box,tem_car);
while(tem_car.car_num!=car.car_num)
{
push(temp,tem_car);
pop(box,tem_car);
}
while(!Sisempty(temp))
{
pop(temp,tem_car2);
push(box,tem_car2);
}
if(!Qisempty(wait))
{
deQueue(wait,tem_car2,thi);
push(box,tem_car2);
}
fee = thi - tem_car.arr_time;
printf("the fee of %d car is:%d\n",tem_car.car_num,fee);
}
printf("enter the car's infor:");
scanf("%c %d %d",&fir,&sec,&thi);
te = getchar();
car.car_num = sec;
car.arr_time = thi;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
停车场问题求解(经典算法)
共13个文件
pdb:2个
ncb:1个
pch:1个
4星 · 超过85%的资源 需积分: 47 44 下载量 75 浏览量
2009-09-10
21:50:05
上传
评论 2
收藏 161KB RAR 举报
温馨提示
停车场问题求解 停车场问题求解 停车场问题求解 停车场问题求解 停车场问题求解 停车场问题求解 停车场问题求解
资源推荐
资源详情
资源评论
收起资源包目录
.rar (13个子文件)
停车场问题
fee.dsw 531B
fee.ncb 41KB
Debug
vc60.pdb 52KB
fee.pdb 417KB
fee.exe 164KB
vc60.idb 33KB
fee.ilk 174KB
fee.obj 9KB
fee.pch 216KB
fee.plg 240B
fee.dsp 4KB
fee.opt 53KB
fee.cpp 3KB
共 13 条
- 1
资源评论
- bloodyhomme2015-08-04我可以说我还没看内容么。
- bingolov2014-06-10非常经典的停车场问题求解的方法,对于理解分布式帮助很大
sweeneyzang
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功