单向循环链表

单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点。

实现

class Node(object):
"""节点"""
def __init__(self, item):
self.item = item
self.next = None

节点实现

class SinCycLinkList(object):
"""单向循环链表"""
def __init__(self):
self.head = None def is_empty(self):
"""判断链表是否为空"""
return self.head is 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
print(cur.item)
while cur.next != self.head:
cur = cur.next
print(cur.item)
print("结束了") def add(self, item):
"""头部添加节点"""
node = Node(item)
# 如果链表为空
if self.is_empty():
# 把插入节点置为首节点
self.head = node
# node的下一个节点置为自身
node.next = self.head
else:
# node指向以前的首节点
node.next = self.head
# 遍历,找到尾节点
cur = self.head
while cur.next != self.head:
cur = cur.next
# 尾节点指向插入的节点
cur.next = node
# 把插入的节点置为当前的头节点
self.head = node def append(self, item):
"""尾部添加节点"""
node = Node(item)
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 insert(self, pos, item):
"""在指定的位置添加节点"""
# 如果插入的索引小于0
if pos <= 0:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
node = Node(item)
num = 0
cur = self.head
# 找到插入位置的前一个节点
while num < pos-1:
num += 1
cur = cur.next
# 注意这里的更改顺序!!!
node.next = cur.next
cur.next = node def remove(self, item):
"""删除节点"""
# 链表为空,则直接返回
if self.is_empty():
return
cur = self.head
pre = None
# 如果头节点的元素就是要查找的元素item
if cur.item == item:
# 如果链表不止一个节点
if cur.next != self.head:
while cur.next != self.head:
cur = cur.next
cur.next = self.head.next
self.head = self.head.next
else:
# 链表只有一个节点
self.head = None
else:
pre = self.head
# 第一个节点不是要删除的
while cur.next != self.head:
# 找到要删除的节点
if cur.item == item:
# 删除
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 循环结束之后cur为尾节点
if cur.item == item:
pre.next = cur.next def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
cur = self.head
if cur.item == item:
return True
while cur.next != self.head:
# 放在判断的前面
cur = cur.next
if cur.item == item:
return True
# 遍历一圈后都找不到,返回False
return False

链表实现

if __name__ == '__main__':
lst1 = SinCycLinkList() # self.head=None
lst1.add(1)
lst1.add(2)
lst1.append(3)
lst1.append(4)
lst1.travel()
lst1.insert(1, 5)
lst1.travel()
lst1.insert(-2, 6)
lst1.travel()
lst1.insert(10, 7)
lst1.travel()
lst1.remove(1)
lst1.travel()
lst1.remove(10)
lst1.travel()
print('head:', lst1.head.item)
print('length:', lst1.length())

测试

python实现单向循环链表的更多相关文章

  1. 数据结构与算法-python描述-单向循环链表

    # coding:utf-8 # 单向循环链表的相关操作: # is_empty() 判断链表是否为空 # length() 返回链表的长度 # travel() 遍历 # add(item) 在头部 ...

  2. Python 单向循环链表

    操作 is_empty() 判断链表是否为空 length() 返回链表的长度 travel() 遍历 add(item) 在头部添加一个节点 append(item) 在尾部添加一个节点 inser ...

  3. python中的单向循环链表实现

    引子 所谓单向循环链表,不过是在单向链表的基础上,如响尾蛇般将其首尾相连,也因此有诸多类似之处与务必留心之点.尤其是可能涉及到头尾节点的操作,不可疏忽. 对于诸多操所必须的遍历,这时的条件是什么?又应 ...

  4. 基于visual Studio2013解决算法导论之021单向循环链表

     题目 单向循环链表的操作 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...

  5. python实现 双向循环链表

    最近身边的朋友在研究用python来实现数据结构.遇到一个问题就是双向循环链表的实现,改指向的时候总是发蒙. 我自己尝实现了一个python的双向循环链表.附上代码,希望对大家有帮助. 如果不懂什么是 ...

  6. 单向循环链表C语言实现

    我们都知道,单向链表最后指向为NULL,也就是为空,那单向循环链表就是不指向为NULL了,指向头节点,所以下面这个程序运行结果就是,你将会看到遍历链表的时候就是一个死循环,因为它不指向为NULL,也是 ...

  7. c/c++ 线性表之单向循环链表

    c/c++ 线性表之单向循环链表 线性表之单向循环链表 不是存放在连续的内存空间,链表中的每个节点的next都指向下一个节点,最后一个节点的下一个节点不是NULL,而是头节点.因为头尾相连,所以叫单向 ...

  8. 复习下C 链表操作(单向循环链表、查找循环节点)

    循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表.  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...

  9. (java实现)单向循环链表

    什么是单向循环链表 单向循环链表基本与单向链表相同,唯一的区别就是单向循环链表的尾节点指向的不是null,而是头节点(注意:不是头指针). 因此,单向循环链表的任何节点的下一部分都不存在NULL值. ...

随机推荐

  1. 写个shell脚本依次运行每个程序半小时

    @echo off :: 运行时间1800000毫秒 echo wscript.sleep 1800000 >run.vbs ::运行Debug/lab1.exe程序,后面是参数 start D ...

  2. Can DBC文件翻译

    1 引言 DBC文件描述单个CAN网络的通信.这个信息足以监测和分析网络并模拟不是物理可用的节点(剩余的总线模拟). DBC文件也可以用来开发电子控制单元的通信软件,该控制单元应该是CAN网络的一部分 ...

  3. kafka为什么这么优秀!

    kafka为什么这么优秀! 阿飞的博客 匠心零度 今天 1.动机2.持久化3.效率4.生产者4.1负载均衡4.2异步发送5.消费者Push vs. Pull消费者位置离线数据加载 1.动机 kafka ...

  4. Django(十五)Form组件

    参考博客: https://www.cnblogs.com/haiyan123/p/7778888.html http://www.cnblogs.com/wupeiqi/articles/61441 ...

  5. struts2 防止表单的重复提交

    防止表单重复提交(拦截器) 1.回顾之前的解决办法: 2.Struts2中的解决办法: 2.1.使用重定向 <result type="redirect">/succe ...

  6. Mock1 moco框架的基本介绍

    前言: Mock就是模拟接口的,一般在开发人员还没有开发完接口,但是有接口文档,这个时候就可以执行接口测试,前端同学也可以用mock功能给自己使用. 功能:可以模拟http协议发送请求 下载链接:ht ...

  7. java 集合的总结

    集合大致可以分为两类: 一类继承Collection接口,存储的是多个孤立的元素,包括List和set: List包括ArrayList类和LinkedList类,ArrayList数组的顺序存储,而 ...

  8. python基础四-文件读取

    文件读取 open()接受一个参数:要打开的文件名, 并返回一个表示文件的对象, 存储到后面的变量中 python会在当前执行文件所在目录查找 可以使用绝对路径, 在linux中使用'/', 在win ...

  9. HTML学习笔记Day4

    一.浮动属性 1.首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流: 无论多么复杂的布局,其基本出发点均是:“如何在一行显示多个div元素”: 显然标准流已经无法满足需求 ...

  10. (set)产生冠军 hdu2094

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...