Stack¶
py_ds.datastructures.stack.Stack
¶
Bases: Generic[T]
A simple LIFO (last-in, first-out) stack.
Backed by a dynamic array (Python list). Provides efficient O(1) operations for push, pop, and peek operations.
Initialize the stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[T] | None
|
Optional iterable of initial items. The last item in the iterable should be considered the "top" of the stack. |
None
|
Example
Stack([1, 2, 3]) # 3 is at the top
Source code in src/py_ds/datastructures/stack.py
Functions¶
push
¶
Push a single item onto the top of the stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to push onto the stack. |
required |
Time complexity: O(1) amortized.
pop
¶
Remove and return the top item of the stack.
Returns:
| Type | Description |
|---|---|
T
|
The item that was at the top of the stack. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the stack is empty. |
Time complexity: O(1).
Source code in src/py_ds/datastructures/stack.py
peek
¶
Return the top item of the stack without removing it.
Returns:
| Type | Description |
|---|---|
T
|
The item at the top of the stack. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the stack is empty. |
Time complexity: O(1).
Source code in src/py_ds/datastructures/stack.py
is_empty
¶
Check if the stack is empty.
Returns:
| Type | Description |
|---|---|
bool
|
True if the stack has no elements, False otherwise. |
Time complexity: O(1).
clear
¶
Remove all items from the stack.
After this call, is_empty() returns True and len(stack) == 0.
Time complexity: O(1).
extend
¶
Push multiple items onto the stack, in iteration order.
The last item of items becomes the new top of the stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[T]
|
An iterable of items to push onto the stack. |
required |
Time complexity: O(k), where k is the number of items.
Source code in src/py_ds/datastructures/stack.py
to_list
¶
Convert the stack to a Python list.
Returns:
| Type | Description |
|---|---|
list[T]
|
A shallow copy of the stack contents as a list. The last element |
list[T]
|
of the returned list is the top of the stack. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/stack.py
__len__
¶
Return the number of items in the stack.
Returns:
| Type | Description |
|---|---|
int
|
The number of items in the stack. |
Time complexity: O(1).
__bool__
¶
Return the truthiness of the stack.
Returns:
| Type | Description |
|---|---|
bool
|
False if the stack is empty, True otherwise. |
Enables: if stack: ...
__iter__
¶
Iterate over the items in the stack from top to bottom.
Yields:
| Type | Description |
|---|---|
T
|
Each item in the stack, starting from the top. |
Example
s = Stack([1, 2, 3]) list(s) # [3, 2, 1] (top to bottom)
Source code in src/py_ds/datastructures/stack.py
__repr__
¶
Return a string representation of the stack.
Returns:
| Type | Description |
|---|---|
str
|
A string representation showing the class name and stack contents. |
Example
Stack([1, 2, 3])