# 在双链表中插元素
def output1(listvalue, listright, head1):
print(listvalue[head1])
next = listright[head1]
while next > -1:
print(listvalue[next])
next = listright[next]
def output2(listvalue, listleft, head2):
print(listvalue[head2])
next = listleft[head2]
while next > -1:
print(listvalue[next])
next = listleft[next]
listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
listleft = [-1, 5, 1, 0, 2, 3]
head1 = 0
head2 = 4
prepos = 5
output1(listvalue, listright, head1)
output2(listvalue, listleft, head2)
print()
listvalue.append(4)
listright.append(listright[prepos]) # 给新元素的两个指针赋值
listleft.append(prepos)
listleft[listright[prepos]] = len(listvalue) - 1 # 将前后两个元素的指针指向新元素
listright[prepos] = len(listvalue) - 1
# 必须先left再right
output1(listvalue, listright, head1)
output2(listvalue, listleft, head2)
listleft[listright[prepos]] = len(listvalue) - 1 # 将前后两个元素的指针指向新元素
listright[prepos] = len(listvalue) - 1
我将这两个调换位置output2函数就插入不上新元素
为什么 可以解决吗