ListStack (LinkedList-based)
Download Source Files NEW
- Download and place the following in your datastructures folder:
- Download and place the following in your tests folder:
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 | |
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 | |
__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 | |
__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 | |
__repr__()
abstractmethod
Returns a string representation of the stack.
Source code in src/datastructures/istack.py
89 90 91 92 | |
__str__()
abstractmethod
Returns a string representation of the stack.
Source code in src/datastructures/istack.py
84 85 86 87 | |
clear()
abstractmethod
Clears the stack.
Source code in src/datastructures/istack.py
46 47 48 49 | |
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 | |
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 | |
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 | |
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 | |
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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
clear()
Clears all items from the stack.
Source code in src/datastructures/liststack.py
70 71 72 73 74 | |
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 | |
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 | |
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 | |