class Node(object):
def __init__(self,elem):
self.elem = elem
self.next = None
class SingleCycleList(object):
def __init__(self, node = None):
#node = None为默认参数
self.__head = node
if node:
node.next = node
def is_empty(self):
return self.__head == None
def length(self):
cur = self.__head
if self.is_empty():
return 0
count = 0
while cur.next != self.__head:
count += 1
cur = cur.next
return count+1
def travel(self):
if self.is_empty():
return
cur = self.__head
while cur.next != self.__head:
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem)
def add(self,item):
"""头部添加数据"""
node = Node(item)
if self.is_empty():
node.next = self
Python写一个单向循环列表
最新推荐文章于 2024-08-16 00:28:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)