循环双链表:就是将双链表中最后一个结点的后继指针指向双链表的头结点,并将其头结点的先驱指针指向表中最后一个结点。
python实现
创建循环双链表
class DoubleLinkedNode:
def __init__(self,data):
self.data = data
self.next = None
self.prev = None
class CircularDoubleLinkedList:
def __init__(self):
self.head = DoubleLinkedNode(None)
'''创建循环双链表'''
def CreatCircularDoubleLinkedList(self):
data = input("请输入数据(按回车键确定,按“#”结束):")
cNode = self.head
while data != "#":
nNode = DoubleLinkedNode(int(data))
cNode.next = nNode
nNode.prev = cNode
nNode.next = self.head
self.head.prev = nNode
cNode = cNode.next
data = input("请输入数据(按回车键确定,按“#”结束):")
CDLList = CircularDoubleLinkedList()
CDLList.CreatCircularDoubleLinkedList()
结果如下:
获取循环双链表长度
class DoubleLinkedNode:
def __init__(self,data):
self.data = data
self.next = None
self.prev = None
class CircularDoubleLinkedList:
def __init__(self):
self.head = DoubleLinkedNode(None)
'''创建循环双链表'''
def CreatCircularDoubleLinkedList(self):
data = input("请输入数据(按回车键确定,按“#”结束):")
cNode = self.head
while data != "#":
nNode = DoubleLinkedNode(int(data))
cNode.next = nNode
nNode.prev = cNode
nNode.next = self.head
self.head.prev = nNode
cNode = cNode.next
data = input("请输入数据(按回车键确定,按“#”结束):")
'''获取循环双链表长度'''
def GetLength(self):
cNode = self.head
i = 0
while cNode.next != self.head:
i += 1
cNode = cNode.next
print(i)
CDLList = CircularDoubleLinkedList()
CDLList.CreatCircularDoubleLinkedList()
CDLList.GetLength()
结果如下:
判断循环双链表是否为空
class DoubleLinkedNode:
def __init__(self,data):
self.data = data
self.next = None
self.prev = None
class CircularDoubleLinkedList:
def __init__(self):
self.head = DoubleLinkedNode(None)
'''创建循环双链表'''
def CreatCircularDoubleLinkedList(self):
data = input("请输入数据(按回车键确定,按“#”结束):")
cNode = self.head
while data != "#":
nNode = DoubleLinkedNode(int(data))
cNode.next = nNode
nNode.prev = cNode
nNode.next = self.head
self.head.prev = nNode
cNode = cNode.next
data = input("请输入数据(按回车键确定,按“#”结束):")
'''获取循环双链表长度'''
def GetLength(self):
cNode = self.head
i = 0
while cNode.next != self.head and cN