将上面的代码优化,优化条件为: 1.优化站点类,包含头结点(头结点标志为#+线路号),尾结点(尾结点标志为*+end) 2.优化站点类储存方式为双向链表 3.优化公交线路类,可以对单条公交线路进行增删改查 4.优化公交线路类函数,创建一个菜单函数, 5.优化公交线路类,可以对单条公交线路中的站点进行增删改查
时间: 2023-09-06 13:10:31 浏览: 48
以下是对上述代码的优化:
```
class Node:
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next
class BusRoute:
def __init__(self, route_no, start_station, end_station):
self.route_no = route_no
self.start_station = start_station
self.end_station = end_station
self.stations = Node('#+' + start_station)
self.stations.next = Node('*+end', self.stations)
def add_station(self, station_name, prev_station_name):
current = self.stations
while current.next.data != '*+end':
if current.next.data.split('+')[1] == prev_station_name:
new_station = Node(station_name, current, current.next)
current.next.prev = new_station
current.next = new_station
return True
current = current.next
return False
def remove_station(self, station_name):
current = self.stations
while current.next.data != '*+end':
if current.next.data.split('+')[1] == station_name:
current.next.next.prev = current
current.next = current.next.next
return True
current = current.next
return False
def modify_station(self, old_station_name, new_station_name):
current = self.stations
while current.next.data != '*+end':
if current.next.data.split('+')[1] == old_station_name:
current.next.data = current.next.data.split('+')[0] + '+' + new_station_name
return True
current = current.next
return False
def search_station(self, station_name):
current = self.stations
while current.next.data != '*+end':
if current.next.data.split('+')[1] == station_name:
return True
current = current.next
return False
def display_stations(self):
current = self.stations
while current.next.data != '*+end':
print(current.next.data.split('+')[1])
current = current.next
def menu():
print("1. 创建公交线路")
print("2. 添加站点")
print("3. 删除站点")
print("4. 修改站点")
print("5. 查找站点")
print("6. 显示站点")
print("7. 退出")
choice = int(input("请输入您的选择:"))
return choice
routes = []
while True:
choice = menu()
if choice == 1:
route_no = input("请输入公交线路号:")
start_station = input("请输入起点站名:")
end_station = input("请输入终点站名:")
route = BusRoute(route_no, start_station, end_station)
routes.append(route)
print("公交线路创建成功!")
elif choice == 2:
route_no = input("请输入要添加站点的公交线路号:")
for route in routes:
if route.route_no == route_no:
station_name = input("请输入要添加的站点名:")
prev_station_name = input("请输入该站点前面的站点名:")
if route.add_station(station_name, prev_station_name):
print("站点添加成功!")
else:
print("站点添加失败,请检查输入是否正确。")
break
else:
print("未找到该公交线路,请检查输入是否正确。")
elif choice == 3:
route_no = input("请输入要删除站点的公交线路号:")
for route in routes:
if route.route_no == route_no:
station_name = input("请输入要删除的站点名:")
if route.remove_station(station_name):
print("站点删除成功!")
else:
print("站点删除失败,请检查输入是否正确。")
break
else:
print("未找到该公交线路,请检查输入是否正确。")
elif choice == 4:
route_no = input("请输入要修改站点的公交线路号:")
for route in routes:
if route.route_no == route_no:
old_station_name = input("请输入要修改的站点名:")
new_station_name = input("请输入新的站点名:")
if route.modify_station(old_station_name, new_station_name):
print("站点修改成功!")
else:
print("站点修改失败,请检查输入是否正确。")
break
else:
print("未找到该公交线路,请检查输入是否正确。")
elif choice == 5:
route_no = input("请输入要查找站点的公交线路号:")
for route in routes:
if route.route_no == route_no:
station_name = input("请输入要查找的站点名:")
if route.search_station(station_name):
print("站点存在!")
else:
print("站点不存在,请检查输入是否正确。")
break
else:
print("未找到该公交线路,请检查输入是否正确。")
elif choice == 6:
route_no = input("请输入要显示站点的公交线路号:")
for route in routes:
if route.route_no == route_no:
route.display_stations()
break
else:
print("未找到该公交线路,请检查输入是否正确。")
elif choice == 7:
print("程序已退出。")
break
else:
print("无效的选择,请重新输入。")
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)