def travel(self):
# print(self.head.item)
# print(self.head.next.item)
# print(self.head.next.next.item)
#head在链表创建
cur = self.head
while cur:
print(cur.item)
cur = cur.next
Link实例-isEmpty
def isEmpty(self):
return self.head == None
Link实例-size
def size(self):
cur = self.head
count = 0
while cur:
count +=1
cur = cur.next
return count
Link实例-append(尾插入)
def append(self,item):
node = Node(item)
#特殊情况,判断是否为空
if self.head == None:
self.head = node
cur = self.head
pre = None
while cur:
pre = cur
cur = cur.next
pre.next = node
Link实例-search(查找某个元素)
def search(self,item):
find = False
cur = self.head
while cur:
if cur.item == item:
find = True
break
cur = cur.next
return find
Link实例-insert(指定位置插入)
def insert(self,pos,item):
node = Node(item)
pre = None
cur = self.head
for i in range(pos):
pre = cur #先等于再改变
cur = cur.next #改变到下一个节点值
pre.next = node #把新插入的节点链接到上一个节点的next
node.next = cur #新插入的节点指向下一个节点
Link实例-remove(指定位置删除)
def remove(self,item):
cur = self.head
pre = Node
#删除第一个节点
if cur.item == item:
self.head = cur.next
return
while cur:
pre = cur
cur = cur.next
if cur.item == item:
pre.next = cur.next
return