1. Experimental topic: Parking lot management 2. Problem description:
Assume that the parking lot is a long and narrow passage that can park n cars, and there is only one gate for vehicles to enter and exit. Vehicles are parked from the innermost part of the parking lot to the gate according to the morning and evening they arrive at the parking lot (the first car that arrives first is placed at the innermost part of the parking lot). If the parking lot is full of n cars, subsequent cars can only wait on the sidewalk outside the parking lot gate. Once a car drives away in the parking lot, the first car on the sidewalk will enter the parking lot. If there is a car in the parking lot that wants to drive away, the cars that enter the parking lot after it must first exit the parking lot to make way for it. After it drives out of the parking lot, these vehicles will enter the parking lot in the original order. When each vehicle leaves the parking lot, it is charged a fee based on the length of time it remains in the parking lot. If a car staying on the sidewalk wants to leave before entering the parking lot, it is allowed to leave without charging a parking fee, and the order of vehicles waiting on the sidewalk is still maintained. Prepare a program to simulate the management of the parking lot.
3. Demand analysis:
The parking lot adopts a stack structure, and the sidewalk outside the parking lot adopts a queue structure (that is, the sidewalk is a waiting queue). The management process of the parking lot is as follows:
① When a vehicle is about to enter the parking lot, check whether the parking lot is full. If it is not full, the vehicle will be pushed into the stack (the vehicle enters the parking lot); if the parking lot is full, the vehicle will enter the waiting queue (the vehicle will enter the sidewalk to wait).
② When a vehicle requests to exit the stack, the vehicles that reach the top of the stack will pop out of the stack first (vehicles entering after it must first exit the parking lot to make way for it), then allow the vehicle to exit the stack, and other vehicles will enter the stack in the original order. (Enter the parking lot). After the vehicle is removed from the stack, check whether there is a vehicle in the waiting queue (on the sidewalk). If there is a vehicle, take a vehicle from the head of the queue and push it into the stack.
Expand