- 2068 Bhadra · 4 marks
Petrol pump queue simulation
Short answer
Use a queue. Enqueue arriving vehicles; when pump free, dequeue, start = max(arrival, pump_free), wait = start−arrival, update pump_free. For the given arrival and service times, total wait is 11 minutes; average wait = 11/5 = 2.2 minutes.
Suppose you are asked to simulate people waiting in a petrol pump, which data structure would you use? Discuss suitable algorithm required for your simulation.
Data structure
A queue is the suitable data structure. Vehicles are served in the order they arrive (FIFO): a new vehicle joins at the rear, and the vehicle at the front is served and leaves. A circular array or a linked queue can be used; a linked queue is better because the number of waiting vehicles is not known beforehand.
arrive -> [ rear ... ... front ] -> pump -> leave
Algorithm
Assume the pump serves one vehicle at a time. Each vehicle has an arrival time and a service time. Clock t runs in minutes.
1. Create an empty queue Q; pump_free_at = 0; total_wait = 0
2. Repeat for each vehicle in order of arrival time:
a. ENQUEUE(Q, vehicle) /* vehicle arrives */
3. While Q is not empty:
a. v = DEQUEUE(Q) /* front vehicle goes to the pump */
b. start = max(v.arrival, pump_free_at)
c. wait = start - v.arrival
d. finish = start + v.service
e. pump_free_at = finish
f. total_wait = total_wait + wait
g. print v, wait, finish
4. average wait = total_wait / number of vehicles
In a real-time simulation, step 2 is carried out whenever the clock reaches an arrival time (random arrivals), and QUEUE EMPTY is checked before every dequeue. Underflow means the pump is idle.
Example
Arrival times 0, 1, 2, 6, 7; service times 3, 2, 4, 1, 2 minutes.
| Vehicle | Arrival | Service | Start | Wait | Finish |
|---|---|---|---|---|---|
| C1 | 0 | 3 | 0 | 0 | 3 |
| C2 | 1 | 2 | 3 | 2 | 5 |
| C3 | 2 | 4 | 5 | 3 | 9 |
| C4 | 6 | 1 | 9 | 3 | 10 |
| C5 | 7 | 2 | 10 | 3 | 12 |
Total wait = 0 + 2 + 3 + 3 + 3 = 11 minutes; average wait = 11 / 5 = 2.2 minutes.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Circular queue algorithm with conditions · Data Structure and Algorithm, chapter 2
- Unfair service centre simulation · Data Structure and Algorithm, chapter 2
- Data structure types and operations · Data Structure and Algorithm, chapter 1
- Queue overflow and underflow errors · Data Structure and Algorithm, chapter 2
- Define data structure and its importance · Data Structure and Algorithm, chapter 1
- Data structure and basic operations · Data Structure and Algorithm, chapter 1
- Java simulation of single server queue · Simulation and Modeling, chapter 9 · asked 5×
- Estimating, purpose and data required · Estimating and Costing, chapter 1 · asked 6×
- Estimated cost never actual; data required · Estimating and Costing, chapter 1
- Data required for detailed estimate · Estimating and Costing, chapter 1