last lab
This commit is contained in:
80
labPython/lab17/task_3.py
Normal file
80
labPython/lab17/task_3.py
Normal 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("Ошибка")
|
||||
Reference in New Issue
Block a user