单向循环链表
# singlelcycinklist
class Node(object):
"""节点"""
def __init__(self, item):
# 记录数据内容
self.item = item
# 记录下一个节点
self.next = None
class SingleCycLinkList(object):
"""单向循环链表"""
def __init__(self):
# 记录头节点,所有的操作从头节点开始
self.__head = None
def is_empty(self):
"""链表是否为空"""
# 判断头节点是否为空
return self.__head is None
def length(self):
"""链表长度"""
if self.is_empty():
return 0
# 定义count 累计加1
count = 1
# 从头到尾遍历
cur = self.__head
while cur.next is not self.__head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
if self.is_empty():
return
# 定义游标,从头往后移动
cur = self.__head
print(cur.item, end=" ")
while cur.next is not self.__head:
cur = cur.next
print(cur.item, end=" ")
print()
def add(self, item):
"""链表头部添加元素"""
# 创建新节点
node = Node(item)
# 判断链表是否为空
if self.is_empty():
# 链表只有一个节点,自己的next指向自己
self.__head = node
node.next = self.__head
return
# 遍历找到尾节点,while循环结束,cur指向的是尾节点
cur = self.__head
while cur.next is not self.__head:
cur = cur.next
# 让新节点的next指向原来的头
node.next = self.__head
# 让head记录新节点
self.__head = node
# 尾节点指向新的头
cur.next = node
def append(self, item):
"""链表尾部添加元素"""
# 判断头节点是否为空
if self.is_empty():
self.add(item)
return
# 从头遍历,找到尾节点
cur = self.__head
while cur.next is not self.__head:
cur = cur.next
# 当while循环结束,cur指向的是尾节点
node = Node(item)
cur.next = node
# 让新节点的next指向头节点
node.next = self.__head
def insert(self, pos, item):
"""指定位置添加元素"""
# 健壮性
if pos <= 0:
self.add(item)
elif pos >= self.length():
self.append(item)
else:
# 从头遍历,找到pos前面的节点
cur = self.__head
# 定义下标,index记录的是当前cur指向的对象的下标
index = 0
while index < (pos - 1):
index += 1
cur = cur.next
# while循环结束,cur执行的是pos前面的节点
node = Node(item)
node.next = cur.next
cur.next = node
def remove(self, item):
"""删除节点"""
# 从头遍历,找到要删除的节点
cur = self.__head
# 定义pre,记录cur的前置节点
pre = None
while cur.next is not self.__head:
if cur.item == item:
# 如果pre为空,证明删除的是首节点
if pre is None:
# 找到尾节点
end_node = self.__head
while end_node.next is not self.__head:
end_node = end_node.next
# 让head指向cur下一个节点
self.__head = cur.next
# 让尾节点指向新的头节点
end_node.next = self.__head
else:
pre.next = cur.next
return
pre = cur
cur = cur.next
# while循环不能处理尾节点,循环后单独处理尾节点
if cur.item == item:
# 如果pre为空,证明当前只有一个节点,而且删除的是这个节点
if pre is None:
self.__head = None
else:
# 让前置节点指向头节点
pre.next = self.__head
def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
# 可能是首节点
cur = self.__head
if cur.item == item:
return True
# 否则遍历
while cur.next is not self.__head:
cur = cur.next
if cur.item == item:
return True
# while循环结束,没找到
return False
if __name__ == '__main__':
sll = SingleCycLinkList()
print(sll.is_empty())
print(sll.length())
sll.add(3)
sll.add(2)
sll.add(1)
print(sll.is_empty())
print(sll.length())
sll.travel()
sll.add(4)
sll.travel()
print(sll.length())
sll.append(5)
sll.travel()
sll.insert(2, 6)
sll.travel()
sll.insert(4, 7)
sll.travel()
sll.insert(-100, 8)
sll.insert(100, 9)
sll.travel()
sll.remove(6)
sll.remove(8)
sll.remove(9)
sll.travel()
print(sll.search(4))
print(sll.search(2))
print(sll.search(5))
print(sll.search(6))