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