class Node:
'''节点'''
def __init__(self, elem=None):
self.elem = elem
self.next = None
class SingleCycleLinkList:
'''单向循环链表'''
def __init__(self, node=None):
self.__head = node
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 # cur 游标,用来移动遍历节点
count = 1 # count 记数
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.elem, end=' ')
cur = cur.next
print(cur.elem)
def add(self, item):
'''链表头部添加元素,头插法'''
node = Node(item)
if self.is_empty():
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
node.next = self.__head
self.__head = node
cur.next = self.__head # 循环退出后,cur指向尾节点,或cur.next = node
def append(self, item):
'''链表尾部添加元素,尾插法'''
node = Node(item)
if self.is_empty():
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
node.next = self.__head # 或node.next = cur.next
cur.next = node
def insert(self, pos, item):
'''指定位置添加元素
:param pos 位置,从0开始
'''
if pos <= 0:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
node = Node(item)
pre = self.__head
count = 0
while count < pos-1:
count += 1
pre = pre.next
node.next = pre.next # 循环退出后,pre指向pos-1位置
pre.next = node
def remove(self, item):
'''删除节点'''
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next != self.__head:
if cur.elem == item:
if cur == self.__head: # 判断该节点是否是头结点
# 找尾节点
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
else:
pre.next = cur.next # 中间结点
return
else:
pre = cur
cur = cur.next
if cur.elem == item: # 循环退出后,cur指向尾节点,判断该节点是否为尾节点
if cur == self.__head: # 判断链表是否只有一个节点
self.__head = None
else:
pre.next = self.__head # 或pre.next = cur.next
def search(self, item):
'''查找节点是否存在'''
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.elem == item:
return True
else:
cur = cur.next
if cur.elem == item:
return True
return False
scll = SingleCycleLinkList() # 创建单向循环链表
scll.is_empty() # 链表是否为空
True
scll.length() # 链表长度
0
尾部插入
# 将数字1 2 3 4 5 6从尾部插入链表
scll.append(1)
scll.append(2)
scll.append(3)
scll.append(4)
scll.append(5)
scll.append(6)
scll.travel() # 遍历链表
1 2 3 4 5 6
头部插入
scll.add(10) # 将数字10从头部插入链表
scll.travel()
10 1 2 3 4 5 6
指定位置插入
scll.insert(-1, 20) # 将数字20在链表位置-1处插入(位置小于等于0时从链表头部插入)
scll.travel()
20 10 1 2 3 4 5 6
scll.insert(2, 30) # 将数字30在链表位置2处插入
scll.travel()
20 10 30 1 2 3 4 5 6
scll.insert(100, 40) # 将数字40在链表位置100处插入(位置超出链表长度时从尾部插入)
scll.travel()
20 10 30 1 2 3 4 5 6 40
scll.remove(20) # 将数字20从链表中删除
scll.travel()
10 30 1 2 3 4 5 6 40
scll.remove(40) # 将数字40从链表中删除
scll.travel()
10 30 1 2 3 4 5 6
scll.remove(3) # 将数字3从链表中删除
scll.travel()
10 30 1 2 4 5 6
scll.search(30) # 数字30是否存在于链表中
True
scll.search(40) # 数字40是否存在于链表中
False