This commit is contained in:
2026-04-18 14:42:00 +05:00
commit ef77f101fa
96 changed files with 3134 additions and 0 deletions

63
labPython/lab17/task_1.py Normal file
View File

@@ -0,0 +1,63 @@
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def create_list(arr):
if not arr:
return None
head = Node(arr[0])
cur = head
for x in arr[1:]:
n = Node(x)
cur.next = n
n.prev = cur
cur = n
return head
def print_list(head):
res = []
while head:
res.append(str(head.data))
head = head.next
print("Список:", " ".join(res) if res else "null")
def remove_odd_positions(head):
index = 1
cur = head
new_head = None
last = None
while cur:
nxt = cur.next
if index % 2 == 0:
if not new_head:
new_head = cur
cur.prev = None
else:
last.next = cur
cur.prev = last
last = cur
index += 1
cur = nxt
if last:
last.next = None
return new_head
while True:
try:
print("\n1 - Ввести список\n0 - Выход")
cmd = input("Выбор: ")
if cmd == "0":
break
if cmd == "1":
arr = list(map(int, input("Введите числа: ").split()))
head = create_list(arr)
head = remove_odd_positions(head)
print_list(head)
except:
print("Ошибка ввода")

58
labPython/lab17/task_2.py Normal file
View File

@@ -0,0 +1,58 @@
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def create_list(arr):
if not arr:
return None, None
head = Node(arr[0])
cur = head
for x in arr[1:]:
n = Node(x)
cur.next = n
n.prev = cur
cur = n
return head, cur
def print_list(head):
res = []
cur = head
while cur:
res.append(str(cur.data))
cur = cur.next
print("Список:", " ".join(res))
def shift(head, last, k):
if not head:
return head, last
last.next = head
head.prev = last
cur = head
for _ in range(k):
cur = cur.next
new_head = cur
new_last = cur.prev
new_last.next = None
new_head.prev = None
return new_head, new_last
while True:
try:
print("\n1 - Ввести список\n0 - Выход")
cmd = input("Выбор: ")
if cmd == "0":
break
if cmd == "1":
arr = list(map(int, input("Введите числа: ").split()))
k = int(input("Введите K: "))
head, last = create_list(arr)
head, last = shift(head, last, k)
print_list(head)
except:
print("Ошибка")

80
labPython/lab17/task_3.py Normal file
View File

@@ -0,0 +1,80 @@
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class IntList:
def __init__(self, head):
self.first = head
self.last = head
self.current = head
while self.last and self.last.next:
self.last = self.last.next
def ToFirst(self):
self.current = self.first
def ToNext(self):
if self.current and self.current.next:
self.current = self.current.next
def SetData(self, d):
if self.current:
self.current.data = d
def IsLast(self):
return self.current == self.last
def process(self):
self.ToFirst()
index = 1
count = 0
while self.current:
count += 1
if index % 2 == 1:
self.SetData(0)
if self.IsLast():
break
self.ToNext()
index += 1
return count
def print_list(self):
cur = self.first
res = []
while cur:
res.append(str(cur.data))
cur = cur.next
print("Список:", " ".join(res))
def create(arr):
if not arr:
return None
head = Node(arr[0])
cur = head
for x in arr[1:]:
n = Node(x)
cur.next = n
n.prev = cur
cur = n
return head
while True:
try:
print("\n1 - Ввести список\n0 - Выход")
cmd = input("Выбор: ")
if cmd == "0":
break
if cmd == "1":
arr = list(map(int, input("Введите числа: ").split()))
head = create(arr)
lst = IntList(head)
n = lst.process()
print("Количество:", n)
lst.print_list()
except:
print("Ошибка")

65
labPython/lab17/task_4.py Normal file
View File

@@ -0,0 +1,65 @@
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("Ошибка")