from dataclasses import dataclass
from typing import ClassVar
@dataclass(repr=False, eq=False)
class Node:
value: int
next: ClassVar = None
@dataclass(repr=False, eq=False)
class Stack:
head: ClassVar = Node("head")
size: ClassVar = 0
def __str__(self):
cur = self.head.next
out = ""
while cur:
out += str(cur.value) + "->"
cur = cur.next
return out[:-3]
def getSize(self):
return self.size
def isEmpty(self):
return self.size == 0
def peek(self):
if self.isEmpty():
raise Exception("Стэк пустой")
return self.head.next.value
def push(self, value):
node = Node(value)
node.next = self.head.next
self.head.next = node
self.size += 1
def pop(self):
if self.isEmpty():
raise Exception("Стэк пустой")
remove = self.head.next
self.head.next = self.head.next.next
self.size -= 1
return remove.value
if __name__ == "__main__":
stack = Stack()
for i in range(1, 11):
stack.push(i)
print(f"Stack: {stack}")
for _ in range(1, 6):
remove = stack.pop()
print(f"Pop: {remove}")
print(f"Stack: {stack}")