Skip to content

ListStack (LinkedList-based)

Download Source Files NEW

Specification

IStack

Bases: Generic[T], ABC

Interface for a stack data structure

Source code in src/datastructures/istack.py
 6
 7
 8
 9
10
11
12
13
14
15
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
89
90
91
92
class IStack(Generic[T], ABC):
    ''' Interface for a stack data structure '''

    @abstractmethod
    def push(self, item: T) -> None:
        ''' Pushes an item onto the stack.

            Arguments:
                item: T -- The item to push onto the stack.
        '''
        ...

    @abstractmethod
    def pop(self) -> T:
        ''' Pops an item from the stack.

            Returns:
                T -- The item popped from the stack.
        '''
        ...

    @abstractmethod
    def peek(self) -> T:
        ''' Returns the top item on the stack without removing it.

            Returns:
                T -- The top item on the stack.
        '''
        ...

    @property
    @abstractmethod
    def empty(self) -> bool:
        ''' Returns True if the stack is empty, False otherwise. 

            Returns:
                bool: True if the stack is empty, False otherwise.
        '''
        ...

    @abstractmethod
    def clear(self) -> None:
        ''' Clears the stack. '''
        ...

    @abstractmethod
    def __contains__(self, item: T) -> bool:
        ''' Returns True if the item is in the stack, False otherwise.

            Arguments:
                item: T -- The item to search for.

            Returns:
                bool: True if the item is in the stack, False otherwise.
        '''
        ...

    @abstractmethod
    def __eq__(self, other: object) -> bool:
        ''' Returns True if the stack is equal to another stack, False otherwise.

            Arguments:
                other: object -- The other stack to compare.

            Returns:
                bool: True if the stack is equal to another stack, False otherwise.
        '''
        ...

    @abstractmethod
    def __len__(self) -> int:
        ''' Returns the number of items in the stack.

            Returns:
                int -- The number of items in the stack.
        '''
        ...

    @abstractmethod
    def __str__(self) -> str:
        ''' Returns a string representation of the stack.'''
        ...

    @abstractmethod
    def __repr__(self) -> str:
        ''' Returns a string representation of the stack.'''
        ...

empty abstractmethod property

Returns True if the stack is empty, False otherwise.

Returns:

Name Type Description
bool bool

True if the stack is empty, False otherwise.

__contains__(item) abstractmethod

Returns True if the item is in the stack, False otherwise.

Parameters:

Name Type Description Default
item T

T -- The item to search for.

required

Returns:

Name Type Description
bool bool

True if the item is in the stack, False otherwise.

Source code in src/datastructures/istack.py
51
52
53
54
55
56
57
58
59
60
61
@abstractmethod
def __contains__(self, item: T) -> bool:
    ''' Returns True if the item is in the stack, False otherwise.

        Arguments:
            item: T -- The item to search for.

        Returns:
            bool: True if the item is in the stack, False otherwise.
    '''
    ...

__eq__(other) abstractmethod

Returns True if the stack is equal to another stack, False otherwise.

Parameters:

Name Type Description Default
other object

object -- The other stack to compare.

required

Returns:

Name Type Description
bool bool

True if the stack is equal to another stack, False otherwise.

Source code in src/datastructures/istack.py
63
64
65
66
67
68
69
70
71
72
73
@abstractmethod
def __eq__(self, other: object) -> bool:
    ''' Returns True if the stack is equal to another stack, False otherwise.

        Arguments:
            other: object -- The other stack to compare.

        Returns:
            bool: True if the stack is equal to another stack, False otherwise.
    '''
    ...

__len__() abstractmethod

Returns the number of items in the stack.

Returns:

Type Description
int

int -- The number of items in the stack.

Source code in src/datastructures/istack.py
75
76
77
78
79
80
81
82
@abstractmethod
def __len__(self) -> int:
    ''' Returns the number of items in the stack.

        Returns:
            int -- The number of items in the stack.
    '''
    ...

__repr__() abstractmethod

Returns a string representation of the stack.

Source code in src/datastructures/istack.py
89
90
91
92
@abstractmethod
def __repr__(self) -> str:
    ''' Returns a string representation of the stack.'''
    ...

__str__() abstractmethod

Returns a string representation of the stack.

Source code in src/datastructures/istack.py
84
85
86
87
@abstractmethod
def __str__(self) -> str:
    ''' Returns a string representation of the stack.'''
    ...

