# -*- coding: utf-8 -*-
# @author: Tele
# @Time : 2019/04/23 下午 6:54
# 单向循环列表
# 单向循环列表与单向列表的不同之处在于最后一个元素的next为头节点
class SingleCycleNode:
def __init__(self, data, next=None):
self.data = data
# next指向下一个节点而不是数据
self.next = next # 使用链表时只需要传入待存储的数据而不是节点
class SingleCycleLinkedList:
def __init__(self, data=None):
node = SingleCycleNode(data)
self.__head = node if node.data else None
if self.__head:
self.__head.next = node def is_empty(self):
return self.__head == None def length(self):
count = 0
if self.is_empty():
return count
cur = self.__head
if cur:
while cur.next is not self.__head:
count += 1
cur = cur.next
return count + 1 # 头部添加元素
def add(self, data):
node = SingleCycleNode(data)
if self.is_empty():
self.__head = node
self.__head.next = node
else:
node.next = self.__head
cur = self.__head
while cur.next is not self.__head:
cur = cur.next
# 此时cur为尾节点
cur.next = node
self.__head = node # 尾部添加元素
def append(self, data):
node = SingleCycleNode(data)
if self.is_empty():
self.__head = node
self.__head.next = node
else:
node.next = self.__head
cur = self.__head
while cur.next is not self.__head:
cur = cur.next
cur.next = node # 指定位置插入
def insert(self, pos, data):
node = SingleCycleNode(data)
cur = self.__head
count = 0
if self.length() >= pos >= 0:
while cur.next is not self.__head:
if count + 1 == pos:
node.next = cur.next
cur.next = node
break
# pos为0
elif count == pos:
self.add(data)
break
count += 1
cur = cur.next
elif pos < 0:
self.add(data)
else:
self.append(data)
# 如果列表中插入时没有元素
if not self.__head:
self.append(data) # 遍历
def travel(self):
cur = self.__head
while cur and cur.next is not self.__head:
print(cur.data)
cur = cur.next
print(cur.data if cur else "") # 移除出现的第一个元素
def remove(self, data):
if self.is_empty():
return
node = self.__find(data)
cur = self.__head
# 头节点
if self.__head.data == node.data:
while cur.next is not self.__head:
cur = cur.next
self.__head = node.next
cur.next = self.__head
# 尾节点,普通位置节点
else:
cur = self.__head
while cur.next and cur.next is not self.__head:
if cur.next.data == node.data:
break
cur = cur.next
cur.next = node.next # 私有方法,用于查找节点
def __find(self, data):
cur = self.__head
node = SingleCycleNode(data)
while cur and cur.next is not self.__head:
if cur.data == data:
node.next = cur.next
break
cur = cur.next
# 如果是尾节点
if cur and cur.data == data:
node.next = cur.next
return node # 查找,找不到返回-1,找到则返回索引
def search(self, data):
index = -1
cur = self.__head
count = 0
while cur and cur.next is not self.__head:
if cur.data == data:
index = count
break
count += 1
cur = cur.next
# 如果是尾节点
if cur and cur.data == data:
index = count
return index def main():
scll = SingleCycleLinkedList()
print(scll.is_empty())
print(scll.length())
scll.add(1)
scll.add(2)
scll.append(10)
scll.append(10)
scll.append(100)
print(scll.is_empty())
print(scll.length())
# print("*" * 50)
# scll.travel()
print("*" * 50)
scll.insert(-1, 1000)
print("search", scll.search(10))
scll.travel()
print("*" * 50)
scll.remove(10)
scll.travel() if __name__ == '__main__':
main()

python 单向循环列表的更多相关文章

  1. Python倒序循环列表(序列)

    如果要倒序遍历访问序列中的元素,可以对该序列使用reversed() 函数,reversed函数会生成一份倒序列表的拷贝,但是不会改变原列表.这个函数理解起来很自然,例如 for i in rever ...

  2. Python循环列表的方法

    python循环列表的几种方法: 第一,依次打印列表中的各项值. 1 #!usr/bin/env python3 2 #!-*- Coding:utf-8 -*- 3 4 ''' 5 多种循环列表的方 ...

  3. Python的循环

    循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法:   ——for iter ...

  4. python学习笔记——列表生成式与生成器

    1.列表生成式(List Comprehensions) python中,列表生成式是用来创建列表的,相较于用循环实现更为简洁.举个例子,生成[1*1, 2*2, ... , 10*10],循环用三行 ...

  5. Python 第二篇:python字符串、列表和字典的基本操作方法

    本文基于python 3.5.1 python常见的数据类型有字串.列表.元组.字典等,本文将详细介绍每一种数据类型的操作方法. 一:str字串的操作方法: 1.capitalize()-->  ...

  6. Python 迭代器和列表解析

    Python 迭代器和列表解析 1)迭代器 一种特殊的数据结构,以对象形式存在 >>> i1 = l1.__iter__() >>> i1 = iter(l1) 可 ...

  7. python基础(五)列表,元组,集合

    列表 在python中是由数个有序的元素组成的数据结构,每一个元素对应一个index索引来隐式标注元素在列表中的位置.是python中最常用的一种数据类型.需要注意的是列表中可以有重复相同的数据. 列 ...

  8. Python编程笔记 - 列表

    这篇文章开始介绍Python中的容器.Python容器包括列表.元组.集合与字典.这些数据结构中都涉及到很多的方法,这里对比较常用的一些方法进行介绍,不用每个方法都记住,熟悉常用的即可. 首先,我们先 ...

  9. Python 迭代器之列表解析

     [TOC] 尽管while和for循环能够执行大多数重复性任务, 但是由于序列的迭代需求如此常见和广泛, 以至于Python提供了额外的工具以使其更简单和高效. 迭代器在Python中是以C语言的 ...

随机推荐

  1. Solr的关键特性

    1.基于标准的开放接口:Solr搜索服务器支持通过XML.JSON和HTTP查询和获取结果. 2.易管理:Solr可以通过HTML页面管理,Solr配置通过XML完成. 3.可伸缩性:能够有效地复制到 ...

  2. Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

    package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...

  3. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  4. Java 学习(18):Java 序列化& 网络编程& 发送邮件

    --Java 序列化 -- 网络编程 -- 发送邮件 Java 序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信 ...

  5. POJ 1932 XYZZY (ZOJ 1935)SPFA+floyd

    http://poj.org/problem?id=1932 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1935 题目大 ...

  6. Codeforces Round #315 (Div. 2) (ABCD题解)

    比赛链接:http://codeforces.com/contest/569 A. Music time limit per test:2 seconds memory limit per test: ...

  7. ArcGIS 中要素的查询与修改

    转自nimeila的回答 求C# ArcGIS Engine修改选中要素的属性,单要素都行 RLAlterFrm RLalter = new RLAlterFrm(); RLalter.ShowDia ...

  8. ZYNQ7000 LVDS接口输出配置

    xilinx 7系列芯片不再支持LVDS33电平,在VCCO电压为3.3V的情况下无法使用LVDS25接口. 有些设计者想通过在软件中配置为LVDS25,实际供电3.3V来实现LVDS33也是无效的, ...

  9. php 删除数组指定元素,下标还不乱

    $arr是目标数组 $offset是要删除的元素的key 1是指删除的长度 array_splice($arr, $offset, 1); 之前用的unset,但是比如删除的是第三个,那么下标的2就会 ...

  10. Kinect 骨骼映射---Let me dance for U!

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45583443 作者:ca ...