单向循环链表

单链表的一个变形是单向循环链表,链表中最后一个节点的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. 【UOJ#177】欧拉回路

    [UOJ#177]欧拉回路 题面 UOJ 题解 首先图不连通就没啥好搞的了. 对于无向图而言,每个点度数为偶数. 对于有向图而言,每个点入度等于出度. 然后就是一本通上有的做法,直接\(dfs\)一遍 ...

  2. C 头文件、宏、编译问题

    @2019-02-15 [小记] > C 头文件的防重复包含是针对同一个源文件而言 原因: #include 头文件就是一段代码的拷贝,头文件中若有类型定义等,重复包含就会造成编译错误,若无类型 ...

  3. 540. Single Element in a Sorted Array

    题目大意: 给你一个由小到大排好序的数组,里面只有一个数出现了一次,其他数都出现了两次,要求找出那个只出现一次的数,而且时间复杂度为O(logn) 题目思路: 说实话一开始没想到,因为几乎每个数都出现 ...

  4. HR_Counting Valleys

    把字符串数字化之后应该从 i>0开始判断而不是 i>1 因此错了4个testcases. #!/bin/python3 import math import os import rando ...

  5. HEOI2016解题报告

    树 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记 ...

  6. LinkedBlockingQueue中put源码分析

    查看源码得知: LinkedBlockingQueue采用是锁分离的技术 //取出锁 private final ReentrantLock takeLock = new ReentrantLock( ...

  7. 跟我一起用node-express搭建一个小项目(node连接mongodb)[三]

    数据库虽然安装并启动成功了,但我们需要连接数据库后才能使用数据库. 怎么才能在 Node.js 中使用 MongoDB 呢? 我们使用官方提供的 node-mongodb-native 驱动模块,打开 ...

  8. python的异步IO模块

    asyncio模块:示例一 import asyncio @asyncio.coroutine def func1(): print('before...func1......') yield fro ...

  9. 详解python的垃圾回收机制

    python的垃圾回收机制 一.引子 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存空间给回收掉,而变量名是访问到变量值的唯一方式 ...

  10. 第五节,TensorFlow编程基础案例-session使用(上)

    在第一节中我们已经介绍了一些TensorFlow的编程技巧;第一节,TensorFlow基本用法,但是内容过于偏少,对于TensorFlow的讲解并不多,这一节对之前的内容进行补充,并更加深入了解讲解 ...