clear() abstractmethod

Clears the stack.

Source code in src/datastructures/istack.py
46
47
48
49
@abstractmethod
def clear(self) -> None:
    ''' Clears the stack. '''
    ...

peek() abstractmethod

Returns the top item on the stack without removing it.

Returns:

Type Description
T

T -- The top item on the stack.

Source code in src/datastructures/istack.py
27
28
29
30
31
32
33
34
@abstractmethod
def peek(self) -> T:
    ''' Returns the top item on the stack without removing it.

        Returns:
            T -- The top item on the stack.
    '''
    ...

pop() abstractmethod

Pops an item from the stack.

Returns:

Type Description
T

T -- The item popped from the stack.

Source code in src/datastructures/istack.py
18
19
20
21
22
23
24
25
@abstractmethod
def pop(self) -> T:
    ''' Pops an item from the stack.

        Returns:
            T -- The item popped from the stack.
    '''
    ...

push(item) abstractmethod

Pushes an item onto the stack.

Parameters:

Name Type Description Default
item T

T -- The item to push onto the stack.

required
Source code in src/datastructures/istack.py
 9
10
11
12
13
14
15
16
@abstractmethod
def push(self, item: T) -> None:
    ''' Pushes an item onto the stack.

        Arguments:
            item: T -- The item to push onto the stack.
    '''
    ...

ListStack

Bases: Generic[T], IStack[T]

ListStack (LinkedList-based Stack)

