You are on page 1of 3

class PriorityQueue:

def __init__(self, size):

"""Constructor method to create the priority queue.

Args:

size (int): The maximum number of elements to store in the queue.

"""

self.size = size

self.items = []

def get_size(self):

"""Method to return the number of items stored.

Returns:

int: The number of items currently stored.

"""

return len(self.items)

def clear(self):

"""Method to remove all elements."""

self.items = []

def isFull(self):

"""Method to check if the queue is full.

Returns:

bool: True if full, otherwise False.

"""
return len(self.items) == self.size

def isEmpty(self):

"""Method to check if the queue is empty.

Returns:

bool: True if empty, otherwise False.

"""

return len(self.items) == 0

def enqueue(self, item):

"""Method to add the item to the end of the queue (only when not full).

Args:

item (any): The item to store in the queue.

"""

if not self.isFull():

self.items.append(item)

def dequeue(self):

"""Method to remove and return the first element of the queue (only when not empty).

Returns:

item: The first element of the queue.

"""

if not self.isEmpty():

return self.items.pop(0)

def peek(self):
"""Method to return the first element of the queue (only when not empty).

Returns:

item: The first element of the queue.

"""

if not self.isEmpty():

return self.items[0]

# Sample code to create a priority queue with a maximum of 5 items

obj = PriorityQueue(5)

# Add items in the queue. The following should push 5 items

obj.enqueue(4)

obj.enqueue(9)

obj.enqueue(5)

obj.enqueue(1)

obj.enqueue(3)

# Test Case No. 1

assert obj.get_size() == 5, "get_size() should return 5"

# Test Case No. 2

assert obj.dequeue() == 4, "dequeue() should return 4"

# Remove and return the first item. This should print 9 (If you completed the correct queue
implementation)

print("Remove and return the first element:", obj.dequeue())

You might also like