请使用C++写一个酒店入住的增删改查,要求使用数据结构树,链表等,请写出代码
时间: 2023-03-08 09:12:49 浏览: 59
我可以提供一个简单的C语言代码示例,用于实现酒店入住的增删改查:#include <stdio.h>
#include <stdlib.h>struct hotel {
int room_id;
char name[20];
};// 使用链表结构存储酒店入住信息
struct node {
struct hotel data;
struct node *next;
};// 增加房间入住信息
void addNode(struct node *head, struct hotel data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head->next;
head->next = newNode;
}// 删除房间信息
struct node* deleteNode(struct node *head, int room_id) {
struct node *prev, *curr;
prev = head;
curr = head->next;
while (curr != NULL) {
if (curr->data.room_id == room_id) {
prev->next = curr->next;
free(curr);
return head;
}
prev = curr;
curr = curr->next;
}
return head;
}// 修改房间信息
void modifyNode(struct node *head, int room_id, char *name) {
struct node *curr = head->next;
while (curr != NULL) {
if (curr->data.room_id == room_id) {
strcpy(curr->data.name, name);
}
curr = curr->next;
}
}// 查询房间信息
struct node* searchNode(struct node *head, int room_id) {
struct node *curr = head->next;
while (curr != NULL) {
if (curr->data.room_id == room_id) {
return curr;
}
curr = curr->next;
}
return NULL;
}int main() {
// 使用头指针创建链表
struct node *head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
// 模拟增加入住信息
struct hotel h1 = {1001, "张三"};
addNode(head, h1);
struct hotel h2 = {1002, "李四"};
addNode(head, h2);
// 模拟删除入住信息
deleteNode(head, 1001);
// 修改入住信息
modifyNode(head, 1002, "王五");
// 查询房间信息
struct node *res = searchNode(head, 1002);
if (res != NULL) {
printf("room_id=%d,name=%s\n", res->data.room_id, res->data.name);
}
return 0;
}