Source code in src/datastructures/liststack.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class ListStack[T](Generic[T], IStack[T]):
    """
    ListStack (LinkedList-based Stack)

    """

    def __init__(self, data_type:object) -> None:
        """
        Initializes the ListStack.

        Args:
            data_type (type): The type of data the stack will hold.

        """
        raise NotImplementedError("ListStack.__init__ is not implemented.")

    def push(self, item: T):
        """
        Pushes an item onto the stack.

        Args:
            item (T): The item to push onto the stack.

        Raises:
            TypeError: If the item is not of the correct type.

        """
        raise NotImplementedError("ListStack.push is not implemented.")

    def pop(self) -> T:
        """
        Removes and returns the top item from the stack.

        Returns:
            T: The top item from the stack.

        Raises:
            IndexError: If the stack is empty.
        """
        raise NotImplementedError("ListStack.pop is not implemented.")

    def peek(self) -> T:
        """
        Returns the top item from the stack without removing it.

        Returns:
            T: The top item from the stack.

        Raises:
            IndexError: If the stack is empty.
        """
        raise NotImplementedError("ListStack.peek is not implemented.")

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

        Returns:
            bool: True if the stack is empty, False otherwise.
        """
        raise NotImplementedError("ListStack.empty is not implemented.")

    def clear(self):
        """
        Clears all items from the stack.
        """
        raise NotImplementedError("ListStack.clear is not implemented.")

    def __contains__(self, item: T) -> bool:
        """
        Checks if an item exists in the stack.

        Args:
            item (T): The item to check for.

        Returns:
            bool: True if the item exists in the stack, False otherwise.

        """
        raise NotImplementedError("ListStack.__contains__ is not implemented.")

    def __eq__(self, other) -> bool:
        """
        Compares two stacks for equality.

        Args:
            other (ListStack): The stack to compare with.

        Returns:
            bool: True if the stacks are equal, False otherwise.

        """
        raise NotImplementedError("ListStack.__eq__ is not implemented.")

    def __len__(self) -> int:
        """
        Returns the number of items in the stack.

        Returns:
            int: The number of items in the stack.
        """
        raise NotImplementedError("ListStack.__len__ is not implemented.")

    def __str__(self) -> str:
        """
        Returns a string representation of the stack.

        Returns:
            str: A string representation of the stack.
        """
        raise NotImplementedError("ListStack.__str__ is not implemented.")

    def __repr__(self) -> str:
        """
        Returns a detailed string representation of the stack.

        Returns:
            str: A detailed string representation of the stack.

        """
        raise NotImplementedError("ListStack.__repr__ is not implemented.")

empty property

Checks if the stack is empty.

Returns:

Name Type Description
bool bool

True if the stack is empty, False otherwise.

__contains__(item)

Checks if an item exists in the stack.

Parameters:

Name Type Description Default
item T

The item to check for.

required

Returns:

Name Type Description
bool bool

True if the item exists in the stack, False otherwise.

Source code in src/datastructures/liststack.py
76
77
78
79
80
81
82
83
84
85
86
87
def __contains__(self, item: T) -> bool:
    """
    Checks if an item exists in the stack.

    Args:
        item (T): The item to check for.

    Returns:
        bool: True if the item exists in the stack, False otherwise.

    """
    raise NotImplementedError("ListStack.__contains__ is not implemented.")

__eq__(other)

Compares two stacks for equality.

Parameters:

Name Type Description Default
other ListStack

The stack to compare with.

required

Returns:

Name Type Description
bool bool

True if the stacks are equal, False otherwise.

Source code in src/datastructures/liststack.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __eq__(self, other) -> bool:
    """
    Compares two stacks for equality.

    Args:
        other (ListStack): The stack to compare with.

    Returns:
        bool: True if the stacks are equal, False otherwise.

    """
    raise NotImplementedError("ListStack.__eq__ is not implemented.")

__init__(data_type)

Initializes the ListStack.

Parameters:

Name Type Description Default
data_type type

The type of data the stack will hold.

required
Source code in src/datastructures/liststack.py
13
14
15
16
17
18
19
20
21
def __init__(self, data_type:object) -> None:
    """
    Initializes the ListStack.

    Args:
        data_type (type): The type of data the stack will hold.

    """
    raise NotImplementedError("ListStack.__init__ is not implemented.")

__len__()

Returns the number of items in the stack.

Returns:

Name Type Description
int int

The number of items in the stack.

Source code in src/datastructures/liststack.py
102
103
104
105
106
107
108
109
def __len__(self) -> int:
    """
    Returns the number of items in the stack.

    Returns:
        int: The number of items in the stack.
    """
    raise NotImplementedError("ListStack.__len__ is not implemented.")

__repr__()

Returns a detailed string representation of the stack.

Returns:

Name Type Description
str str

A detailed string representation of the stack.

Source code in src/datastructures/liststack.py
120
121
122
123
124
125
126
127
128
def __repr__(self) -> str:
    """
    Returns a detailed string representation of the stack.

    Returns:
        str: A detailed string representation of the stack.

    """
    raise NotImplementedError("ListStack.__repr__ is not implemented.")

__str__()

Returns a string representation of the stack.

Returns:

Name Type Description
str str

A string representation of the stack.

Source code in src/datastructures/liststack.py
111
112
113
114
115
116
117
118
def __str__(self) -> str:
    """
    Returns a string representation of the stack.

    Returns:
        str: A string representation of the stack.
    """
    raise NotImplementedError("ListStack.__str__ is not implemented.")

clear()

Clears all items from the stack.

Source code in src/datastructures/liststack.py
70
71
72
73
74
def clear(self):
    """
    Clears all items from the stack.
    """
    raise NotImplementedError("ListStack.clear is not implemented.")

peek()

Returns the top item from the stack without removing it.

Returns:

Name Type Description
T T

The top item from the stack.

Raises:

Type Description
IndexError

If the stack is empty.

Source code in src/datastructures/liststack.py
48
49
50
51
52
53
54
55
56
57
58
def peek(self) -> T:
    """
    Returns the top item from the stack without removing it.

    Returns:
        T: The top item from the stack.

    Raises:
        IndexError: If the stack is empty.
    """
    raise NotImplementedError("ListStack.peek is not implemented.")

pop()

Removes and returns the top item from the stack.

Returns:

Name Type Description
T T

The top item from the stack.

Raises:

Type Description
IndexError

If the stack is empty.

Source code in src/datastructures/liststack.py
36
37
38
39
40
41
42
43
44
45
46
def pop(self) -> T:
    """
    Removes and returns the top item from the stack.

    Returns:
        T: The top item from the stack.

    Raises:
        IndexError: If the stack is empty.
    """
    raise NotImplementedError("ListStack.pop is not implemented.")

push(item)

Pushes an item onto the stack.

Parameters:

Name Type Description Default
item T

The item to push onto the stack.

required

Raises:

Type Description
TypeError

If the item is not of the correct type.

Source code in src/datastructures/liststack.py
23
24
25
26
27
28
29
30
31
32
33
34
def push(self, item: T):
    """
    Pushes an item onto the stack.

    Args:
        item (T): The item to push onto the stack.

    Raises:
        TypeError: If the item is not of the correct type.

    """
    raise NotImplementedError("ListStack.push is not implemented.")