22 - python & all web labs

This commit is contained in:
2026-05-02 20:27:02 +05:00
parent ef77f101fa
commit 19af922baf
5781 changed files with 770492 additions and 96 deletions

64
labPython/lab21/task_5.py Normal file
View File

@@ -0,0 +1,64 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
class DoublyCircularList:
def __init__(self):
self.head = None
def append(self, value):
new_node = Node(value)
if not self.head:
self.head = new_node
new_node.next = new_node
new_node.prev = new_node
else:
tail = self.head.prev
tail.next = new_node
new_node.prev = tail
new_node.next = self.head
self.head.prev = new_node
def __iter__(self):
return ReverseIterator(self.head)
class ReverseIterator:
def __init__(self, head):
self.current = head.prev if head else None
def __iter__(self):
return self
def __next__(self):
if not self.current:
raise StopIteration
val = self.current.value
self.current = self.current.prev
return val
while True:
print("1 - Создать список и начать итерацию")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
data = input("Введите элементы через пробел: ").split()
lst = DoublyCircularList()
for x in data:
lst.append(x)
it = iter(lst)
n = int(input("Сколько элементов вывести: "))
for _ in range(n):
print(next(it))
except:
print("Ошибка")