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,也是 ...
随机推荐
- 论文翻译:BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or −1
目录 摘要 引言 1.BinaryNet 符号函数 梯度计算和累积 通过离散化传播梯度 一些有用的成分 算法1 使用BinaryNet训练DNN 算法2 批量标准化转换(Ioffe和Szegedy,2 ...
- python中的operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号.看下面的例子 a = [,,] >>> b=) ...
- FF D8 FF FE 00 24 47 00转图片
String[] img = "FF D8 FF FE 00 24 47 00 9D 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 0 ...
- multiwan 系统配置补充
/etc/sysctl.conf: # Controls source route verification net.ipv4.conf.default.rp_filter = # Allows to ...
- mysql 从一个表查询数据插入另一个表或当前表
mysql insert into 表明(uid,lng,lat) SELECT uuid,lng,lat FROM 表明
- vue 图片地址错误处理
<img src="/logo.png" :onerror="defaultImg"> data() { return { defaultImg: ...
- 烧写uboot和openwrt固件ARxx系列
以AR9331为例. 1.用烧录器将uboot烧写到flash中 (AR9331_U-Boot_Oolite-v1-v20170713.bin) 2.登录:192.168.1.1网页烧写uboot ...
- md5两次加密
private static final String salt="hzjfstkfdff"; public static String MD5(String src) { ret ...
- wx:for获取 data-xxx 自定义的属性
今天在写wx:for循环时,在事件对象上e.target.dataset上一直拿不到自定义属性 data-id. 示例: <view wx:for='{{list}}' wx:key='{{it ...
- WebView与JS互调
在Android 4.2之后JS的注入需要加入注解 @javascriptInterface 1.Android 调用 JS 初始化WebView控件,开启该控件对JS的支持 调用loadUrl()方 ...