45 lines
1017 B
Python
45 lines
1017 B
Python
class Node:
|
|
def __init__(self, data, next=None):
|
|
self.data = data
|
|
self.next = next
|
|
|
|
|
|
def push(top, d):
|
|
return Node(d, top)
|
|
|
|
|
|
def print_stack(top):
|
|
cur = top
|
|
while cur:
|
|
print(cur.data, end=" ")
|
|
cur = cur.next
|
|
print()
|
|
|
|
|
|
top = None
|
|
|
|
while True:
|
|
try:
|
|
print("\n1 - создать стек")
|
|
print("2 - добавить элемент")
|
|
print("3 - показать стек")
|
|
print("0 - выход")
|
|
c = input(">> ")
|
|
|
|
if c == "1":
|
|
top = None
|
|
arr = list(map(int, input("Введите числа: ").split()))
|
|
for x in arr:
|
|
top = push(top, x)
|
|
elif c == "2":
|
|
d = int(input("Введите D: "))
|
|
top = push(top, d)
|
|
print("Новая вершина:", top.data if top else None)
|
|
elif c == "3":
|
|
print_stack(top)
|
|
elif c == "0":
|
|
break
|
|
except:
|
|
print("Ошибка ввода")
|
|
|