用单链表实现宾馆客房管理系统
时间: 2023-11-18 11:05:24 浏览: 64
宾馆客房管理系统可以使用单链表来实现,下面是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义房间状态常量
#define EMPTY 0 // 空房间
#define OCCUPY 1 // 占用房间
// 定义房间结构体
struct Room {
int id; // 房间编号
int status; // 房间状态
int price; // 房间价格
char name[20]; // 房客姓名
struct Room *next; // 指向下一个房间的指针
};
// 创建空链表
struct Room *createList() {
struct Room *head = (struct Room *) malloc(sizeof(struct Room));
head->next = NULL;
return head;
}
// 添加房间
void addRoom(struct Room *head, int id, int price) {
struct Room *room = (struct Room *) malloc(sizeof(struct Room));
room->id = id;
room->status = EMPTY;
room->price = price;
room->next = head->next;
head->next = room;
}
// 查找房间
struct Room *findRoom(struct Room *head, int id) {
struct Room *room = head->next;
while (room != NULL) {
if (room->id == id) {
return room;
}
room = room->next;
}
return NULL;
}
// 获得房间状态字符串
char *getRoomStatus(int status) {
switch (status) {
case EMPTY:
return "空房间";
case OCCUPY:
return "占用房间";
default:
return "未知状态";
}
}
// 显示所有房间信息
void showRooms(struct Room *head) {
printf("房间编号\t房间状态\t房间价格\t房客姓名\n");
struct Room *room = head->next;
while (room != NULL) {
printf("%d\t\t%s\t\t%d\t\t%s\n", room->id, getRoomStatus(room->status), room->price, room->name);
room = room->next;
}
}
// 预订房间
void reserveRoom(struct Room *head, int id, char *name) {
struct Room *room = findRoom(head, id);
if (room != NULL) {
if (room->status == EMPTY) {
room->status = OCCUPY;
strcpy(room->name, name);
printf("预订成功!\n");
} else {
printf("该房间已被占用!\n");
}
} else {
printf("该房间不存在!\n");
}
}
// 退房
void checkoutRoom(struct Room *head, int id) {
struct Room *room = findRoom(head, id);
if (room != NULL) {
if (room->status == OCCUPY) {
room->status = EMPTY;
memset(room->name, 0, sizeof(room->name));
printf("退房成功!\n");
} else {
printf("该房间未被占用!\n");
}
} else {
printf("该房间不存在!\n");
}
}
int main() {
// 创建空链表
struct Room *head = createList();
// 添加房间
addRoom(head, 101, 100);
addRoom(head, 102, 120);
addRoom(head, 103, 150);
// 显示所有房间信息
showRooms(head);
// 预订房间
reserveRoom(head, 101, "张三");
reserveRoom(head, 102, "李四");
reserveRoom(head, 104, "王五");
// 显示所有房间信息
showRooms(head);
// 退房
checkoutRoom(head, 101);
checkoutRoom(head, 102);
checkoutRoom(head, 105);
// 显示所有房间信息
showRooms(head);
return 0;
}
```
运行程序后,您可以按照提示进行操作,例如预订房间、退房等。该程序只是一个简单的示例,您可以根据实际需求进行修改和扩展。
相关推荐
![](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)
![](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)