Professional Documents
Culture Documents
Ex 5
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 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()
Output:
Current Queue: ['Customer 1', 'Customer 2', 'Customer 3']
Next Customer: Customer 1
Serving Customer: Customer 1
Updated Queue: ['Customer 2', 'Customer 3']