Files
labs/labPython/lab17/task_4.py
2026-04-18 14:42:00 +05:00

66 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class Node:
def __init__(self, data):
self.data = data
self.prev = self
self.next = self
def create_barrier_list(arr):
barrier = Node(0)
last = barrier
for x in arr:
n = Node(x)
n.prev = last
n.next = barrier
last.next = n
barrier.prev = n
last = n
return barrier
def print_list(barrier):
cur = barrier.next
res = []
while cur != barrier:
res.append(str(cur.data))
cur = cur.next
print("Список:", " ".join(res) if res else "пусто")
def split(barrier, current):
new_barrier = Node(0)
if current == barrier:
return new_barrier
last = barrier.prev
new_barrier.next = current
new_barrier.prev = last
current.prev.next = barrier
barrier.prev = current.prev
current.prev = new_barrier
last.next = new_barrier
return new_barrier
while True:
try:
print("\n1 - Ввести список\n0 - Выход")
cmd = input("Выбор: ")
if cmd == "0":
break
if cmd == "1":
arr = list(map(int, input("Введите числа: ").split()))
barrier = create_barrier_list(arr)
print_list(barrier)
idx = int(input("Индекс текущего элемента (с 1): "))
cur = barrier.next
for _ in range(idx - 1):
if cur != barrier:
cur = cur.next
new_barrier = split(barrier, cur)
print("Первый список:")
print_list(barrier)
print("Второй список:")
print_list(new_barrier)
except:
print("Ошибка")