# current 表示一个变量,用来表示当前节点 # 用类来创造节点 class Node: def __init__(self, data): self.data = data self.next = None class SingleList: def __init__(self, node=None): self._head = None if node is None else node # 其他方法保持不变 # 判断链表是否为空: def is_empty(self): return self._head is None # 如果头节点是none,表示链表为空 # 求链表长度 def length(self): current = self._head # 将当前节点设置为当前链表的头节点 count = 0 # 记录节点数量 while current is not None: # 如果当前节点是None,表示已经到达链表的末尾,跳出循环 count += 1 current = current.next # 到下一个节点 return current # 遍历链表 def travel(self): current = self._head # 将当前链表设置为链表的头节点 while current is not None: # 当节点为None时,表示到达了链表的末尾 # 然后使用while来循环遍历整个链表 print("{0}".format(current.data), end="") # 打印当前链表的数据 current = current.next # 将节点更新为下一个节点 print("") # 在链表头部插入节点,头插法 # 头插法,在链表的头部插入一个新节点,新节点的next指向原来的头节点,然后将头节点更新为新节点 def add(self, item): node = Node(item)# 创造一个新节点,值为传入的item if self.is_empty():# 如果链表为空 self._head = node# 将新节点作为头节点 else: node.next = self._head # 将新节点的下一个节点指向当前头节点 self._head = node# 更新头节点为新节点 # 在链表尾部插入元素,尾插法 # 尾插法,遍历链表,找到最后一个节点,将新节点插入到最后一个节点的后面 def append(self, item): # 创造一个新节点,值为传入的item node = Node(item)# 如果链表为空 if self.is_empty():# 将新节点作为头节点 self._head = node else: # 如果链表不为空 current = self._head# 从头开始遍历链表 while current.next is not None:# 找到链表的最后一个节点 current = current.next# 移动到下一个节点 current.next = node# 将最后一个节点的下一个节点指向新节点 # 在某个位置插入链表 # 两个特殊位置 # 在0位置,add # 在最后一个位置,append # 在插入一个节点的时候要考虑当前位置的前一个节点 def insert(self, item, pos): # item为插入的元素,pos表示插入的位置 if pos <= 0: # 如果pos的位置小于0,表示要插入到链表的开头,因此调用self.add(item)将元素添加到到链表的开头 self.add(item) elif pos > (self.length() - 1): # 如果pos大于链表的长度-1,表示要插入到链表的末尾,调用self.append(item) self.append(item) else: node = Node(item)# 创建一个新节点,并将item赋值给该节点的val属性 pre = self._head# 初始化一个指针pre,将其指向链表的头节点self._head count = 0 # 初始化一个计数器,值为0 while count < (pos - 1): # 使用一个循环来遍历链表,直到找到要插入的位置的前一个节点。循环中,每次将pre指针向后移动一位,并将count加一,直到count等于pos-1 pre = pre.next count += 1 node.next = pre.next# 在循环结束后,pre指针指向了要插入位置的前一个节点,并将新节点的node的next属性设置为pre.next,即将新节点插入到前一个节点之后 pre.next = node# 最后,将前一个节点的next属性设置为新节点node,即将新节点连接到链表中 # 删除节点 def remove(self, item): current = self._head # 获取链表的头节点 if self.is_empty(): # 如果链表为空,则直接返回 return elif current.data == item: # 如果头节点就是要删除的元素,则将头节点指向下一个节点 self._head = current.next else: previous = None # 初始化前一个节点为None while current is not None and current.data != item: # 遍历节点,直到找到要删除的元素或者到达链表末尾 previous = current # 更新前一个节点为当前节点 current = current.next # 移动到下一个节点 previous.next = current.next # 将前一个节点的next指针指向当前节点的下一个节点,从而跳过当前节点 # 寻找链表中的节点 def search(self, item): found = False # 定义了一个布尔变量found,将初始值设定为false,用来标记是否找到目标元素 if self.is_empty():# 检查链表是否为空 return found else: # 如果当前链表不为空 current = self._head # 则将当前链表初始化为头节点self._head while current is not None and not found: # 当前节点不为空并且未找到目标元素 if current.data == item:# 首先判断当前节点是否等于目标元素,如果是,则将found设置为true found = True else:# 否则将节点更新为下一个节点,继续进行下一次循环 current = current.next return found my_list = SingleList() if __name__ == "__main__": my_list = SingleList() print("add操作:") my_list.add(98) my_list.add(99) my_list.travel() print("{:#^50}".format("")) print("append操作:") my_list.append(100) my_list.append(101) my_list.append(102) my_list.append(103) my_list.travel() print("{:#^50}".format("")) print("insert操作:") my_list.insert(99, 0) my_list.insert(104, 10) my_list.insert(19, 3) my_list.insert(56, 5) my_list.travel() print("{:#^50}".format("")) print("remove操作:") my_list.remove(19) my_list.remove(56) my_list.travel() print("{:#^50}".format("")) print("search操作:") print(my_list.search(1000)) print(my_list.search(101)) print("链表长为:{0}".format(my_list.length()))
python实现单链表
最新推荐文章于 2024-07-13 23:51:10 发布