如何用链表类写一个车辆管理系统
时间: 2023-08-04 09:10:31 浏览: 49
车辆管理系统可以使用链表来实现。下面是一个简单的示例:
```python
class Vehicle:
def __init__(self, reg_no, make, model, year):
self.reg_no = reg_no
self.make = make
self.model = model
self.year = year
self.next = None
class VehicleList:
def __init__(self):
self.head = None
def add_vehicle(self, vehicle):
if self.head is None:
self.head = vehicle
else:
current = self.head
while current.next is not None:
current = current.next
current.next = vehicle
def find_vehicle(self, reg_no):
current = self.head
while current is not None:
if current.reg_no == reg_no:
return current
current = current.next
return None
def remove_vehicle(self, reg_no):
current = self.head
previous = None
while current is not None:
if current.reg_no == reg_no:
if previous is None:
self.head = current.next
else:
previous.next = current.next
return True
previous = current
current = current.next
return False
def display(self):
current = self.head
while current is not None:
print(f"{current.reg_no} {current.make} {current.model} {current.year}")
current = current.next
```
上面的代码中,`Vehicle` 类表示一个车辆,包含车牌号、制造商、型号和年份。`next` 属性表示下一个车辆节点的指针,用于构建链表。
`VehicleList` 类表示车辆列表,包含一个头节点 `head`,指向链表的第一个节点。`add_vehicle` 方法将新车辆加入到链表的末尾。`find_vehicle` 方法根据车牌号查找车辆,并返回车辆对象。`remove_vehicle` 方法从链表中删除车辆。`display` 方法将所有车辆信息输出到终端。
你可以使用以下代码测试该系统:
```python
# 创建车辆列表并添加几个车辆
vehicle_list = VehicleList()
vehicle_list.add_vehicle(Vehicle("ABC123", "Toyota", "Corolla", 2015))
vehicle_list.add_vehicle(Vehicle("DEF456", "Honda", "Civic", 2018))
vehicle_list.add_vehicle(Vehicle("GHI789", "Ford", "Mustang", 2020))
# 显示所有车辆
vehicle_list.display()
# 根据车牌号查找车辆
vehicle = vehicle_list.find_vehicle("DEF456")
if vehicle is not None:
print(f"Found vehicle: {vehicle.reg_no} {vehicle.make} {vehicle.model} {vehicle.year}")
else:
print("Vehicle not found")
# 删除一辆车
if vehicle_list.remove_vehicle("DEF456"):
print("Vehicle removed")
else:
print("Vehicle not found")
```
输出结果应该为:
```
ABC123 Toyota Corolla 2015
DEF456 Honda Civic 2018
GHI789 Ford Mustang 2020
Found vehicle: DEF456 Honda Civic 2018
Vehicle removed
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)