Files
DCS229/Project 5/Stack.py
2026-03-15 16:28:26 -04:00

80 lines
2.0 KiB
Python

######################################
# DCS 229 -- Stack
# Stack data structure implemented with Linked List
# Date:
# Name:
# Resources Used: LinkedList.py from Project 4
##########################################
from LinkedList import *
class EmptyError(Exception):
''' class extending Exception to better document stack errors '''
def __init__(self, message: str):
self.message = message
class Stack[T]:
''' class to implement a stack ADT using our LinkedList class'''
def __init__(self):
''' initializes an empty stack '''
self._data = LinkedList()
def __len__(self) -> int:
''' allows the len function to be called using an ArrayStack object, e.g.,
stack = ArrayStack()
print(len(stack))
Returns:
number of elements in the stack, as an integer
'''
return len(self._data)
def push(self, item: T) -> None:
''' pushes a given item of arbitrary type onto the stack
Parameters:
item: an item of arbitrary type
Returns:
None
'''
self._data.insert_head(item)
def pop(self) -> T:
''' removes the topmost element from the stack and returns that element
Returns:
the topmost item, of arbitrary type
Raises:
EmptyError exception if the stack is empty
'''
pass
def peek(self) -> T:
''' returns the topmost element from the stack without modifying the stack
Returns:
the topmost item, of arbitrary type
Raises:
EmptyError exception if the stack is empty
'''
pass
def is_empty(self) -> bool:
''' indicates whether the stack is empty
Returns:
True if the stack is empty, False otherwise
'''
pass
def __str__(self) -> str:
''' returns an str implementation of the Stack '''
return str(self._data)
def main():
# add your tests here!
pass
if __name__ == "__main__":
main()