python实现单向循环链表
普通单链表和单向循环链表的不同之处在于单向循环链表成环,在这种情况下链表指针移动到尾部的判定条件要发生改变,增删改查也要发生改变,但还是以本博客的"python实现单链表"中的代码为基础来实现的。 # 单向循环链表 class Node(object): """节点类""" def __init__(self,elem): self.elem = elem self.next = None class SingLinkList(object): """单向循环链表类""" def __init__(self,node=None): self.__head = node if node: node.next = node def is_empty(self): """判断链表是否为空""" return self.__head == None def length(self): """返回链表的长度""" #如果是空链表则返回0 if self.is_empty(): return 0 #cur游标移动,从而实现遍历元素的功能 cur = self.__head #count用来计数 count = 1 while cur.next != self.__head: count += 1 #让cur游标可以向下移动 cur = cur.next return count def