停车场管理模拟程序:栈和队列的应用

假设停车场内只有一个可停放N(假设N为5)辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由南向北排列。若车场内已停满N辆车,则后来的汽车只能在门外的便道,也就是候车场等候,一旦有车开走,则排在候车场上的第一辆车便可开入停车场(假定候车场同样只有N辆汽车的容量)。当停车场内某辆汽车要开走时,在它之后进入的车辆必须先退出停车场在待停区等候,等该车开出大门外,待停区的车辆再按原次序进入停车场。整个停车场的示意图如下图所示。

停车场示意图

请利用栈和队列结构为停车场编写管理的模拟程序内容:

思路:

  1. 创建一个栈和一个队列,分别用于代表停车场和便道。
  2. 当有车辆进入停车场时,先判断停车场是否已满,若未满则将该车辆入栈并输出停车信息;若已满则将该车辆入队列并输出等待信息。
  3. 当有车辆开出停车场时,先将其从栈中弹出并输出离开信息,再判断队列中是否有车辆等待,若有则将队首车辆入栈并输出停车信息。
  4. 重复步骤2和3,直到程序结束。
class ParkingLot:
    def __init__(self, capacity):
        self.capacity = capacity
        self.stack = []
        self.queue = []

    def enter(self, car):
        if len(self.stack) < self.capacity:
            self.stack.append(car)
            print(f'Car {car} parked in the parking lot.')
        else:
            self.queue.append(car)
            print(f'Car {car} is waiting in the queue.')

    def exit(self, car):
        if car in self.stack:
            self.stack.remove(car)
            print(f'Car {car} has left the parking lot.')
            if self.queue:
                next_car = self.queue.pop(0)
                self.stack.append(next_car)
                print(f'Car {next_car} parked in the parking lot.')
        else:
            print(f'Car {car} is not in the parking lot.')

    def display(self):
        print(f'Parking lot: {self.stack}')
        print(f'Queue: {self.queue}')

parking_lot = ParkingLot(5)

while True:
    command = input('Enter command (enter, exit, display, quit): ')
    if command == 'enter':
        car = input('Enter car number: ')
        parking_lot.enter(car)
    elif command == 'exit':
        car = input('Enter car number: ')
        parking_lot.exit(car)
    elif command == 'display':
        parking_lot.display()
    elif command == 'quit':
        break
    else:
        print('Invalid command.')

运行示例:

Enter command (enter, exit, display, quit): enter
Enter car number: A123
Car A123 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: B456
Car B456 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: C789
Car C789 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: D012
Car D012 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: E345
Car E345 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: F678
Car F678 is waiting in the queue.
Enter command (enter, exit, display, quit): display
Parking lot: ['A123', 'B456', 'C789', 'D012', 'E345']
Queue: ['F678']
Enter command (enter, exit, display, quit): exit
Enter car number: C789
Car C789 has left the parking lot.
Car F678 parked in the parking lot.
Enter command (enter, exit, display, quit): exit
Enter car number: G901
Car G901 is not in the parking lot.
Enter command (enter, exit, display, quit): display
Parking lot: ['A123', 'B456', 'D012', 'E345', 'F678']
Queue: []
Enter command (enter, exit, display, quit): quit

代码实现中使用了Python中的列表来模拟栈和队列的数据结构。代码逻辑清晰,易于理解。该程序可以帮助读者更好地理解栈和队列数据结构的应用,以及如何使用它们来解决实际问题。

停车场管理模拟程序:栈和队列的应用

原文地址: https://www.cveoy.top/t/topic/m1jY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录