class Node():
def __init__(self, value):
self.value = value
self.next = None
def setnext(self, n_next):
self.next = n_next
class Linklist():
def __init__(self, head):
self.head = head
self.tail = head
self.ListNode = []
self.ListValue = []
def addnode(self, value):#添加
node = Node(value)
self.tail.setnext(node)
self.tail = node
def getnode(self, node):
return node.value, node.next
def traversing(self): # 遍历
self.ListNode=[]
self.ListValue=[]
value, n_ext = self.getnode(self.head)
self.ListNode.append(self.head)
self.ListValue.append(value)
while (n_ext):
self.ListNode.append(n_ext)
value, n_ext = self.getnode(n_ext)
self.ListValue.append(value)
def insertnode(self,node1, node2, value):#插入
node = Node(value)
node1.setnext(node)
node.setnext(node2)
data = [1, 2, 3, 4, 5, 7, 8, 9, 10]
head = Node(1)
linkList = Linklist(head)
for i in range(len(data) - 1):
linkList.addnode(data[i + 1])
linkList.traversing()
print(len(linkList.ListValue))
print(len(linkList.ListNode))
print(linkList.ListValue)
print(linkList.ListNode)
linkList.insertnode(linkList.ListNode[4],linkList.ListNode[5],6)
linkList.traversing()
print(len(linkList.ListValue))
print(len(linkList.ListNode))
print(linkList.ListValue)
print(linkList.ListNode)
输出
9
9
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[<__main__.Node object at 0x00000204AD301BA8>, <__main__.Node object at 0x00000204AD301C18>, <__main__.Node object at 0x00000204AD301C50>, <__main__.Node object at 0x00000204AD301C88>, <__main__.Node object at 0x00000204AD301CC0>, <__main__.Node object at 0x00000204AD301CF8>, <__main__.Node object at 0x00000204AD301D30>, <__main__.Node object at 0x00000204AD301D68>, <__main__.Node object at 0x00000204AD301DA0>]
10
10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[<__main__.Node object at 0x00000204AD301BA8>, <__main__.Node object at 0x00000204AD301C18>, <__main__.Node object at 0x00000204AD301C50>, <__main__.Node object at 0x00000204AD301C88>, <__main__.Node object at 0x00000204AD301CC0>, <__main__.Node object at 0x00000204AD301DD8>, <__main__.Node object at 0x00000204AD301CF8>, <__main__.Node object at 0x00000204AD301D30>, <__main__.Node object at 0x00000204AD301D68>, <__main__.Node object at 0x00000204AD301DA0>]