last lab
This commit is contained in:
44
labPython/lab16/task_1.py
Normal file
44
labPython/lab16/task_1.py
Normal file
@@ -0,0 +1,44 @@
|
||||
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("Ошибка ввода")
|
||||
|
||||
Reference in New Issue
Block a user