0% found this document useful (0 votes)
24 views20 pages

Case Study

The document outlines a case study for simulating a ticket counter system using a Queue data structure to manage customer arrivals and service in a FIFO manner. It details the objectives, system requirements, operations on the queue, and provides a Python implementation for managing customer interactions. Additionally, it discusses types of queues, their real-world applications, and includes an algorithm for the ticket counter system.

Uploaded by

vaishamudhol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views20 pages

Case Study

The document outlines a case study for simulating a ticket counter system using a Queue data structure to manage customer arrivals and service in a FIFO manner. It details the objectives, system requirements, operations on the queue, and provides a Python implementation for managing customer interactions. Additionally, it discusses types of queues, their real-world applications, and includes an algorithm for the ticket counter system.

Uploaded by

vaishamudhol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CASE STUDY

Data Structure
Aim:
Simulate customers queuing at a ticket counter in a FIFO manner

Introduction:
A ticket counter system is a real-time service environment where customer arrive
at a counter to buy tickets( for ex: Railway tickets, Cinema tickets). Customer are
served in the order in which they arrive. To manage this in an organized way, a
Queue data structure is ideal because it follows FIFO(First In, First Out).manner.

Objective:
 To develop a program that models a ticket counter using the Queue data
structure.
 To handle customer arrivals, service completion, and departures.
 To calculate and display useful statistics like waiting time, total served
customers, and queue length at any point.

System Requirements:
Hardware/Software:
 Programming Language: Python/Java/C++ (any)
 Data Structure: Queue (can be implemented using array or linked list)

Functional Requirements:
 Enqueue: Add a customer when they arrive.
 Dequeue: Remove a customer when their ticket is issued.
 Peek: Show the next customer to be served
 Display: Show the entire queue
Theory:
What is Queue ? Queue concept.
 A queue is a linear list in which data can only be
inserted at one end, called the rear, and deleted
from the other end, called the front.
 A queue is a first in–first out (FIFO) structure.
 Insertion of an element in the queue is called as
enqueue operation.
 Deletion of an element from the queue is called as
dequeue operation.
 Insertion (enqueue) takes place at the rear of the
queue.
 Deletion (dequeue) takes place at the front of the
queue.
 Queue is said to be in Overflow state when it is
completely full.
 Queue is said to be in Underflow state if it is
completely empty.
Operation on Queue
Enqueue and Dequeue Operations on Queue
Operations on Queue Using Python Code
# Create an empty queue

queue = deque()

def enqueue():

item = input("Enter element to enqueue: ")

queue.append(item)

print(f"{item} added to the queue.")

def dequeue():

if not queue:

print("Queue is empty. Cannot dequeue.")

else:

removed = queue.popleft()

print(f"{removed} removed from the queue.")

def peek():

if not queue:

print("Queue is empty.")

else:

print("Front element is:", queue[0])

def display():

if not queue:

print("Queue is empty.")
print("Queue elements:", " -> ".join(queue))

# ----- Main Menu -----

while True:

print("\n--- Queue Operations ---")

print("1. Enqueue (Insert)")

print("2. Dequeue (Remove)")

print("3. Peek (Front Element)")

print("4. Display Queue")

print("5. Exit")

choice = input("Enter your choice (1-5): ")

if choice == '1':

enqueue()

elif choice == '2':

dequeue()

elif choice == '3':

peek()

elif choice == '4':

display()

elif choice == '5':

print("Exiting program.")

break

else:

print("Invalid choice! Please try again.")


Explanation of Operations:

Operation Method Used Description


Enqueue queue.append(x) Adds element x to the rear of the queue.
Dequeue queue.popleft() Removes and returns element from the front of the queue.
Peek queue[0] Returns the front element without removing it.
Display Iterate/print Shows all elements in order from front to rear.

Types of Queue
There are total Two Types of Queue:

1.Circular Queue.

2.Priority Queue.

I. Circular Queue:-
II. Priority Queue:-
A Priority Queue is a special type of queue in which each
element is assigned a priority, and the element with the
highest priority is served first, regardless of the order in
which it arrived.
If two elements have the same priority, the one that
arrived earlier is served first (FIFO within the same priority
level).

o Types of Priority Queue:


1. Ascending Priority Queue
 Elements with lowest priority value are served first.
 Example: Shortest Job First (SJF) scheduling where smallest time job
