用链表实现酒店信息管理系统的代码
时间: 2024-09-13 18:09:40 浏览: 47
在实现一个酒店信息管理系统的代码中,使用链表作为数据结构可以方便地进行酒店房间信息的增删改查操作。下面是一个简单的示例,使用单链表结构来管理酒店房间信息。
首先定义房间信息的数据结构:
```java
class RoomInfo {
int roomNumber; // 房间号
String type; // 房间类型
double price; // 房间价格
boolean isOccupied; // 房间是否被占用
public RoomInfo(int roomNumber, String type, double price) {
this.roomNumber = roomNumber;
this.type = type;
this.price = price;
this.isOccupied = false;
}
}
```
然后定义链表节点以及链表结构:
```java
class RoomNode {
RoomInfo data;
RoomNode next;
public RoomNode(RoomInfo data) {
this.data = data;
this.next = null;
}
}
class RoomLinkedList {
RoomNode head; // 链表头节点
public RoomLinkedList() {
this.head = null;
}
// 添加房间信息到链表
public void addRoom(RoomInfo roomInfo) {
RoomNode newNode = new RoomNode(roomInfo);
if (head == null) {
head = newNode;
} else {
RoomNode current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
// 根据房间号查找房间信息
public RoomInfo findRoomByNumber(int roomNumber) {
RoomNode current = head;
while (current != null) {
if (current.data.roomNumber == roomNumber) {
return current.data;
}
current = current.next;
}
return null;
}
// 删除房间信息
public void deleteRoom(int roomNumber) {
if (head == null) return;
if (head.data.roomNumber == roomNumber) {
head = head.next;
return;
}
RoomNode current = head;
while (current.next != null) {
if (current.next.data.roomNumber == roomNumber) {
current.next = current.next.next;
return;
}
current = current.next;
}
}
// 更多管理功能可以继续添加,比如修改房间信息,显示所有房间信息等
}
```
以上代码展示了如何使用链表实现基本的酒店信息管理功能,包括添加房间信息、查找房间信息、删除房间信息。实际的系统会更加复杂,可能还会包括房间预订、取消预订、客户管理等功能。
阅读全文