编辑代码

#include <stdio.h>
#include <stdlib.h>

// 定义房间结构体
typedef struct Room {
    int roomNumber;     //房间号
    int capacity;       //容量
    int isAvailable;    //可用性
    struct Room* next;  //指向下一个房间的指针
} Room;

Room* head = NULL;  //定义头指针

// 创建新房间的函数
Room* createRoom(int roomNumber, int capacity, int isAvailable) {
    Room* newRoom = (Room*)malloc(sizeof(Room));  //为新房间分配内存
    if (newRoom == NULL) {
        printf("内存分配失败。房间未创建。\n");
        return NULL;
    }
    newRoom->roomNumber = roomNumber;  //设置房间号
    newRoom->capacity = capacity;      //设置容量
    newRoom->isAvailable = isAvailable;//设置可用性
    newRoom->next = NULL;              //设置下一个房间指针为NULL
    return newRoom;                    //返回新创建的房间
}

// 添加房间到链表的函数
void addRoom(int roomNumber, int capacity, int isAvailable) {
    Room* newRoom = createRoom(roomNumber, capacity, isAvailable);  //创建新房间
    if (newRoom == NULL) {
        return;
    }
    newRoom->next = head;  //将新房间的下一个房间设置为当前的头房间
    head = newRoom;        //将头房间设置为新房间
}

// 从链表中删除房间的函数
void deleteRoom(int roomNumber) {
    Room* temp = head, * prev = NULL;  //定义临时和前一个房间指针
    if (temp != NULL && temp->roomNumber == roomNumber) {  //如果头房间就是要删除的房间
        head = temp->next;  //将头房间设置为下一个房间
        free(temp);         //释放要删除的房间的内存
        return;
    }
    // 寻找要删除的房间
    while (temp != NULL && temp->roomNumber != roomNumber) {
        prev = temp;        //保存当前房间
        temp = temp->next;  //移动到下一个房间
    }
    if (temp == NULL) return;  //如果没有找到要删除的房间,直接返回
    prev->next = temp->next;   //将前一个房间的下一个房间设置为要删除的房间的下一个房间
    free(temp);                //释放要删除的房间的内存
}

// 更新房间信息的函数
void updateRoom(int roomNumber, int capacity, int isAvailable) {
    Room* temp = head;  //定义临时房间指针
    // 遍历链表寻找要更新的房间
    while (temp != NULL) {
        if (temp->roomNumber == roomNumber) {  //如果找到了要更新的房间
            temp->capacity = capacity;         //更新容量
            temp->isAvailable = isAvailable;   //更新可用性
            return;
        }
        temp = temp->next;  //移动到下一个房间
    }
}

// 打印所有房间信息的函数
void printRooms() {
    Room* temp = head;  //定义临时房间指针
    // 遍历链表打印每个房间的信息
    while (temp != NULL) {
        printf("Room Number: %d\n", temp->roomNumber);
        printf("Capacity: %d\n", temp->capacity);
        printf("Availability: %d\n", temp->isAvailable);
        temp = temp->next;  //移动到下一个房间
    }
}

// 主函数
int main() {
    int choice, roomNumber, capacity, isAvailable;
    while (1) {
      printf("**********************\n");
      printf("* ");printf("1. 添加房间信息    ");  printf("*\n");
      printf("* ");printf("2. 删除房间信息    ");  printf("*\n");
      printf("* ");printf("3. 更新房间信息    ");  printf("*\n");
      printf("* ");printf("4. 打印房间信息    ");  printf("*\n"); 
      printf("* ");printf("5. 退出酒店管理系统");  printf("*\n");
      printf("**********************\n");
      printf("请输入选择: \0\0");        
      
        scanf("%d", &choice);  //读取用户的选择
        switch (choice) {  //根据用户的选择执行相应的操作
        case 1:
            printf("请输入房间号,容量,是否可用(可用1不可用0): ");  //提示用户输入房间信息
            scanf("%d %d %d", &roomNumber, &capacity, &isAvailable);  //读取用户输入的房间信息
            addRoom(roomNumber, capacity, isAvailable);  //添加房间
            break;
        case 2:
            printf("输入要删除的房间号: ");  
            scanf("%d", &roomNumber);  //读取用户输入的房间号
            deleteRoom(roomNumber);  //删除房间
            break;
        case 3:
            printf("输入要更新的房间号: ");  
            scanf("%d", &roomNumber);  //读取用户输入的房间号
            printf("输入新的房间信息: ");  
            scanf("%d %d", &capacity, &isAvailable);  //读取用户输入的新房间信息
            updateRoom(roomNumber, capacity, isAvailable);  //更新房间
            break;
        case 4:
            printRooms();  //打印所有房间信息
            break;
        case 5:
            exit(0);  //退出程序
        default:
            printf("选择无效\n");  //如果用户输入的选择无效,打印错误信息
        }
    }
    return 0;
}