这个判断比较多了。

一次审准,注释作好,

以后就可以照搬这些功能代码了。

# coding = utf-8

# 单向循环链表
class Node:

    def __init__(self, new_data):
        # 链表有效负载--数据
        self.data = new_data
        # 链表指针
        self.next = None

    def get_data(self):
        return self.data

    def set_data(self, new_data):
        self.data = new_data

    def get_next(self):
        return self.next

    def set_next(self, new_next):
        self.next = new_next

class SingleCycleList:

    def __init__(self):
        self.head = None

    # 作头插入时,需要先判断是否为空列表, 需要要注意插入顺序,
    def add(self, item):
        # 因为实现单链表时,可以统一方式作头插入,所以需要三行代码
        # 而作双向链表和循环链表时,要区别是否为空链表,所以插入的代码就变化了很多。(简单一行,复杂多行)
        node = Node(item)
        if self.is_empty():
            # 循环列表,必定首尾相连
            self.head = node
            node.set_next(self.head)
        else:
            # 添加的节点指向head
            node.set_next(self.head)
            # 移到链表尾部,将尾部节点的next指向node
            current = self.head
            while current.get_next() != self.head:
                current = current.get_next()
            current.set_next(node)
            # head指向添加node的
            self.head = node

    # 作尾插入时,需要先判断是否为空列表
    def append(self, item):
        node = Node(item)
        if self.is_empty():
            # 循环列表,必定首尾相连
            self.head = node
            node.set_next(self.head)
        else:
            # 移到链表尾部,此处不优美,尾插入,要使用prev,从head往前移动一下!!!!!
            current = self.head
            while current.get_next() != self.head:
                current = current.get_next()
            # 将尾节点指向node
            current.set_next(node)
            # 将node指向头节点_head
            node.set_next(self.head)

    # 指定位置插入节点
    def insert(self, pos, item):
        # 相当于头插入
        if pos <= 0:
            self.add(item)
        # 相当于尾插入
        elif pos >= self.size():
            self.append(item)
        else:
            node = Node(item)
            count = 0
            current = self.head
            # 移动到指定位置的前一个位置
            while count < pos - 1:
                count += 1
                current = current.get_next()
            # 由于不是头尾,直接插入即可
            node.set_next(current.get_next())
            current.set_next(node)

    # 删除指定节点数据
    def remove(self, item):
        if self.is_empty():
            return
        previous = None
        current = self.head
        while current.get_next() != self.head:
            # 待删除节点如果找到
            if current.get_data() == item:
                # 在找到节点之后,需要判断是否为首节点
                # 因为首节点时,还没有Previous这个变量
                if current == self.head:
                    rear = self.head
                    while rear.get_next() != self.head:
                        rear = rear.get_next()
                    self.head = current.get_next()
                    rear.set_next(self.head)
                # 待删除节点在中间
                else:
                    previous.set_next(current.get_next())
                return
            # 待删除节点如果还没有找到
            else:
                previous = current
                current = current.get_next()
        # 待删除节点在尾部
        if current.get_data() == item:
            # 如果链表中只有一个元素,则此时prior为None,Next属性就会报错
            # 此时直接使其头部元素为None即可
            if current == self.head:
                self.head = None
                return
            previous.set_next(current.get_next())

    # 查找指定数据是否存在
    def search(self, item):
        current = self.head
        found = False
        while current.get_next() != self.head:
            if current.get_data() == item:
                found = True
            current = current.get_next()
        return found

    def is_empty(self):
        return self.head is None

    def __len__(self):
        return self.size()

    def size(self):
        if self.is_empty():
            return 0
        count = 0
        current = self.head
        # 由于是循环单链表,需要一个中断循环的机制
        while current.get_next() != self.head:
            count += 1
            current = current.get_next()
        return count

    def show(self):
        # 因为是循环链表,遍历的方式和非循环的不一样的。
        if self.is_empty():
            return
        current = self.head
        print(current.get_data(), end=' ')
        while current.get_next() != self.head:
            current = current.get_next()
            print(current.get_data(), end=' ')
        print()

if __name__ == '__main__':
    s_list = SingleCycleList()
    print(s_list.is_empty())
    s_list.add(5)
    s_list.add(4)
    s_list.add(76)
    s_list.add(23)
    s_list.show()
    s_list.append(47)
    s_list.show()
    s_list.insert(0, 100)
    s_list.show()
    s_list.insert(99, 345)
    s_list.show()
    s_list.insert(3, 222)
    s_list.show()
    s_list.remove(76)
    s_list.show()
    print(s_list.search(23))
    s_list.show()
    print(s_list.is_empty())
    print(s_list.size())
    print(len(s_list))
True

True

False

Process finished with exit code 

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

  1. Python 单向循环链表

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

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

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

  3. python实现单向循环链表

    单向循环链表 单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点. 实现 class Node(object): """节 ...

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

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

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

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

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

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

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

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

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

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

  9. Python 单向队列Queue模块详解

    Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...

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

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

随机推荐

  1. jQuery对页面的操作

    一.对元素内容和值进行操作 1.对元素内容操作 [text()]:获取值. [text(val)]:获取并修改值. [html()]:获取值. [html(val)]:获取并修改值,与text的区别在 ...

  2. 前端js区域上下拖拽

    先说说需求吧,网页内又上下两个区域,需要做到的功能是,第一个区域A底部的边可以进行拖拽使得区域变大或变小,同时第二个区域B跟着拖动的变化进行自适应. 思路: 1.使用一个假的div定义为那条可进行拖拽 ...

  3. Virtual Box虚拟机安装Ubuntu16.04以及整理的一些基本操作

    事先声明,参考自:https://www.cnblogs.com/wyt007/p/9856290.html 撰写此文,纯属是为了便利以后换电脑重装. 转载请注明地址:https://www.cnbl ...

  4. Codeforces Round #382 (div2)

    A:题目:http://codeforces.com/contest/735/problem/A 题意:出发点G,终点T,每次只能走k步,#不能走,问能否到达终点 思路:暴力 #include < ...

  5. 「LibreOJ NOI Round #1」验题

    麻烦的动态DP写了2天 简化题意:给树,求比给定独立集字典序大k的独立集是哪一个 主要思路: k排名都是类似二分的按位确定过程. 字典序比较本质是LCP下一位,故枚举LCP,看多出来了多少个独立集,然 ...

  6. CF914G Sum the Fibonacci

    解:发现我们对a和b做一个集合卷积,对d和e做一个^FWT,然后把这三个全部对位乘上斐波那契数,然后做&FWT就行了. #include <bits/stdc++.h> , MO ...

  7. Unity 动画系统

    Legacy动画系统:Animation组件(旧) Mecanim动画系统:Animator组件(新) 动画播放过程: //动画片段 [System.Serializable] public clas ...

  8. log4net在C#项目里的配置

    做个记录,这个可用.每次新项目配置从网上找来的都要配半天 这里不说这是什么,从哪来,为什么这样配置 App.config或其他.config文件里加入如下配置 <log4net> < ...

  9. 数据可视化之pyecharts

    Echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生成 Echarts 图表的类库.实际上就是 Echarts 与 Python 的对接. 安装 ...

  10. Python 文件读取

    1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file ...