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