Stack ADT
Stack (ABC)
A stack is a Last-In-First-Out ADT that supports insertion (pushing) and removal (popping) of elements from the same end. The order in which elements are pushed into the stack is reversed when popping out elements from the stack.
__len__(self)
special
Get the total number of elements stored in the stack
Returns:
Type | Description |
---|---|
int |
count of elements in stack |
Source code in stacks/stack.py
@abstractmethod
def __len__(self) -> int:
"""Get the total number of elements stored in the stack
:returns: count of elements in stack
"""
pass
is_empty(self)
Check if stack contains no elements
Returns:
Type | Description |
---|---|
bool |
True if stack is empty, else False |
Source code in stacks/stack.py
@abstractmethod
def is_empty(self) -> bool:
"""Check if stack contains no elements
:return: True if stack is empty, else False
"""
pass
peek(self)
Get element at the top of the stack without removing it
Returns:
Type | Description |
---|---|
Any |
element at top of stack |
Source code in stacks/stack.py
@abstractmethod
def peek(self) -> Any:
"""Get element at the top of the stack without removing it
:return: element at top of stack
"""
pass
pop(self)
Get element at the top of the stack, and remove it from the stack
Returns:
Type | Description |
---|---|
Any |
element at top of stack |
Source code in stacks/stack.py
@abstractmethod
def pop(self) -> Any:
"""Get element at the top of the stack, and remove it from the stack
:return: element at top of stack
"""
pass
push(self, x)
Insert element x to the top of the stack
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any |
element to add to the stack |
required |
Source code in stacks/stack.py
@abstractmethod
def push(self, x: Any) -> None:
"""Insert element x to the top of the stack
:param x: element to add to the stack
"""
pass