Module dalpy.queues

This module holds classes related to FIFO queues.

This module contains Queue, an implementation of a FIFO queue, and QueueUnderflowError, an error raised by Queue.

Examples

Initializing, adding, and removing elements from a Queue:

q = Queue()
q.enqueue(1)
q.enqueue(2)
q.dequeue()

The following code will raise a QueueUnderflowError because the second dequeue is done on an empty Queue:

q.dequeue()
q.dequeue()

Classes

class Queue

This class represents a FIFO queue that has no maximum capacity.

Examples

To initialize a Queue:

q = Queue()

To add elements to the end of q:

q.enqueue(1)
q.enqueue(2)

To remove and return the element at the front of q (in this case x = 1):

x = q.dequeue()

To see the element at the front of q (in this case y = 2):

y = q.front()

Initializes an empty Queue in O(1) time.

Methods

def dequeue(self)

Removes the element at the front of this Queue.

This operation runs in O(1) time with respect to the size of this Queue.

Returns

The element at the front of the Queue that was removed. That is, the element that was first added to this Queue of the elements in it.

Raises

QueueUnderflowError
If this Queue is empty.
def enqueue(self, value)

Adds an element to the end of this Queue.

This operation runs in O(1) time with respect to the size of this Queue.

Args

value
Element to add to this Queue. It can be of any type.
def front(self)

Gets the element at the front of this Queue.

This operation runs in O(1) time with respect to the size of this Queue.

Returns

The element at the front of the Queue. That is, the element that was first added to this Queue of the elements in it.

Raises

QueueUnderflowError
If this Queue is empty.
def is_empty(self)

Returns True if this Queue is empty, False otherwise in O(1) time w/r/t the size of this Queue.

def size(self)

Returns the integer number of elements in this Queue in O(1) time w/r/t the size of this Queue.

class QueueUnderflowError (operation)

This class is used by Queue to raise errors for operations done on an empty Queue.

Initializes a QueueUnderflowError that will be raised associated with a particular Queue operation.

Args

operation
a string specifying the operation to raise an error on

Ancestors

  • builtins.Exception
  • builtins.BaseException