# 尾部添加元素 defadd_last(self, data): node = Node(data) # 如果链表为空,需要特殊处理 if self.is_empty(): self.head = node else: cur = self.head while cur.nextisnotNone: cur = cur.next # 退出循环的时候,curl指向尾结点 cur.next = node
2.5 指定位置插元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 在指定位置添加元素 definsert_node(self, index, data): node = Node(data) if index < 0or index > self.length(): returnFalse elif index == 0: self.add_fist() elif index == self.length(): self.add_last() else: cur = self.head count = 0 while count < (index - 1): count += 1 cur = cur.next # 退出循环的时候,cur指向index的前一个位置 node.next = cur.next cur.next = node
2.6 删除节点
1 2 3 4 5 6 7 8 9 10 11 12
# 删除指定节点 defremove_node(self, data): cur = self.head # 指针指向的结点 pre = None# 指针指向结点的前一个 if self.head == data: self.head.next = self.head else: while cur.data isnot data: # 不是要找的元素,移动游标 pre = cur cur = cur.next pre.next = cur.next
2.7 查找节点
1 2 3 4 5 6 7 8 9
# 查找节点 defsearch_node(self, data): cur = self.head while cur isnotNone: if cur.data == data: returnTrue else: cur = cur.next returnFalse
2.8 打印链表
1 2 3 4 5 6 7 8 9 10 11 12
# 遍历 打印链表 deftraverse_to_print_node(self): # cur = self.head # while cur is not None: # print(cur.data, end = " ") # cur = cur.next print_list = [] cur = self.head while cur isnotNone: print_list.append(str(cur.data)) cur = cur.next print('->'.join(print_list))
2.9 测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13
# 测试 if __name__ == '__main__': list = SingleLinkedList() list.add_fist(2) list.add_fist(1) list.add_last(4) list.insert_node(2, 3) list.traverse_to_print_node() print(list.is_empty()) print(list.length()) list.remove_node(4) print(list.search_node(3)) list.traverse_to_print_node()
# 尾部添加元素 defadd_last(self, data): node = Node(data) # 如果链表为空,需要特殊处理 if self.is_empty(): self.head = node else: cur = self.head while cur.nextisnotNone: cur = cur.next # 退出循环的时候,curl指向尾结点 cur.next = node
# 在指定位置添加元素 definsert_node(self, index, data): node = Node(data) if index < 0or index > self.length(): returnFalse elif index == 0: self.add_fist() elif index == self.length(): self.add_last() else: cur = self.head count = 0 while count < (index - 1): count += 1 cur = cur.next # 退出循环的时候,cur指向index的前一个位置 node.next = cur.next cur.next = node
# 删除指定节点 defremove_node(self, data): cur = self.head # 指针指向的结点 pre = None# 指针指向结点的前一个 if self.head == data: self.head.next = self.head else: while cur.data isnot data: # 不是要找的元素,移动游标 pre = cur cur = cur.next pre.next = cur.next
# 查找节点 defsearch_node(self, data): cur = self.head while cur isnotNone: if cur.data == data: returnTrue else: cur = cur.next returnFalse
# 遍历 打印链表 deftraverse_to_print_node(self): # cur = self.head # while cur is not None: # print(cur.data, end = " ") # cur = cur.next print_list = [] cur = self.head while cur isnotNone: print_list.append(str(cur.data)) cur = cur.next print('->'.join(print_list))
# 测试 if __name__ == '__main__': list = SingleLinkedList() list.add_fist(2) list.add_fist(1) list.add_last(4) list.insert_node(2, 3) list.traverse_to_print_node() print(list.is_empty()) print(list.length()) list.remove_node(4) print(list.search_node(3)) list.traverse_to_print_node()