- 2068 Magh · 4 marks
Unfair service centre simulation
Short answer
A stack (LIFO) models the centre. Push arriving customers; when counter free, pop and serve. Example: C1, C2, C3 arrive, C3 served first; later C4 arrives, served before C2 and C1. Early arrivals may starve if new customers keep arriving.
In some unfair customer service centre, a person who comes at last gets services at first and the persons who come at first are still waiting for the services up to last. Devise a suitable algorithm required for the simulation of this system.
Data structure
The last person to arrive is served first, and the first person waits longest. This is LIFO, so a stack models the service centre. Arrival is a push; serving a customer is a pop.
arrive -> push serve -> pop
| C3 | <- top (served first)
| C2 |
| C1 | <- bottom (waits longest)
Algorithm
1. Create an empty stack S (top = -1 for an array of size MAX)
2. Repeat until the simulation ends:
if a customer c arrives:
if S is full: print "Centre full, customer turned away"
else PUSH(S, c)
if the counter is free:
if S is empty: print "No customer waiting"
else c = POP(S); serve c
3. When closing time comes, customers left in S are never served (starvation).
PUSH(S, c): if top == MAX-1 -> overflow; else top = top + 1; S[top] = c
POP(S): if top == -1 -> underflow; else c = S[top]; top = top - 1; return c
Example trace
| Event | Stack (bottom → top) | Served |
|---|---|---|
| C1 arrives | C1 | - |
| C2 arrives | C1, C2 | - |
| C3 arrives | C1, C2, C3 | - |
| Counter free | C1, C2 | C3 |
| C4 arrives | C1, C2, C4 | - |
| Counter free | C1, C2 | C4 |
| Counter free | C1 | C2 |
| Counter free | (empty) | C1 |
If new customers keep arriving before the counter becomes free, C1 and C2 are never served, which is why the centre is "unfair".
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Petrol pump queue simulation · Data Structure and Algorithm, chapter 2
- Doubly linked list: bank customer records · Data Structure and Algorithm, chapter 4
- Delete last node of doubly linked list · Data Structure and Algorithm, chapter 4
- Depth first traversal in graph · Data Structure and Algorithm, chapter 10 · asked 4×
- Breadth first traversal in graph · Data Structure and Algorithm, chapter 10 · asked 2×
- KUKL customer system design and code · Object Oriented Analysis and Design, chapter 4
- Simulation of computer system serving WWW · Simulation and Modeling, chapter 10 · asked 2×
- Define system, model, simulation · Simulation and Modeling, chapter 1
- Continuous system simulation; analog computer example · Simulation and Modeling, chapter 3