双链表的建立
代码如下:
class Node:
def __init__(self,item):
self.item=item
self.prior=None
self.next=None
node=Node(1)
node1=Node(2)
node1.prior=node
node2=Node(3)
node1.next=node2
node2.prior=node1
双链表的删除
class Node:
def __init__(self,item):
self.item=item
self.prior=None
self.next=None
def print_double_link_list(head):
while head:
print(head,end='')
head=head.next
def creat_double_link_list(li):
head=Node(li[0])
foot=head
for j in li[1:]:
node=Node(j)
foot.next=node
node.prior=foot
foot=node
return head
head=creat_double_link_list([1,2,3]) //删除head之后的节点
print_double_link_list(head)
p=head.next
head.next=p.next
p.next.piror=head
del p
print_double_link_list(head)
双链表的插入
class Node:
def __init__(self,item):
self.item=item
self.prior=None
self.next=None
def print_double_link_list(head):
while head:
print(head,end='')
head=head.next
def creat_double_link_list(li):
head=Node(li[0])
foot=head
for j in li[1:]:
node=Node(j)
foot.next=node
node.prior=foot
foot=node
return head
head=creat_double_link_list([1,2,3]) //删除head之后的节点
print_double_link_list(head)
p=Node(4)
p.next=head.next
head.next.prior=p
p.piror=head
head.next=p
print_double_link_list(head)