单链表实现宾馆客房管理系统的各类客房收入情况的源代码
时间: 2023-11-18 08:06:27 浏览: 74
以下是单链表实现宾馆客房管理系统的各类客房收入情况的源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义客房结构体
typedef struct Room {
int id; // 客房编号
char type[20]; // 客房类型
int price; // 客房价格
int income; // 客房收入
struct Room* next; // 指向下一个客房的指针
} Room;
// 定义单链表结构体
typedef struct List {
Room* head; // 指向链表头结点的指针
int len; // 链表长度
} List;
// 初始化单链表
List* initList() {
List* list = (List*)malloc(sizeof(List));
list->head = NULL;
list->len = 0;
return list;
}
// 创建新客房
Room* createRoom(int id, char* type, int price) {
Room* room = (Room*)malloc(sizeof(Room));
room->id = id;
strcpy(room->type, type);
room->price = price;
room->income = 0;
room->next = NULL;
return room;
}
// 在单链表尾部添加新客房
void addRoom(List* list, Room* room) {
if (list == NULL || room == NULL) {
return;
}
Room* p = list->head;
if (p == NULL) {
list->head = room;
} else {
while (p->next != NULL) {
p = p->next;
}
p->next = room;
}
list->len++;
}
// 根据客房编号查找客房
Room* findRoomById(List* list, int id) {
if (list == NULL) {
return NULL;
}
Room* p = list->head;
while (p != NULL) {
if (p->id == id) {
return p;
}
p = p->next;
}
return NULL;
}
// 更新客房收入
void updateRoomIncome(List* list, int id, int income) {
Room* room = findRoomById(list, id);
if (room != NULL) {
room->income += income;
}
}
// 输出所有客房信息
void printRooms(List* list) {
printf("客房编号\t客房类型\t客房价格\t客房收入\n");
Room* p = list->head;
while (p != NULL) {
printf("%d\t\t%s\t\t%d\t\t%d\n", p->id, p->type, p->price, p->income);
p = p->next;
}
}
int main() {
List* list = initList();
// 添加客房
addRoom(list, createRoom(101, "单人间", 150));
addRoom(list, createRoom(102, "双人间", 200));
addRoom(list, createRoom(103, "豪华套房", 500));
// 更新客房收入
updateRoomIncome(list, 101, 300);
updateRoomIncome(list, 102, 600);
updateRoomIncome(list, 103, 1000);
// 输出客房信息
printRooms(list);
return 0;
}
```
上述代码实现了一个简单的宾馆客房管理系统,可以在单链表中存储客房信息,并且能够根据客房编号进行查询和更新客房收入。
阅读全文