数据结构停车场c语言代码
时间: 2023-09-05 15:06:56 浏览: 106
以下是一个简单的停车场管理系统的C语言代码,使用了链表数据结构来实现:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 20
struct Car {
char licensePlate[MAX_SIZE];
struct Car* next;
};
struct ParkingLot {
int capacity;
struct Car* head;
};
void initParkingLot(struct ParkingLot* parkingLot, int capacity) {
parkingLot->capacity = capacity;
parkingLot->head = NULL;
}
int isFull(struct ParkingLot* parkingLot) {
int count = 0;
struct Car* node = parkingLot->head;
while(node != NULL) {
count++;
node = node->next;
}
return count >= parkingLot->capacity;
}
int isEmpty(struct ParkingLot* parkingLot) {
return parkingLot->head == NULL;
}
void parkCar(struct ParkingLot* parkingLot, char licensePlate[]) {
if(isFull(parkingLot)) {
printf("The parking lot is full, cannot park the car.\n");
return;
}
struct Car* node = (struct Car*) malloc(sizeof(struct Car));
strcpy(node->licensePlate, licensePlate);
node->next = parkingLot->head;
parkingLot->head = node;
printf("The car with license plate %s is parked successfully.\n", licensePlate);
}
void unparkCar(struct ParkingLot* parkingLot, char licensePlate[]) {
if(isEmpty(parkingLot)) {
printf("The parking lot is empty, cannot unpark the car.\n");
return;
}
struct Car* prev = NULL;
struct Car* curr = parkingLot->head;
while(curr != NULL) {
if(strcmp(curr->licensePlate, licensePlate) == 0) {
if(prev == NULL) {
parkingLot->head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("The car with license plate %s is unparked successfully.\n", licensePlate);
return;
}
prev = curr;
curr = curr->next;
}
printf("The car with license plate %s is not found in the parking lot.\n", licensePlate);
}
void displayParkingLot(struct ParkingLot* parkingLot) {
if(isEmpty(parkingLot)) {
printf("The parking lot is empty.\n");
return;
}
struct Car* node = parkingLot->head;
printf("The current parked cars in the parking lot are:\n");
while(node != NULL) {
printf("%s\n", node->licensePlate);
node = node->next;
}
}
int main() {
struct ParkingLot parkingLot;
initParkingLot(&parkingLot, 5);
parkCar(&parkingLot, "ABC123");
parkCar(&parkingLot, "DEF456");
displayParkingLot(&parkingLot);
unparkCar(&parkingLot, "ABC123");
displayParkingLot(&parkingLot);
parkCar(&parkingLot, "GHI789");
parkCar(&parkingLot, "JKL012");
parkCar(&parkingLot, "MNO345");
parkCar(&parkingLot, "PQR678");
parkCar(&parkingLot, "STU901");
displayParkingLot(&parkingLot);
unparkCar(&parkingLot, "GHI789");
unparkCar(&parkingLot, "XYZ999");
displayParkingLot(&parkingLot);
return 0;
}
```
这个简单的停车场管理系统可以实现三个基本操作:停车、取车和展示当前停车场内停车数量,使用链表数据结构来存储停车场内的车辆信息。
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)