59 lines
1.3 KiB
Python
59 lines
1.3 KiB
Python
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("Ошибка")
|