火车售票单链表增删改查
时间: 2023-07-12 09:21:24 浏览: 88
好的,我可以帮你解答这个问题。
首先,我们可以定义一个火车售票的节点类,包含以下属性:
- 车次号
- 出发站点
- 到达站点
- 出发时间
- 到达时间
- 座位类型
- 票价
- 下一节点指针
接下来,我们可以定义一个火车售票单链表类,实现以下方法:
1. 增加节点:在链表末尾添加节点
2. 删除节点:根据车次号删除指定节点
3. 修改节点:根据车次号修改指定节点
4. 查询节点:根据车次号查询指定节点信息
代码实现如下:
```python
class TrainTicketNode:
def __init__(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price):
self.train_num = train_num
self.start_station = start_station
self.end_station = end_station
self.start_time = start_time
self.end_time = end_time
self.seat_type = seat_type
self.ticket_price = ticket_price
self.next = None
class TrainTicketLinkedList:
def __init__(self):
self.head = None
# 添加节点
def add_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price):
new_node = TrainTicketNode(train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price)
if self.head is None:
self.head = new_node
else:
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
current_node.next = new_node
# 删除节点
def delete_node(self, train_num):
if self.head is None:
print("链表为空")
else:
if self.head.train_num == train_num:
self.head = self.head.next
else:
current_node = self.head
while current_node.next is not None and current_node.next.train_num != train_num:
current_node = current_node.next
if current_node.next is None:
print("未找到该车次信息")
else:
current_node.next = current_node.next.next
# 修改节点
def modify_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price):
if self.head is None:
print("链表为空")
else:
current_node = self.head
while current_node is not None and current_node.train_num != train_num:
current_node = current_node.next
if current_node is None:
print("未找到该车次信息")
else:
current_node.start_station = start_station
current_node.end_station = end_station
current_node.start_time = start_time
current_node.end_time = end_time
current_node.seat_type = seat_type
current_node.ticket_price = ticket_price
# 查询节点
def search_node(self, train_num):
if self.head is None:
print("链表为空")
else:
current_node = self.head
while current_node is not None and current_node.train_num != train_num:
current_node = current_node.next
if current_node is None:
print("未找到该车次信息")
else:
print("车次号:{}\n出发站点:{}\n到达站点:{}\n出发时间:{}\n到达时间:{}\n座位类型:{}\n票价:{}".format(
current_node.train_num, current_node.start_station, current_node.end_station, current_node.start_time,
current_node.end_time, current_node.seat_type, current_node.ticket_price))
```
这样,我们就实现了火车售票单链表的增删改查功能。
阅读全文