Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

EX 5:

To manage the queuing system of ticket counter in SKY cinemas implement the queue
data structure with all of its corresponding operations.

Algorithm:
1. Queue Initialization:
• Create a class TicketQueue.
• Initialize an empty list named customer_queue in the __init__ method to
store customer names.
• class TicketQueue: Initialize an empty list customer_queue to store customer
names in __init__.
2. Check if Queue is Empty:
• Implement the is_empty method to check if the ticket queue is empty.
• Implement is_empty: Return True if customer_queue is empty, otherwise
return False.
3. Enqueue Operation (Join the Queue):
• Implement the enqueue(customer_name) method to add a customer to the
end of the ticket queue.
• Implement enqueue(customer_name): Append customer_name to
customer_queue.
4. Dequeue Operation (Serve a Customer):
• Implement the dequeue method to remove and return the customer at the front
of the ticket queue.
• Implement dequeue: If customer_queue is not empty: - Remove and return the
first customer_name from customer_queue. Else: - Return an indication that
the queue is empty.
5. Peek Operation (Next Customer):
• Implement the peek method to return the customer at the front of the ticket
queue without serving.
• Implement peek: If customer_queue is not empty: - Return the first
customer_name in customer_queue. Else: - Return an indication that the queue
is empty.
6. Queue Size:
• Implement the size method to return the number of customers in the ticket
queue.
• Implement size: Return the length of customer_queue.
Program:
class Queue:
def __init__(self):
self.items = []

def is_empty(self):
return len(self.items) == 0

def enqueue(self, item):


self.items.append(item)

def dequeue(self):
if not self.is_empty():
return self.items.pop(0)

def peek(self):
if not self.is_empty():
return self.items[0]

def size(self):
return len(self.items)

# Example Usage:
ticket_queue = Queue()

# Enqueue customers to the ticket counter queue


ticket_queue.enqueue("Customer 1")
ticket_queue.enqueue("Customer 2")
ticket_queue.enqueue("Customer 3")
# Display the current queue
print("Current Queue:", ticket_queue.items)

# Peek at the next customer in line


next_customer = ticket_queue.peek()
print("Next Customer:", next_customer)

# Dequeue the next customer


served_customer = ticket_queue.dequeue()
print(f"Serving Customer: {served_customer}")

# Display the updated queue


print("Updated Queue:", ticket_queue.items)

Output:
Current Queue: ['Customer 1', 'Customer 2', 'Customer 3']
Next Customer: Customer 1
Serving Customer: Customer 1
Updated Queue: ['Customer 2', 'Customer 3']

You might also like