Skip to main content

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

EventStack (bottom → top)Served
C1 arrivesC1-
C2 arrivesC1, C2-
C3 arrivesC1, C2, C3-
Counter freeC1, C2C3
C4 arrivesC1, C2, C4-
Counter freeC1, C2C4
Counter freeC1C2
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)

All Data Structure and Algorithm chapter 2 questions →