编写停车场管理程序用数据结构c语言代码写
时间: 2024-11-29 11:32:28 浏览: 162
编写一个简单的停车场管理程序,我们可以考虑使用链表数据结构来存储车辆信息,包括每个停车位的状态(是否已被占用)、车辆信息等。这里我会提供一个基础的示例,使用单链表来模拟。请注意,这个例子非常简陋,实际应用中需要更复杂的数据结构和错误处理。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct ParkingLot {
int slotNumber;
char vehicleInfo[50];
bool isOccupied; // 停车位状态,0表示空闲,1表示已占用
} ParkSlot;
typedef struct ParkingLotNode {
ParkSlot parkingData;
struct ParkingLotNode* next;
} ParkSlotNode;
// 初始化节点
ParkSlotNode* createSlot(int slotNumber) {
ParkSlotNode* newNode = (ParkSlotNode*)malloc(sizeof(ParkSlotNode));
newNode->parkingData.slotNumber = slotNumber;
newNode->parkingData.isOccupied = 0;
newNode->next = NULL;
return newNode;
}
// 添加车辆到停车位
void addVehicle(ParkSlotNode** head, int slotNumber, char vehicleInfo[]) {
ParkSlotNode* newNode = createSlot(slotNumber);
newNode->parkingData.vehicleInfo = vehicleInfo;
if (*head == NULL) {
*head = newNode;
} else {
ParkSlotNode* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 查看停车位状态
void checkStatus(ParkSlotNode* node) {
if (node->parkingData.isOccupied) {
printf("停车位 %d 已经被占用了,车辆信息:%s\n", node->parkingData.slotNumber, node->parkingData.vehicleInfo);
} else {
printf("停车位 %d 空闲。\n", node->parkingData.slotNumber);
}
}
int main() {
ParkSlotNode* head = NULL;
// 添加一些车辆示例
addVehicle(&head, 1, "小汽车");
addVehicle(&head, 2, "SUV");
// 检查停车位状态
checkStatus(head); // 打印出所有车位的信息
return 0;
}
```
阅读全文
相关推荐















