普通单链表的结尾元素的next=None
,循环链表就是将结尾元素的next=head
-
节点实现(同单链表)
class Node(object): def __init__(self, value): self.value = value self.next = None
-
单向循环列表实现及常用方法
比之单项非循环链表,结尾元素的next指向了head,使其一些方法需要单独考虑对于链表收尾的操作。
class SinglCycleLinkList(object): def __init__(self, node=None): self.head = None if node: # 每加入一个节点,就是一个指向自己的循环链表? node.next = node def is_empty(self): '''判断链表是否为空''' return self.head == None def length(self): '''返回链表长度''' if self.is_empty(): # 如果链表为空 return 0 cur = self.head # 指针 count = 1 # 计数器 while cur.next != self.head: count += 1 cur = cur.next return count def travel(self): '''遍历链表''' if self.is_empty(): return cur = self.head # 指针 while cur.next != self.head: print(cur.value, end=' ') cur = cur.next print(cur.value) def add(self, value): '''头部插入——头插法''' node = Node(value) if self.is_empty(): self.head = node node.next = self.head else: cur = self.head while cur.next != self.head: cur = cur.next node.next = self.head # 把新节点夹在head之前 self.head = node # 链表的新头部编程node cur.next = self.head # 尾部指向新头部 def append(self, value): '''尾部插入——尾插法''' node = Node(value) if self.is_empty(): self.head = node node.next = self.head else: cur = self.head while cur.next != self.head: cur = cur.next cur.next = node node.next = self.head def insert(self, index, value): ''' :param index: the position of insert(start from 0) :param value: node.value :return: ''' node = Node(value) if index <= 0: self.add(node) elif index > self.length(): self.append(node) else: cur = self.head count = 0 while count < index - 1: count += 1 cur = cur.next node.next = cur.next cur.next = node def remove(self, item): '''从链表中删除item''' # 如果为空 if self.head == None: return # 如果第一个命中 if self.head.value == item: # 如果只有一个元素 if self.length() == 1: self.head = None return else: rear = self.head # 用这个索引去找尾 while rear.next != self.head: rear = rear.next self.head = self.head.next rear.next = self.head else: cur = self.head while cur.next != self.head and cur.next.value != item: cur = cur.next if cur.next.value == item: cur.next = cur.next.next def search(self, item): '''在链表中查找item,含有返回True,不含返回False(因为是链表,返回索引也没有意义)''' if self.is_empty(): return False cur = self.head if cur.value == item: return True else: while cur != self.head: cur = cur.next if cur.value == item: return True return False