Skip to content

Queue ADT

Abstract Class IQueue

Bases: Sized, Iterable

IQueue provides an interface for the Queue ADT that supports the Pythonic way of iterating over elements and querying collection size.

Implementations of this ADT must implement the abstract methods __len__(), __iter__(), front(), enqueue(), and dequeue(). In addition, the abstract property element_type also has to be implemented, and it is suggested to acquire this data via __init__() with an argument of type pydsa.utils.ElemTypeName.

Source code in pydsa/queue/adt.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class IQueue(Sized, Iterable):
    """`IQueue` provides an interface for the Queue ADT that supports the
    Pythonic way of iterating over elements and querying collection size.


    Implementations of this ADT must implement the abstract methods 
    `__len__()`, `__iter__()`, `front()`, `enqueue()`, and 
    `dequeue()`. In addition, the abstract property `element_type` also 
    has to be implemented, and it is suggested to acquire this data via 
    `__init__()` with an argument of type `pydsa.utils.ElemTypeName`.
    """

    @property
    def empty(self) -> bool:
        """Checks if the queue is empty.

        Returns:
            bool: True if the queue contains no elements.
        """
        return len(self) == 0

    def __str__(self) -> str:
        """String representation of the queue, listing elements in queue order
        from left to right.

        Example:
            `[3,1,4,1,5]`

        Returns:
            str: The string representation.
        """
        s = '['
        if not self.empty:
            itr = iter(self)
            s = f'{s}{next(itr)}'
            for elem in itr:
                s = f'{s},{elem}'
        s = f'{s}]'

        return s

    @property
    @abstractmethod
    def element_type(self) -> ElemTypeName:
        """String representation of the type of each element in the queue.

        Returns:
            Type: The element type string.
        """

    @abstractmethod
    def front(self) -> Elem:
        """Gets the element at the front of the queue if it is not empty.

        Returns:
            Elem: The front element.
        """

    @abstractmethod
    def enqueue(self, elem: Elem) -> None:
        """Inserts an element at the end of the queue.

        Args:
            elem (Elem): The element to insert.
        """

    @abstractmethod
    def dequeue(self) -> Elem:
        """Removes the front element from the queue if it is not empty.

        Returns:
            Elem: _description_
        """

__str__()

String representation of the queue, listing elements in queue order from left to right.

Example

[3,1,4,1,5]

Returns:

Name Type Description
str str

The string representation.

Source code in pydsa/queue/adt.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def __str__(self) -> str:
    """String representation of the queue, listing elements in queue order
    from left to right.

    Example:
        `[3,1,4,1,5]`

    Returns:
        str: The string representation.
    """
    s = '['
    if not self.empty:
        itr = iter(self)
        s = f'{s}{next(itr)}'
        for elem in itr:
            s = f'{s},{elem}'
    s = f'{s}]'

    return s

dequeue() abstractmethod

Removes the front element from the queue if it is not empty.

Returns:

Name Type Description
Elem Elem

description

Source code in pydsa/queue/adt.py
82
83
84
85
86
87
88
@abstractmethod
def dequeue(self) -> Elem:
    """Removes the front element from the queue if it is not empty.

    Returns:
        Elem: _description_
    """

element_type() property abstractmethod

String representation of the type of each element in the queue.

Returns:

Name Type Description
Type ElemTypeName

The element type string.

Source code in pydsa/queue/adt.py
57
58
59
60
61
62
63
64
@property
@abstractmethod
def element_type(self) -> ElemTypeName:
    """String representation of the type of each element in the queue.

    Returns:
        Type: The element type string.
    """

empty() property

Checks if the queue is empty.

Returns:

Name Type Description
bool bool

True if the queue contains no elements.

Source code in pydsa/queue/adt.py
28
29
30
31
32
33
34
35
@property
def empty(self) -> bool:
    """Checks if the queue is empty.

    Returns:
        bool: True if the queue contains no elements.
    """
    return len(self) == 0

enqueue(elem) abstractmethod

Inserts an element at the end of the queue.

Parameters:

Name Type Description Default
elem Elem

The element to insert.

required
Source code in pydsa/queue/adt.py
74
75
76
77
78
79
80
@abstractmethod
def enqueue(self, elem: Elem) -> None:
    """Inserts an element at the end of the queue.

    Args:
        elem (Elem): The element to insert.
    """

front() abstractmethod

Gets the element at the front of the queue if it is not empty.

Returns:

Name Type Description
Elem Elem

The front element.

Source code in pydsa/queue/adt.py
66
67
68
69
70
71
72
@abstractmethod
def front(self) -> Elem:
    """Gets the element at the front of the queue if it is not empty.

    Returns:
        Elem: The front element.
    """