Skip to content

Priority Queue ADT

PriorityQueue (ABC)

A priority queue is a queue ADT that supports insertion (enqueueing) of elements at one end and removal (dequeueing) of elements from the opposite end, with the extra attribute of priority of each element it contains, meaning an element can move closer to the front of the queue if the elements in front of it are of less precedence than itself.

__len__(self) special

Get the total number of elements stored in the queue

Returns:

Type Description
int

count of elements in queue

Source code in priority_queues/priority_queue.py
@abstractmethod
def __len__(self) -> int:
    """Get the total number of elements stored in the queue

    :returns: count of elements in queue
    """
    pass

dequeue(self)

Remove first element of the queue and return it

Returns:

Type Description
Any

first element of queue

Source code in priority_queues/priority_queue.py
@abstractmethod
def dequeue(self) -> Any:
    """Remove first element of the queue and return it

    :return: first element of queue
    """
    pass

enqueue(self, x, priority)

Insert an element to the end of the queue

Parameters:

Name Type Description Default
x Any

element to add to the queue

required
priority Union[int, float]

value that determines precedence of x in relation to the rest of the elements in the queue

required
Source code in priority_queues/priority_queue.py
@abstractmethod
def enqueue(self, x: Any, priority: Union[int, float]) -> None:
    """Insert an element to the end of the queue

    :param x: element to add to the queue
    :param priority: value that determines precedence of x in relation to the rest of the elements in the queue
    """
    pass

get_first(self)

Return first element of the queue without removing it

Returns:

Type Description
Any

first element of queue

Source code in priority_queues/priority_queue.py
@abstractmethod
def get_first(self) -> Any:
    """Return first element of the queue without removing it

    :return: first element of queue
    """
    pass

is_empty(self)

Check if queue contains no elements

Returns:

Type Description
bool

True if queue is empty, else False

Source code in priority_queues/priority_queue.py
@abstractmethod
def is_empty(self) -> bool:
    """Check if queue contains no elements

    :return: True if queue is empty, else False
    """
    pass