dsa.queue module

Module containing queue classes.

class dsa.queue.CircularQueue(contents=None, capacity=10)

Bases: CircularArray

A circular queue implementation.

dequeue()

Dequeue an element from the queue. Raise Exception when there are no elements to dequeue.

Raises:

Exception – When there are no elements to dequeue.

Returns:

The from element in the queue.

enqueue(element)

Enqueue an element into the queue. Wrap around when trying to enqueue more elements than the capacity.

Parameters:

element – The element to enqueue.

Returns:

None

peek()

Return the element in front of the queue. Raise Exception if queue is empty.

Returns:

The element in front of the queue.

Raises:

Exception – When the queue is empty.

class dsa.queue.DynamicQueue(contents=None, capacity=10)

Bases: Queue

A dynamic queue implementation. Note that shrink is not impelmented.

check_capacity()

If count >= capacity, grow the array.

Returns:

None

enqueue(element)

Enqueue an element into the queue. Increae capacity if count is greater than the capacity.

Parameters:

element – the element to enqueue

Returns:

None

grow()

Double the capacity of the current array.

Returns:

None

class dsa.queue.Queue(contents=None, capacity=10)

Bases: object

A static queue implementation.

capacity()

Return the capacity of the queue.

Returns:

The capacity of the queue.

count

number of elements in queue

dequeue()

Dequeue an element from the queue. Raise Exception when there are no elements to dequeue.

Raises:

Exception – When there are no elements to dequeue.

Returns:

The from element in the queue.

enqueue(element)

Enqueue an element into the queue. Raise Exception when trying to enqueue more elements than the capacity.

Parameters:

element – The element to enqueue.

Raises:

Exception – When trying to enqueue more elements than the capacity.

Returns:

None

classmethod from_list(alist)

Set the contents of a queue into an array. Raise Exception when trying to enqueue more elements than the capacity.

Parameters:

alist – The list with contents to enqueue.

Returns:

The queue with the contents of the list.

is_empty()

Return a Boolean on whether the stack is empty or not.

Returns:

True if the stack is empty, False otherwise.

peek()

Return the element in front of the queue. Raise Exception if queue is empty.

Returns:

The element in front of the queue.

Raises:

Exception – When the queue is empty.

raw_view()

Return the queue in its array representation.

Returns:

The array representation of the queue.

to_ordered_list()

Return the contents of the queue as a Python list.

Return type:

list

Returns:

The contents of the queue as a Python list.