循环链表
一、概要:
循环链表是相对于单链表而言的一种特殊情况,就是将最后一个节点的next域赋值给头结点(图一为单链表,图二为循环链表)
级循环链表与单链表的比较
循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活。①循环链表中没有None域。涉及遍历操作时,其终止条件就不再是像非循环链表那样判别p或p->next是否为None,而是判别它们是否等于某一指定指针,如头指针或尾指针等。 ②在单链表中,从一已知结点出发,只能访问到该结点及其后续结点,无法找到该结点之前的其它结点。而在单循环链表中,从任一结点出发都可访问到表中所有结点,这一优点使某些运算在单循环链表上易于实现。
二、实现代码:
(1)建立节点:
class Node():
"""
建立节点
"""
def __init__(self,date):
self.date=date
self.next=Node
(2)建立带有头节点的循环链表:
判断循环链表是否为空
def is_empty(self):
"""判断链表是否为空"""
return self.__head==None
求循环链表的长度
def length(self):
"""
求链表的长度
"""
if self.is_empty():
return 0
else:
count=1
cur=self.__head
while cur.next!=self.__head:
count+=1
cur=cur.next
return count
求循环链表的长度
def travel(self):
cur=self.__head
if self.is_empty():
print("该链表为空,无法进行遍历")
else:
while cur!=self.__head:
print(str(cur.date),end=" ")
cur=cur.next
print(str(cur.date))#用于遍历最后一个节点
这行代码要注意最后一行,不能少,否则不能够输出最后一个节点,因为在while循环中,扫描到最后一个节点时cur==self.__head 不能够进入while循环中,所以需要单独输出
从头插入节点:
def HeadInsert(self,num):
"""
头部插入节点
"""
node=Node(num)
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
从尾部插入节点:
def TailInsert(self,num):
"""
尾部插入节点
"""
node=Node(num)
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
cur.next=node
索引插入:
def NodeInsert(self,index,num):
"""在指定位置添加元素"""
#指向不是头部元素,self.__head的地址
# 为下一个元素,所以pre为下一个元素
if index <= 0:
#认为是头插法
self.HeadInsert(num)
elif index > (self.length()-1):
self.TailInsert(num)
else:
pre_cur = self.__head
count = 0
while count < (index-1):
count+=1
pre_cur = pre_cur.next
node = Node(num)
node.next = pre_cur.next
pre_cur.next = node
完整代码:
import random
class Node():
"""
建立节点
"""
def __init__(self,date):
self.date=date
self.next=None
class LinkNode():
def __init__(self,node=None):
self.__head=node
if node:
node.next=self.__head #建立一个循环头节点
def is_empty(self):
"""判断链表是否为空"""
return self.__head==None
def length(self):
"""
求链表的长度
"""
if self.is_empty():
return 0
count=1
cur=self.__head
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.date,end=" ")
cur = cur.next
print(cur.date,end="\n")
def HeadInsert(self,num):
"""
头部插入节点
"""
node=Node(num)
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
def TailInsert(self,num):
"""
尾部插入节点
"""
node=Node(num)
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 NodeInsert(self,index,num):
"""在指定位置添加元素"""
#指向不是头部元素,self.__head的地址
# 为下一个元素,所以pre为下一个元素
if index <= 0:
#认为是头插法
self.HeadInsert(num)
elif index > (self.length()-1):
self.TailInsert(num)
else:
pre_cur = self.__head
count = 0
while count < (index-1):
count+=1
pre_cur = pre_cur.next
node = Node(num)
node.next = pre_cur.next
pre_cur.next = node
a=LinkNode()
for i in range(6):
a.TailInsert(random.randint(0,10))#随机从插入6个数字
a.travel()
a.NodeInsert(5,100)
a.travel()