定义了单向循环链表的结点类,单向循环链表类(尾结点指向头结点)及其一些方法,以及有一些测试
class node(object):
def __init__(self,value):
self.item=value
self.next=None
#链表的处理完整性考虑顺序:
# 一般结点(中间点),空链表,头结点,单个节点头结点,尾结点,单个尾结点。
# [单个节点既可以被看成头结点也可以看成尾结点]
# (明显例子:单向循环链表的remove)
class singlelooplinklist(object):
'''单向循环链表,尾结点指向头结点'''
def __init__(self,node=None):
self.__head=node
if node:#若是空链表,则无需指向头结点
node.next=node
def isempty(self):
'''判断链表是否为空'''
return self.__head==None#用is None会好一点,编译器不会下划线警告
def len(self):
'''返回链表长度'''
'''指针遍历计数'''
if self.__head==None:#或者用self.isempty来判断是否为空链表
return 0
count=0#第二种为1
cur=self.__head
while cur.next!=self.__head:#该判断条件特点,直到循环前的最后一个结点停止,并指向最后一个节点
count+=1 #注意有单一尾结点的情况
cur=cur.next
return count+1#第二种为count
def travel(self):
if self.isempty():
return
cur=self.__head
while cur.next!=self.__head:
print(cur.item,end=" ")
cur=cur.next
#cur指向尾结点停止结束循环,但尾结点并未处理
print(cur.item,end=" ")
print('')
def append(self,value):
'''尾部添加结点'''
newnode=node(value)
cur=self.__head
if self.isempty(): # 考虑空链表
self.__head = newnode
newnode.next = newnode
else:
while cur.next!=self.__head:
cur=cur.next
newnode.next=self.__head
cur.next=newnode
def add(self,value):
'''头部添加结点'''
newnode=node(value)
if self.isempty():#考虑空链表
self.__head=newnode
newnode.next=newnode
else:
'''先把尾结点找到,在再把新的结点放到头部'''
cur=self.__head
while cur.next!=self.__head:
cur=cur.next#找到尾结点
#将新节点放到头部
newnode.next=self.__head
self.__head=newnode
cur.next=newnode#改变尾结点循环的位置
def insert(self,index,value):
'''指定位置插入节点'''
newnode=node(value)
n=self.len()
if index<=0:
self.add(value)
return
if index>n-1:
self.append(value)
return
#中间插入不涉及尾部和循环操作,故和单链表一样
cur=self.__head
count=0
while count<index-1:
count+=1
cur=cur.next
newnode.next=cur.next
cur.next = newnode
def remove(self,value):
'''删除左边第一个与指定值相同的结点'''
if self.isempty():
return
cur=self.__head
pre=None
while cur.next!=self.__head:#遍历直到最后一个结点(最后一个节点不处理)
if cur.item==value:
if cur!=self.__head:#中间点
pre.next = cur.next
else:#头结点(包括了单一头结点)
'''先把尾结点找出,再把源头结点去除'''
rear=self.__head
while rear.next!=self.__head:
rear=rear.next
self.__head=cur.next
rear.next=self.__head
return#关键,删除之后整个函数退出,而非break,
# 不然会进行下面代码,执行pre.next,但next为空
else:
pre=cur
cur=cur.next
if cur.item==value:#处理尾结点
if cur==self.__head:#单个节点的尾结点
self.__head=None
else:#多个节点的尾结点
pre.next = cur.next
def search(self,value):
'''查找某结点是否在链表中,返回TRUE/False'''
if self.isempty():
return False
cur = self.__head
while cur.next!= self.__head:
if cur.item==value:
return True
cur = cur.next
# cur指向尾结点停止结束循环,但尾结点并未处理
if cur.item == value:
return True
return False
def found(self,value):
'''找到指定元素的下标(实际链表无下标)'''
cur = self.__head
count=0
if self.isempty():
return
while cur.next!=self.__head:
if cur.item == value:
return count
cur = cur.next
count += 1
if cur.item == value:
return count
if __name__=='__main__':
ll = singlelooplinklist()
print(ll.isempty())
print(ll.len())
ll.append(1)
print(ll.isempty())
print(ll.len())
ll.append(2)
ll.add(8)
ll.append(3)
ll.append(4)
ll.append(5)
ll.append(6) # 8 1 2 3 4 5 6
ll.travel()
ll.insert(-1, 9) # 9 8 1 2 3 4 5 6
ll.travel()
ll.insert(3, 100) # 9 8 1 100 2 3 4 5 6
ll.travel()
ll.insert(10, 200) # 9 8 1 100 2 3 4 5 6 200
ll.travel()
ll.remove(100) # 9 8 1 2 3 4 5 6 200
ll.travel()
ll.remove(9) # 8 1 100 2 3 4 5 6 200
ll.travel()
ll.remove(200) # 8 1 2 3 4 5 6
ll.travel()
print(ll.search(55)) # False
print(ll.search(5)) # True
print(ll.found(5)) # 5
测试结果