78 lines
1.5 KiB
Python
78 lines
1.5 KiB
Python
class Node:
|
|
def __init__(self, data):
|
|
self.data = data
|
|
self.next = None
|
|
|
|
|
|
def build(arr):
|
|
head = None
|
|
for x in reversed(arr):
|
|
node = Node(x)
|
|
node.next = head
|
|
head = node
|
|
return head
|
|
|
|
|
|
def process(head, m):
|
|
if not head:
|
|
return head
|
|
|
|
dummy = Node(0)
|
|
dummy.next = head
|
|
prev = dummy
|
|
cur = head
|
|
i = 1
|
|
|
|
while cur:
|
|
if i % 2 == 0:
|
|
node = Node(m)
|
|
prev.next = node
|
|
node.next = cur
|
|
prev = cur
|
|
cur = cur.next
|
|
i += 1
|
|
else:
|
|
prev = cur
|
|
cur = cur.next
|
|
i += 1
|
|
|
|
return dummy.next
|
|
|
|
|
|
def print_list(h):
|
|
while h:
|
|
print(h.data, end=" ")
|
|
h = h.next
|
|
print()
|
|
|
|
|
|
def print_last_ref(h):
|
|
if not h:
|
|
print("None")
|
|
return
|
|
while h.next:
|
|
h = h.next
|
|
print(h)
|
|
|
|
|
|
while True:
|
|
try:
|
|
print("\n1 - создать список")
|
|
print("2 - обработать")
|
|
print("3 - вывести ссылку на последний элемент")
|
|
print("0 - выход")
|
|
c = input(">> ")
|
|
|
|
if c == "1":
|
|
head = build(list(map(int, input().split())))
|
|
elif c == "2":
|
|
m = int(input("M: "))
|
|
head = process(head, m)
|
|
print_list(head)
|
|
elif c == "3":
|
|
print_last_ref(head)
|
|
elif c == "0":
|
|
break
|
|
except:
|
|
print("Ошибка")
|