executes first.

2. Descending Priority Queue


 with highest priority value are served first.
 Elements Example: Emergency room treatment (critical patients treated
first).

3. Double-Ended Priority Queue (Deque-based)


 Allows insertion/deletion at both ends with priorities.

Basic Operations:
Operation Description
Insert a new element along with its
Insertion (enqueue)
priority.
Remove the element with the highest
Deletion (dequeue)
priority.
View the element with the highest
Peek/Front
priority without removing it.
Change Priority
Update the priority of an element.
(optional)

 Real-World Applications:
✅ Operating System Scheduling – CPU selects processes
based on priority (e.g., high-priority tasks execute first).
✅ Hospital Emergency Rooms – Patients with critical
conditions are treated before others.
✅ Network Routing – Packets with higher priority are
transmitted first.
✅ Dijkstra’s Algorithm – Finding the shortest path in
graphs.
✅ Event Simulation – Handling time-based events where
earliest events have higher priority.

Algorithm for Ticket Counter System:


1. Initialize an empty queue.

2. While the ticket counter is open:

 If a customer arrives → Enqueue(customer).


 If a counter becomes free → Dequeue() and process service.

3. Track the time each customer spends in the system.

Data Flow:
 Input: Customer arrival time, service time.
 Process: Queue operations.
 Output: Customer order, waiting times, statistics.

Example Scenario:
Suppose a railway ticket counter opens at 10:00 AM:
 Customer A arrives at 10:01 → added to queue.
 Customer B arrives at 10:02 → added to queue.
 Service time is 2 minutes per customer.
 The counter serves A first, then B, ensuring FIFO order.

Applications:
 Railway or bus reservation counters
 Cinema or event ticket booking
 Customer service centers
 Bank counters and billing counters

# Python code for Ticket Counter System


from collections import deque
import time

class TicketCounter:
def __init__(self):
self.queue = deque()

def arrival(self, name):


# Add customer to the queue
self.queue.append(name)
print(f"{name} has arrived and joined the queue.")
def serve(self):
# Serve the first customer
if not self.queue:
print("No customers in queue to serve.")
else:
served = self.queue.popleft()
print(f"{served} has been served and left the counter.")

def next_customer(self):
# Show the next customer
if not self.queue:
print("No customers waiting.")
else:
print(f"Next customer to be served: {self.queue[0]}")

def display_queue(self):
# Display all waiting customers
if not self.queue:
print("Queue is empty.")
else:
print("Customers in queue:", " -> ".join(self.queue))
# --- Main Simulation ---
if __name__ == "__main__":
counter = TicketCounter()

while True:
print("\n--- Ticket Counter Menu ---")
print("1. New Customer Arrival")
print("2. Serve Next Customer")
print("3. View Next Customer")
print("4. Display Queue")
print("5. Exit")
choice = input("Enter your choice (1-5): ")

if choice == "1":
name = input("Enter Customer Name: ")
counter.arrival(name)

elif choice == "2":


counter.serve()

elif choice == "3":


counter.next_customer()
elif choice == "4":
counter.display_queue()

elif choice == "5":


print("Ticket Counter Closed.")
break

else:
print("Invalid choice! Please try again.")

#OUTPUT
--- Ticket Counter Menu ---
1. New Customer Arrival
2. Serve Next Customer
3. View Next Customer
4. Display Queue
5. Exit
Enter your choice (1-5): 1
Enter Customer Name: ATHARVA
ATHARVA has arrived and joined the queue.

Enter your choice (1-5): 1


Enter Customer Name: BOB
BOB has arrived and joined the queue.

Enter your choice (1-5): 4


Customers in queue: ATHARVA -> BOB

Enter your choice (1-5): 2


ATHARVA has been served and left the counter.

Enter your choice (1-5): 3


Next customer to be served: BOB

--- Ticket Counter Menu ---


1. New Customer Arrival
2. Serve Next Customer
3. View Next Customer
4. Display Queue
5. Exit
Enter your choice (1-5): 6
Invalid choice! Please try again.

--- Ticket Counter Menu ---


1. New Customer Arrival
2. Serve Next Customer
3. View Next Customer
4. Display Queue
5. Exit
Enter your choice (1-5): 5
Ticket Counter Closed.

You might also like