python中的单向循环链表实现
引子

所谓单向循环链表,不过是在单向链表的基础上,如响尾蛇般将其首尾相连,也因此有诸多类似之处与务必留心之点。尤其是可能涉及到头尾节点的操作,不可疏忽。
对于诸多操所必须的遍历,这时的条件是什么?又应该在哪里停止?
在做删除操作时,如若待删除节点是头或尾节点时,该如何处理?如果链表只有一个节点,又该如何处理?
代码实现
class Node(object):
def __init__(self, value):
# 元素域
self.value = value
# 链接域
self.next = None class CircularLinkedListOneway(object):
def __init__(self, node=None):
# 构造非空链时,让其地址域指向自己
if node:
node.next = node
self.__head = node def is_empty(self):
# 头节点不为None则不为空
return self.__head == None def __len__(self):
count = 1
cur = self.__head
if self.is_empty():
return 0
while cur.next != self.__head:
count += 1
cur = cur.next
return count def traversal(self):
if self.is_empty():
return
cur = self.__head
while cur.next != self.__head:
print(cur.value)
cur = cur.next
# 退出循环时,cur正是尾节点
print(cur.value) def add(self, value):
"""头插法"""
node = Node(value)
if self.is_empty():
self.__head = node
self.__head.next = self.__head
return
cur = self.__head
while cur.next != self.__head:
cur = cur.next
# 新节点的next指针指向原头节点
node.next = self.__head
# 将新节点指向头节点
self.__head = node
# 尾节点next指针指向新头节点
cur.next = self.__head def append(self, value):
node = Node(value)
cur = self.__head
if self.is_empty():
self.__head = node
self.__head.next = self.__head
return
while cur.next != self.__head:
cur = cur.next
node.next = cur.next
cur.next = node def insert(self, pos, value):
if pos <= 0:
self.add(value)
elif pos > len(self) - 1:
self.append(value)
else:
node = Node(value)
cur = self.__head
count = 0
while count < pos - 1:
count += 1
cur = cur.next
node.next = cur.next
cur.next = node def search(self, value):
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.value == value:
return True
else:
cur = cur.next
# 别忘了while循环外的尾节点
if cur.value == value:
return True
return False def remove(self, value):
cur = self.__head
prior = None
if self.is_empty():
return
while cur.next != self.__head:
# 待删除节点如果找到
if cur.value == value:
# 待删除节点在头部
if cur == self.__head:
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
# 待删除节点在中间
else:
prior.next = cur.next
# 这里不是跳出循环的break,而是退出函数的return哦,因为已经处理完毕了
return
# 如果还没找到
else:
prior = cur
cur = cur.next
# 待删除节点在尾部
if cur.value == value:
# 如果链表中只有一个元素,则此时prior为None,Next属性就会报错
# 此时直接使其头部元素为None即可
if cur == self.__head:
self.__head = None
return
prior.next = cur.next
python中的单向循环链表实现的更多相关文章
- python中的单向链表实现
引子 数据结构指的是是数据的组织的方式.从单个数据到一维结构(线性表),二维结构(树),三维结构(图),都是组织数据的不同方式. 为什么需要链表? 顺序表的构建需要预先知道数据大小来申请连续的存储空间 ...
- python实现单向循环链表
单向循环链表 单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点. 实现 class Node(object): """节 ...
- 数据结构与算法-python描述-单向循环链表
# coding:utf-8 # 单向循环链表的相关操作: # is_empty() 判断链表是否为空 # length() 返回链表的长度 # travel() 遍历 # add(item) 在头部 ...
- Python 单向循环链表
操作 is_empty() 判断链表是否为空 length() 返回链表的长度 travel() 遍历 add(item) 在头部添加一个节点 append(item) 在尾部添加一个节点 inser ...
- python算法与数据结构-循环链表(39)
一.循环链表的介绍 上一篇我们已经讲过单链表,本篇给大家讲解循单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点,其基本操作和单链表思路一样. 常用的操 ...
- python中的数据结构-链表
一.什么是链表 链表是由一系列节点构成,每个节点由一个值域和指针域构成,值域中存储着用户数据,指针域中存储这指向下一个节点的指针.根据结构的不同,链表可以分为单向链表.单向循环链表.双向链表.双向循环 ...
- python中的collections
python中有大量的内置模块,很多是属于特定开发的功能性模块,但collections是属于对基础数据的类型的补充模块,因此,在日常代码中使用频率更高一些,值得做个笔记,本文只做主要关键字介绍,详细 ...
- 基于visual Studio2013解决算法导论之021单向循环链表
题目 单向循环链表的操作 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...
- 单向循环链表C语言实现
我们都知道,单向链表最后指向为NULL,也就是为空,那单向循环链表就是不指向为NULL了,指向头节点,所以下面这个程序运行结果就是,你将会看到遍历链表的时候就是一个死循环,因为它不指向为NULL,也是 ...
随机推荐
- AppCan
启动服务 将app程序寄宿在计算机上,在计算机上调试:访问服务地址,将appToken值复制一下 在浏览器输入192.168.2.102:3000/appToken的值/文件路径后即可调试 入口文件 ...
- 20165337《网络对抗技术》week1 Exp0 Kali安装
1.下载kali kali官网:https://www.kali.org 在官网中下载,并且在VMvare里打开 2.修改视图 进去之后虚拟机界面很小,需要修改视图来调整 3.网络设置 4.文件夹共享 ...
- application.properties 文件和 application.yml 文件的区别
yml文件的好处,天然的树状结构,方便查看,最终会转成application.properties 注意点: 1,原有的key,例如spring.jpa.properties.hibernate.di ...
- The base and high address of the custom IP are not correctly reflected in xparameters.h in SDK
This issue has been observed in 2015.3, 2015.4, and 2015.4.1 builds of Vivado. When you create and a ...
- 项目Alpha冲刺(团队)-第五天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 作业目标:描述第五天冲刺的项目进展.问题困难.心得体会 ...
- mysql插入大数据
/*部门表*/ CREATE TABLE dept( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, /*id*/ deptno MEDIUMINT UNSIG ...
- zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控
zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控 前端nginx虚拟主机引用后端多个php接口,为了方便监控,将后端服务器集中配置在nginx中,具体配置如下: [roo ...
- ASP.NET Core 中使用Session会话
添加Session Nuget包 更新Startup.cs文件 在ConfigureServices方法中添加如下代码 services.AddSession(options => { // S ...
- Hadoop之Flume 记录
出现这个错误是自己的粗心大意,解决: 在配置flume-conf.properties文件时,source和channel的对应关系是: myAgentName.sources.mySourceNam ...
- python django2.x报错No module named 'django.core.urlresolvers'
解决方法就是: from django.urls import reverse 最近从django1.9迁移到django2.0中出现一个意外的报错: 这个报错的原因在stack overflow上有 ...