76 lines
1.5 KiB
Python
76 lines
1.5 KiB
Python
class Node:
|
|
def __init__(self, data):
|
|
self.data = data
|
|
self.next = None
|
|
|
|
|
|
def enqueue(head, tail, d):
|
|
node = Node(d)
|
|
if not head:
|
|
return node, node
|
|
tail.next = node
|
|
return head, node
|
|
|
|
|
|
def merge(h1, h2):
|
|
if not h1:
|
|
return h2
|
|
if not h2:
|
|
return h1
|
|
|
|
if h1.data < h2.data:
|
|
head = h1
|
|
h1 = h1.next
|
|
else:
|
|
head = h2
|
|
h2 = h2.next
|
|
|
|
cur = head
|
|
|
|
while h1 and h2:
|
|
if h1.data < h2.data:
|
|
cur.next = h1
|
|
h1 = h1.next
|
|
else:
|
|
cur.next = h2
|
|
h2 = h2.next
|
|
cur = cur.next
|
|
|
|
cur.next = h1 if h1 else h2
|
|
return head
|
|
|
|
|
|
def print_q(h):
|
|
while h:
|
|
print(h.data, end=" ")
|
|
h = h.next
|
|
print()
|
|
|
|
|
|
h1 = t1 = None
|
|
h2 = t2 = None
|
|
|
|
while True:
|
|
try:
|
|
print("\n1 - создать очередь 1")
|
|
print("2 - создать очередь 2")
|
|
print("3 - объединить")
|
|
print("0 - выход")
|
|
c = input(">> ")
|
|
|
|
if c == "1":
|
|
h1 = t1 = None
|
|
for x in map(int, input().split()):
|
|
h1, t1 = enqueue(h1, t1, x)
|
|
elif c == "2":
|
|
h2 = t2 = None
|
|
for x in map(int, input().split()):
|
|
h2, t2 = enqueue(h2, t2, x)
|
|
elif c == "3":
|
|
res = merge(h1, h2)
|
|
print_q(res)
|
|
elif c == "0":
|
|
break
|
|
except:
|
|
print("Ошибка")
|