单向循环链表的增、删、查、改
具体代码如下:
class Node(object):
"""链表单节点实现"""
def __init__(self, item):
self.item = item # 元素域
self.next = None # 链接域
class CycleleLink:
"""单向循环链表"""
def __init__(self, node=None):
self.__head = node
# head为指向一个链表头结点的p变量,接收一个参数node,node就是一个结点(第一个结点),当然也可以不传参数,
# 当做一个空链表
# head设置成私有属性,防止外部访问,如果head被外部访问并改动,则head之前指向的链表就丢失找不到了
def is_empty(self):
"""判断链表是否为空
:return 如果链表为空, 返回真
"""
return self.__head is None
def length(self):
"""
:return: 链表长度
cur 游标
count 结点计数
"""
if self.is_empty():
return 0
else:
cur = self.__head # 设置游标指向第一个结点
count = 1 # 计数初始值为:1
while cur.next != self.__head: # 如果游标指向的结点的next为head则表示该结点为链表的最后一个元素
count += 1 # 每次加一
cur = cur.next # 如果cur不为空则让游标指向下一个结点
return count # 最后返回链表长度
def travel(self):
"""
遍历链表
:return:打印链表所有元素
"""
if self.is_empty():
return # 如果链表是空的就直接返回
cur = self.__head
while cur.next != self.__head:
print(cur.item, end=" ")
cur = cur.next
print(cur.item)
# 最后一个元素的next为head,不进入循环也就不会打印,所以在此处补充打印
def add(self, item):
"""
在链表头部添加结点
:param item: 结点元素
:return:
"""
node = Node(item) # 创建一个结点对象
# 链表为空时特殊处理
if self.is_empty():
self.__head = node
node.next = node # 即使只有一个结点时也要遵循循环链表规则,尾结点指头
# 该链表的尾部的next指向head所以在头部
# 所以要改变head要先找到最后一个元素
cur = self.__head
while cur.next != self.__head:
cur = cur.next
# 此while循环结束后cur就指向了最后一个结点了
node.next = self.__head # 让新结点的next指向原始的第一个结点
self.__head = node # 让头指针指向新加入的结点
cur.next = node # 让最后一个结点的next指向新的头结点
def append(self, item):
"""
在链表的尾部追加结点
:param item:
:return:
"""
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
# while循环结束后cur指向最后一个结点
cur.next = node # 尾部追加结点
node.next = self.__head # 新尾结点指头
def insert(self, pos, item):
"""
在指定位置插入元素
:param item:
:return:
"""
# TODO 此方法代码涉及头尾的都已封装好了,其余代码也不需要更改(与单链表相同)
if pos <= 0: # 如果要插入的位置小于等于0,则相当于在头部加入结点
self.add(item) # 直接调用已经写好的方法
elif pos >= self.length(): # 如果要插入的位置大于等于链表的长度,则相当于在链表的尾部追加元素
self.append(item) # 直接调用写好的方法
else: # 如果插入位置在链表中间则进行一下操作
cur = self.__head # 游标指向第一个结点
count = 0 # 计数制0
while count < (pos - 1): # (pos - 1) cur需要停留在pos位置的前一个结点的位置
cur = cur.next
count += 1
# 当退出循环的时候cur指向pos的前一个位置
node = Node(item) # 创建新结点
node.next = cur.next # 将新结点指向cur的下一个结点
cur.next = node # 将cur指向新结点
def remove(self, item):
"""删除结点"""
# TODO 此方法重点特殊处理
if self.is_empty(): # 如果链表为空直接返回
return
cur = self.__head
pre = None # 设置前后两个游标,cur,pre
while cur.next != self.__head: # 如果cur不为空则进入循环
if cur.item == item: # 找到了要删除的元素
if cur == self.__head: # 如果找到的元素在头结点上
rea = self.__head
while rea.next != self.__head:
rea = rea.next
# 此while循环结束时rea指向尾结点
self.__head = cur.next # 让头结点指向下一个结点,丢弃上个结点
rea.next = self.__head
else: # 找到的结点不在头结点上,
# 让在目标结点的前一个结点的next指向目标结点的后一个结点,使目标结点的引用变成0,被丢弃
pre.next = cur.next
return # 删除后直接结束循环
else:
pre = cur # 让前一个游标向后移指向cur,让cur向后移指向cur的下一个结点
cur = cur.next
# 退出外层while时,cur指向尾结点,没有进入循环,也没有判断,在此处处理一下
if cur.item == item:
if cur.next == self.__head: # cur指向尾结点,而cur的next指向head则表明链表只有一个结点
self.__head = None
else:
pre.next = self.__head
def search(self, item):
"""
查找结点是否存在
:return 布尔型
"""
if self.is_empty():
return False
cur = self.__head
while cur != self.__head:
if cur.item == item:
return True # 如果找到了目标结点就返回true
cur = cur.next
# while退出时cur指向尾结点,没有进入循环,也没有判断,在此处处理一下
if cur.item == item:
return True
return False # 如果整个循环结束也没有找到目标结点,就返回false
if __name__ == '__main__':
link = CycleleLink()
print(link.length())
print(link.is_empty())
link.add(1)
link.travel()
link.append(6)
link.travel()
link.insert(1, 3)
link.travel()
print(link.length())
print(link.search(1))
link.remove(3)
link.travel()
link.remove(6)
link.remove(1)
print(link.length())
总结: 单向循环链表操作重点注意:
1 判空
2 首尾结点特殊情况处理
3 链表只有一个结点时的next指向自己