单向循环链表

单链表的一个变形是单向循环链表,链表中最后一个节点的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. 【CodeForces 717C】Potions Homework

    BUPT 2017 summer training (for 16) #1G 题意 每个人有一个懒惰值,每个任务有个难度,一开始每个人的任务和懒惰值都为\(a_i\),完成任务时间是懒惰值乘以难度,现 ...

  2. 【WC2018】州区划分(FWT,动态规划)

    [WC2018]州区划分(FWT,动态规划) 题面 UOJ 洛谷 题解 首先有一个暴力做法(就有\(50\)分了) 先\(O(2^nn^2)\)预处理出每个子集是否合法,然后设\(f[S]\)表示当前 ...

  3. 【Hihocoder1413】Rikka with String(后缀自动机)

    [Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...

  4. 新建WINDOWS服务C#

    当前作业环境 Windows8.1 | Visual Studio 2013 一. 建立项目,选择"Windows服务"模板 二. 查看生成的项目,结构很像WinForm的项目,其 ...

  5. HDU6321 Dynamic Graph Matching (杭电多校3C)

    给出一些点集,然后对于每一次要求给出的这些点集里的1,2,3,4,5,6....n/2的匹配数, dp[i][j] 表示到第i次操作里点集为j的匹配数,然后我每次加入一条边u-v,我的状态就是 dp[ ...

  6. Thinkphp5 captcha扩展包安装,验证码验证以及点击刷新

    首先下载 captcha扩展包,↓ 下载附件,解压到vendor目录下: 然后进入application/config.php添加配置信息: //验证码       'captcha'  =>  ...

  7. JetBrains全家桶破解思路(最新更新:2019-04-17)

    JetBrains全家桶破解思路(以DataGrip为例) 2019-04-17 add new key 2019-01-24 add new key and code 2018-12-24 add ...

  8. 群福利:Redis云服务器免费领取(附Redis安装和连接远程连接Redis案例)

    Redis安装:在线体验:https://try.redis.io Ubuntu:sudo apt-get install redis CentOS:yum install redis (root权限 ...

  9. angular2的lazyload初体验

    angular2自带了lazyload,就是路由的loadChild,要优化ng2项目必不可少.代码已更新到ng-demo ->https://github.com/chenby/ng2-dem ...

  10. Redis命令:scan实现模糊查询

    转: Redis命令:scan实现模糊查询 2017年12月31日 16:54:33 琦彦 阅读数:22893 标签: redis数据库Redis命令scan模糊查询 更多 个人分类: Redis 所 ...