链表存储酒店管理系统
时间: 2023-10-03 15:11:26 浏览: 42
可以使用链表来存储酒店管理系统的信息。每个节点可以表示一个酒店,包含该酒店的相关信息,如酒店名称、地址、联系方式等。
链表的节点结构可以定义如下:
```cpp
struct Hotel {
string name;
string address;
string contact;
// 其他相关信息
Hotel* next; // 指向下一个节点的指针
};
```
通过这个节点结构,可以创建一个链表,每个节点保存一个酒店的信息,并通过`next`指针链接到下一个节点。
下面是一个简单的示例代码,展示如何使用链表存储酒店管理系统的信息:
```cpp
#include <iostream>
using namespace std;
struct Hotel {
string name;
string address;
string contact;
Hotel* next;
};
class HotelManagementSystem {
private:
Hotel* head; // 链表头指针
public:
HotelManagementSystem() {
head = NULL; // 初始化头指针为空
}
void addHotel(string name, string address, string contact) {
Hotel* newHotel = new Hotel; // 创建新节点
newHotel->name = name;
newHotel->address = address;
newHotel->contact = contact;
newHotel->next = NULL;
if (head == NULL) {
// 如果链表为空,将新节点设为头节点
head = newHotel;
} else {
// 遍历链表,找到最后一个节点
Hotel* current = head;
while (current->next != NULL) {
current = current->next;
}
// 将新节点加在最后一个节点的后面
current->next = newHotel;
}
}
void displayHotels() {
Hotel* current = head; while (current != NULL) {
// 打印每个酒店的信息
cout << "Hotel Name: " << current->name << endl;
cout << "Address: " << current->address << endl;
cout << "Contact: " << current->contact << endl;
current = current->next;
}
}
};
int main() {
HotelManagementSystem hms;
hms.addHotel("Hotel A", "Address A", "Contact A");
hms.addHotel("Hotel B", "Address B", "Contact B");
hms.addHotel("Hotel C", "Address C", "Contact C");
hms.displayHotels();
return 0;
}
```
这段代码演示了如何添加酒店信息并展示所有酒店的功能。你可以根据实际需求扩展该代码,添加其他功能,如搜索酒店、删除酒店等。