LK_03_schusterje/stack.py

27 lines
597 B
Python
Raw Normal View History

2024-12-04 08:58:55 +01:00
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
raise IndexError("Pop von leerem Stack")
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
raise IndexError("Peek von leerem Stack")
def size(self):
return len(self.items)
def __str__(self):
return str(self.items)