Skip to content

Instantly share code, notes, and snippets.

@eduardogpg
Last active June 27, 2023 21:16
Show Gist options
  • Save eduardogpg/3d19ee23cb3a481ad9a9f3cb1434a3cc to your computer and use it in GitHub Desktop.
Save eduardogpg/3d19ee23cb3a481ad9a9f3cb1434a3cc to your computer and use it in GitHub Desktop.
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