7-2 停车场管理
设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的先后次序依次从停车场最里面向大门口处停放 (即最先到达的第一辆车停放在停车场的最里面) 。如果停车场已放满n辆车,则以后到达的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车可以进入停车场。停车场内如有某辆车要开走,则在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费,停留在便道上的车不收停车费。编写程序对该停车场进行管理。
输入格式:
先输入一个整数n(n<=10),再输入若干组数据,每组数据包括三个数据项:汽车到达或离开的信息(A表示到达、D表示离开、E表示结束)、汽车号码、汽车到达或离开的时刻。
输出格式:
若有车辆到达,则输出该汽车的停车位置;若有车辆离开,则输出该汽车在停车场内停留的时间。如果汔车号码不存在,输出the car not in park
输入样例:
3
A 1 1
A 2 2
A 3 3
D 1 4
A 4 5
A 5 6
D 4 7
D 5 8
E 0 0
输出样例:
car#1 in parking space #1
car#2 in parking space #2
car#3 in parking space #3
car#1 out,parking time 3
car#4 in parking space #3
car#5 waiting
car#4 out,parking time 2
car#5 in parking space #3
car#5 out,parking time 1
有一点小小的复杂,大概思路是用一个结构体储存每个车的信息,用一个链表记录停车,用一个数组记录等待的车
其实最好的是和停车一起记录到同一个链表,不管是删除还是停入都比较方便,但相应代码也更加复杂,要另设一个记录max_number的结点位置,或者是用队列实现(符合先进先出),然而对于这道题的测试点没有这个就不考虑
这道题如果再考难一点比如说记录一个车出来时有哪几个车要让位置,那么就可以用栈来实现,因为我们知道栈是先进后出的,我第一遍看题时就想用栈来实现,但后面发现考点不在这
不用链表,而只用数组来实现这道题也可以,但是对于数组删除一个元素的时间复杂度相对于链表还是比较大的,效率太低
代码实现如下
一定要注意传入的是指针的指针,就是传入头指针的指针才能修改头指针,想应的传入也要取地址&,修改时也要取*,还有注意访问空指针会引发很大且不易察觉的问题,一定要单独考虑空指针和头节点的问题
#include <stdio.h>
#include <stdlib.h>
//结构定义
typedef struct car{
int name;
int car_in;
int car_out;
} strcar;
typedef struct node *List;
struct node{
strcar data;
List next;
};
//函数实现
//在结尾插入
//注意传入的是指针的地址,而不是指针,才能改变
void Create(List *head, strcar X){
if(*head == NULL){
*head = (List)malloc(sizeof(struct node));
(*head)->data = X;
(*head)->next = NULL;
}
else{
List P = *head;
while(P->next){
P = P->next;
}
List L = (List)malloc(sizeof(struct node));
L->data = X;
L->next = NULL;
P->next = L;
}
}
//判断停了几个车
int judge(List head){
List L = head;
int count = 0;
while(L){
L = L->next;
count++;
}
return count;
}
//找到要删除的车,返回该车的位置
List Find(List head, int X, int out){
List L = head;
while(L && L->data.name != X){
L = L->next;
}//避免访问空指针
if(L){
L->data.car_out = out;
}//避免访问空指针
return L;
}
//删除指定位置的车
void Delete(List *head, List delet){
if(*head == delet)
{
*head = delet->next;
} //单独考虑第一个结点删除的情况
else
{
List L = *head;
while(L && L->next != delet){
L = L->next;
}
if(L){
L->next = delet->next;
}
}
free(delet);//释放内存
}
int main(){
int max_number;
scanf("%d", &max_number);
char c;
int a, b;
List head = NULL;
strcar wait[10];//待停入的车
int i = -1;//记停入车的位置
scanf(" %c %d %d", &c, &a, &b);
while( c != 'E'){
//停车
if( c == 'A'){
int part_location = judge(head) + 1;
if(part_location > max_number){
printf("car#%d waiting\n", a);
wait[++i].name = a;
}
else{
printf("car#%d in parking space #%d\n", a, part_location);
strcar Car;
Car.name = a;
Car.car_in = b;
Create(&head, Car);
}
}
//离开
if( c == 'D'){
int time = b;
List delet_find = Find(head, a, b);
if(!delet_find){
printf("the car not in park\n");
}
else{
printf("car#%d out, parking time %d\n", a, (delet_find->data.car_out) - (delet_find->data.car_in));
Delete(&head, delet_find);
}
//排队的车进去
if(i != -1){
int part_location = judge(head) + 1;
printf("car#%d in parking space #%d\n", wait[i].name, part_location);
strcar Car;
Car.name = wait[i].name;
Car.car_in = time;
i--;
Create(&head, Car);
}
}
scanf(" %c %d %d", &c, &a, &b);
}
return 0;
}