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

2
labPython/lab16/data.txt Normal file
View File

@@ -0,0 +1,2 @@
10
70 65 39 94 8 16 22 58 63 15

44
labPython/lab16/task_1.py Normal file
View 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("Ошибка ввода")

75
labPython/lab16/task_2.py Normal file
View File

@@ -0,0 +1,75 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None
def enqueue(head, tail, d):
node = Node(d)
if not head:
return node, node
tail.next = node
return head, node
def merge(h1, h2):
if not h1:
return h2
if not h2:
return h1
if h1.data < h2.data:
head = h1
h1 = h1.next
else:
head = h2
h2 = h2.next
cur = head
while h1 and h2:
if h1.data < h2.data:
cur.next = h1
h1 = h1.next
else:
cur.next = h2
h2 = h2.next
cur = cur.next
cur.next = h1 if h1 else h2
return head
def print_q(h):
while h:
print(h.data, end=" ")
h = h.next
print()
h1 = t1 = None
h2 = t2 = None
while True:
try:
print("\n1 - создать очередь 1")
print("2 - создать очередь 2")
print("3 - объединить")
print("0 - выход")
c = input(">> ")
if c == "1":
h1 = t1 = None
for x in map(int, input().split()):
h1, t1 = enqueue(h1, t1, x)
elif c == "2":
h2 = t2 = None
for x in map(int, input().split()):
h2, t2 = enqueue(h2, t2, x)
elif c == "3":
res = merge(h1, h2)
print_q(res)
elif c == "0":
break
except:
print("Ошибка")

43
labPython/lab16/task_3.py Normal file
View File

@@ -0,0 +1,43 @@
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 find_second(head):
count = 0
while head:
if head.data % 6 == 0:
count += 1
if count == 2:
return head
head = head.next
return None
while True:
try:
print("\n1 - создать список")
print("2 - найти элемент")
print("0 - выход")
c = input(">> ")
if c == "1":
arr = list(map(int, input().split()))
head = build(arr)
elif c == "2":
res = find_second(head)
print(res.data if res else "nil")
elif c == "0":
break
except:
print("Ошибка")

77
labPython/lab16/task_4.py Normal file
View File

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

56
labPython/lab16/task_5.py Normal file
View File

@@ -0,0 +1,56 @@
# task: ListWork62 / Создание убывающего списка из файла
import random
class Node:
def __init__(self, data):
self.data = data
self.next = None
def insert_desc(head, x):
node = Node(x)
if not head or x > head.data:
node.next = head
return node
cur = head
while cur.next and cur.next.data > x:
cur = cur.next
node.next = cur.next
cur.next = node
return head
def print_list(h):
while h:
print(h.data, end=" ")
h = h.next
print()
while True:
try:
print("\n1 - создать файл")
print("2 - обработать файл")
print("0 - выход")
c = input(">> ")
if c == "1":
n = int(input("N: "))
arr = [random.randint(1, 100) for _ in range(n)]
with open("data.txt", "w") as f:
f.write(str(n) + "\n")
f.write(" ".join(map(str, arr)))
elif c == "2":
with open("data.txt") as f:
n = int(f.readline())
arr = list(map(int, f.readline().split()))
head = None
for x in arr:
head = insert_desc(head, x)
print_list(head)
elif c == "0":
break
except:
print("Ошибка")