class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def get_length(self): l_len = 0 tmp_node = self.head pre_node = None if self.head is None: return l_len while tmp_node is not None: l_len = l_len + 1 pre_node = tmp_node tmp_node = pre_node.next return l_len def getIndex(self, value): tmp_node = self.head pre_node = None n = 0 if not tmp_node: # 空链表 return None while value != tmp_node.data: n = n + 1 pre_node = tmp_node tmp_node = pre_node.next if not tmp_node: # 每次后移,都判断一下tmp_node是否为空 return None return n def listprint(self): preval = self.head while preval: print(preval.data) preval = preval.next def add_begin(self, NewNode=None): if self.head is None: self.head = NewNode else: NewNode.next = self.head self.head = NewNode def append(self, NewNode=None): if self.head is None: self.head = NewNode else: tmp_node = self.head while tmp_node.next: tmp_node = tmp_node.next tmp_node.next = NewNode def search(self, Needed_Node=None): tmp_node = self.head if self.head is None: return False while tmp_node: if tmp_node.data == Needed_Node.data: break tmp_node = tmp_node.next if not tmp_node: return False return True def remove(self, value): if not self.search(Node(value)): return False pre_node = None tmp_node = self.head if value == tmp_node.data: self.head = tmp_node.next tmp_node.next = None return True while value != tmp_node.data: pre_node = tmp_node tmp_node = pre_node.next pre_node.next = tmp_node.next tmp_node.next = None return True # 往后插入 def insert(self, n, value): new_node = Node(value) node_n = 0 # 设置成参数变量, 更合理一些 tmp_node = self.head pre_node = None if self.head is None: self.head = new_node if n <= 0: self.add_begin(new_node) print(self.listprint()) return True if n >= self.get_length(): self.append(new_node) print(self.listprint()) return True while n != node_n: pre_node = tmp_node tmp_node = pre_node.next node_n = node_n + 1 new_node.next = tmp_node pre_node.next = new_node print(self.listprint()) return True # 单链反转 def reverse_list(self): pre_node = self.head node = pre_node.next tmp = None if self.head == None or self.head.next == None: return self.head while node: tmp = node.next # 把第三个节点地址赋值给tmp,因为接下来要改node.next了 node.next = pre_node # 让第二个节点直接指向第一个节点; pre_node = node # 核心: 主要容易出现理解偏差,node.next变了,node却没变 node = tmp # 跳出循环后,pre_node为最后一个节点,node、tmp在同一位置None; self.head.next = None # 从始至终,self.head容器里的地址始终没变;self.head.next容器里的地址也没变; self.head = pre_node # 让self.head容器里装上pre_node中的地址; self.listprint() return True if __name__ == ‘__main__‘: # 1、创建链表对象 lk = LinkedList() lk2 = LinkedList() lk.head = Node(‘lin‘) # 2、创建节点 e2 = Node(‘chen‘) e3 = Node(‘son‘) # 3、连接节点( 为何不是连接到e2.data? ) lk.head.next = e2 e2.next = e3 # print(id(e2)) # print(id(e2.data)) # # lk.add_begin(NewNode=Node(‘wang‘)) # lk.append(NewNode=Node(‘888bu‘)) # lk.listprint() # print(lk.remove(value=‘chen‘)) # lk.listprint() # print(lk.getIndex(‘lin‘)) # print(lk.get_length()) # print(lk.search(Needed_Node=Node())) # print(lk.insert(2, ‘soo‘)) print(‘反转前:‘) lk.listprint() print(‘---------------‘) print(‘反转后:‘) lk.reverse_list() pass
原文:https://www.cnblogs.com/wolfstark/p/15265526.html