单链表增删改查火车信息
时间: 2023-07-12 12:21:23 浏览: 69
好的,下面是一个基于单链表的火车票信息管理系统的实现,包括增加、删除、修改和查询火车票信息的功能:
```python
class TrainTicket:
def __init__(self, train_num, start_station, end_station, date, price, next_ticket=None):
self.train_num = train_num
self.start_station = start_station
self.end_station = end_station
self.date = date
self.price = price
self.next_ticket = next_ticket
class TrainTicketLinkedList:
def __init__(self):
self.head = None
# 添加火车票信息
def add_ticket(self, train_num, start_station, end_station, date, price):
new_ticket = TrainTicket(train_num, start_station, end_station, date, price)
if self.head is None:
self.head = new_ticket
else:
current_ticket = self.head
while current_ticket.next_ticket is not None:
current_ticket = current_ticket.next_ticket
current_ticket.next_ticket = new_ticket
# 删除火车票信息
def remove_ticket(self, train_num):
if self.head is None:
return
if self.head.train_num == train_num:
self.head = self.head.next_ticket
return
current_ticket = self.head
while current_ticket.next_ticket is not None:
if current_ticket.next_ticket.train_num == train_num:
current_ticket.next_ticket = current_ticket.next_ticket.next_ticket
return
current_ticket = current_ticket.next_ticket
# 修改火车票信息
def update_ticket(self, train_num, start_station, end_station, date, price):
current_ticket = self.get_ticket(train_num)
if current_ticket is not None:
current_ticket.start_station = start_station
current_ticket.end_station = end_station
current_ticket.date = date
current_ticket.price = price
# 查询火车票信息
def get_ticket(self, train_num):
current_ticket = self.head
while current_ticket is not None:
if current_ticket.train_num == train_num:
return current_ticket
current_ticket = current_ticket.next_ticket
return None
# 打印所有火车票信息
def display_tickets(self):
if self.head is None:
print("No train tickets available.")
return
current_ticket = self.head
while current_ticket is not None:
print("Train number:", current_ticket.train_num)
print("Start station:", current_ticket.start_station)
print("End station:", current_ticket.end_station)
print("Date:", current_ticket.date)
print("Price:", current_ticket.price)
print("-----------------------")
current_ticket = current_ticket.next_ticket
```
你可以通过创建一个 `TrainTicketLinkedList` 对象,来使用这些功能,例如:
```python
# 创建一个火车票信息管理系统
ticket_system = TrainTicketLinkedList()
# 增加火车票信息
ticket_system.add_ticket("G1234", "Beijing", "Shanghai", "2022-01-01", 500)
ticket_system.add_ticket("G2345", "Shanghai", "Guangzhou", "2022-01-02", 600)
ticket_system.add_ticket("G3456", "Beijing", "Guangzhou", "2022-01-03", 800)
# 打印所有火车票信息
ticket_system.display_tickets()
# 查询火车票信息
ticket = ticket_system.get_ticket("G2345")
if ticket is not None:
print("Train number:", ticket.train_num)
print("Start station:", ticket.start_station)
print("End station:", ticket.end_station)
print("Date:", ticket.date)
print("Price:", ticket.price)
else:
print("Train ticket not found.")
# 修改火车票信息
ticket_system.update_ticket("G3456", "Beijing", "Shenzhen", "2022-01-03", 900)
# 删除火车票信息
ticket_system.remove_ticket("G1234")
# 打印所有火车票信息
ticket_system.display_tickets()
```
输出结果如下:
```
Train number: G1234
Start station: Beijing
End station: Shanghai
Date: 2022-01-01
Price: 500
-----------------------
Train number: G2345
Start station: Shanghai
End station: Guangzhou
Date: 2022-01-02
Price: 600
-----------------------
Train number: G3456
Start station: Beijing
End station: Guangzhou
Date: 2022-01-03
Price: 800
-----------------------
Train number: G2345
Start station: Shanghai
End station: Guangzhou
Date: 2022-01-02
Price: 600
Train number: G3456
Start station: Beijing
End station: Shenzhen
Date: 2022-01-03
Price: 900
-----------------------
```
阅读全文