#include
#include
/**
* 具体要求请看C语言数据结构课程设计 3.4.1:停车场管理系统
* 思路:
* 进:
* 1. 车按照顺序先进去通道(入队列)
* 2. 给出指定,让先进去通道的车进入停车场(先出队列后入栈)
* 出:
* 1. 最后一个进停车场的车(栈头)出栈,进入队列(入队列)
* 2. 车出队列
* 3. 至于停车时间,可以根据该车入栈和出栈的位置差来决定 / 或者搞一个时间函数记录(我选择前者)
*/
/**
* 队列使用链表实现
* 使用队列模拟通道
* 也就是车在排队,其实就是车的链表,使用 id 记录车的信息
*/
typedef struct Cars{
int id ;
struct Cars *next;
}*Cars,Car;
/**
* 栈使用顺序表实现
* 使用栈模拟停车场
*/
typedef struct Parking{
int carId[50];
int top;
}*Parking;
//初始化停车场
Parking initParking(){
Parking parking;
parking = malloc(sizeof(Parking));
if (parking == NULL){
printf("停车场初始化失败!");
return NULL;
}
parking->top = 0;
return parking;
}
//初始化车链表的表头
Cars initCar(){
Cars cars ;
cars = malloc(sizeof(Cars));
if (cars == NULL){
printf("通道初始化失败!");
return NULL;
}
cars->id = 0;
cars->next = NULL;
return cars;
}
//车进入通道,也就是形成链表
void inQueue(Cars cars){
Cars newCar;
newCar = malloc(sizeof(Car));
if (newCar == NULL){
printf("车进入通道失败");
return;
}
//找到链表尾部
while (cars->next != NULL){
cars = cars->next;
}
newCar->id = cars->id + 1;
newCar->next = NULL;
cars->next = newCar;
printf("\n",newCar->id);
}
//车出通道进停车场
void inPark(Parking parking,Cars cars){
/*while (cars->next != NULL){
printf("这次通道的车:%d\n",cars->id);
cars = cars->next;
}*/
if (cars->next == NULL){
printf("\n");
return;
}
if (parking->top == 49){
printf("\n");
return;
}
//车的链表头进入停车场
Cars temp = cars->next;
printf("%d---\n",temp->id);
parking->carId[parking->top+1] = temp->id;
printf("%d---\n",parking->top+1);
parking->top ++;
cars->next = temp->next;
printf("\n",temp->id);
free(temp);
}
//车出停车场进入通道
void outPark(Parking parking,Cars cars){
if (parking->top == 0){
printf("\n");
return;
}
//出栈
int id = parking->carId[parking->top];
parking->top = parking->top - 1;
while (cars->next != NULL){
cars = cars->next;
}
//进队列
Cars c = malloc(sizeof(Cars));
c->id = id;
c->next = NULL;
cars->next = c;
printf("\n",id);
}
//车出通道
void outQueue(Cars cars){
if (cars->next == NULL){
printf("\n");
return;
}
Cars temp = cars->next;
cars->next = temp->next;
printf("\n",temp->id);
free(temp);
}
//查看此时通道的车
void showQueue(Cars cars){
if (cars->next == NULL){
printf("\n");
return;
}
while (cars->next != NULL){
printf("\n",cars->next->id);
cars = cars->next;
}
}
//查看停车场的车
void showParking(Parking car){
if (car->top == 0){
printf("\n");
return;
}
for (int i = 1; i <= car->top; ++i) {
printf("\n",car->carId[i]);
}
}
// 菜单
void menu(){
printf("\n*********** 欢迎来到麻花的停车场 **********\n");
printf("-- 1.车辆进入通道\n");
printf("-- 2.车辆进入停车场\n");
printf("-- 3.车辆离开停车场\n");
printf("-- 4.车辆离开通道\n");
printf("-- 5.查看当前通道车辆\n");
printf("-- 6.查看当前停车场车辆\n");
printf("-- 7.退出程序\n");
printf("---请选择你的操作[1-7]:");
}
int main() {
Parking parking ;
parking = initParking();
Cars cars = initCar();
int i;
while (1){
menu();
scanf("%d",&i);
switch (i){
case 1:
inQueue(cars);
break;
case 2:
inPark(parking,cars);
break;
case 3:
outPark(parking,cars);
break;
case 4:
outQueue(cars);
break;
case 5:
showQueue(cars);
break;
case 6:
showParking(parking);
break;
case 7:
printf("*** 欢迎使用 ***");
exit(0);
}
}
}