Last active
June 27, 2023 21:16
-
-
Save eduardogpg/3d19ee23cb3a481ad9a9f3cb1434a3cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Optional, Any | |
| class StackUnderFlowError(Exception): | |
| def __init__(self): | |
| super().__init__('Stack Under Flow error') | |
| class Node: | |
| def __init__(self, data: Any = None) -> None: | |
| self.value: Any = data | |
| self.next: Optional[Node] = None | |
| class StackLL: | |
| def __init__(self) -> None: | |
| self.top: Optional[Node] = None | |
| self.size: int = 0 | |
| def push(self, element: Any) -> None: | |
| new_node: Node = Node(element) | |
| new_node.next = self.top # None | |
| self.top = new_node | |
| def pop(self) -> Any: | |
| if self.empty: | |
| raise StackUnderFlowError() | |
| element: Any = self.top.value | |
| self.top = self.top.next | |
| return element | |
| def peek(self) -> Any: | |
| if self.empty: | |
| raise StackUnderFlowError() | |
| return self.top.value | |
| @property | |
| def empty(self) -> bool: | |
| return self.top is None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment