python 单向循环列表
# -*- 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 单向循环列表的更多相关文章
- Python倒序循环列表(序列)
如果要倒序遍历访问序列中的元素,可以对该序列使用reversed() 函数,reversed函数会生成一份倒序列表的拷贝,但是不会改变原列表.这个函数理解起来很自然,例如 for i in rever ...
- Python循环列表的方法
python循环列表的几种方法: 第一,依次打印列表中的各项值. 1 #!usr/bin/env python3 2 #!-*- Coding:utf-8 -*- 3 4 ''' 5 多种循环列表的方 ...
- Python的循环
循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法: ——for iter ...
- python学习笔记——列表生成式与生成器
1.列表生成式(List Comprehensions) python中,列表生成式是用来创建列表的,相较于用循环实现更为简洁.举个例子,生成[1*1, 2*2, ... , 10*10],循环用三行 ...
- Python 第二篇:python字符串、列表和字典的基本操作方法
本文基于python 3.5.1 python常见的数据类型有字串.列表.元组.字典等,本文将详细介绍每一种数据类型的操作方法. 一:str字串的操作方法: 1.capitalize()--> ...
- Python 迭代器和列表解析
Python 迭代器和列表解析 1)迭代器 一种特殊的数据结构,以对象形式存在 >>> i1 = l1.__iter__() >>> i1 = iter(l1) 可 ...
- python基础(五)列表,元组,集合
列表 在python中是由数个有序的元素组成的数据结构,每一个元素对应一个index索引来隐式标注元素在列表中的位置.是python中最常用的一种数据类型.需要注意的是列表中可以有重复相同的数据. 列 ...
- Python编程笔记 - 列表
这篇文章开始介绍Python中的容器.Python容器包括列表.元组.集合与字典.这些数据结构中都涉及到很多的方法,这里对比较常用的一些方法进行介绍,不用每个方法都记住,熟悉常用的即可. 首先,我们先 ...
- Python 迭代器之列表解析
 [TOC] 尽管while和for循环能够执行大多数重复性任务, 但是由于序列的迭代需求如此常见和广泛, 以至于Python提供了额外的工具以使其更简单和高效. 迭代器在Python中是以C语言的 ...
随机推荐
- C#集合类:动态数组、队列、栈、哈希表、字典
1.动态数组:ArrayList 主要方法:Add.AddRange.RemoveAt.Remove 2.队列:Queue 主要方法:Enqueue入队列.Dequeue出队列.Peek返回Queue ...
- 【CS Round #43 A】Expected Dice
[链接]https://csacademy.com/contest/round-43/task/expected-dice/ [题意] 大水题 [题解] 把36种可能的结果都存下来. 然后把重复出现的 ...
- JS学习笔记 - cookie设置、读取、删除
<script> // 设置cookie function setCookie(name, value, iDay) { var oDate = new Date(); oDate.set ...
- html5 10大html5前端框架
Bootstrap 首先说 Bootstrap,估计你也猜到会先说或者一定会有这个( 呵呵了 ),这是说明它的强大之处,拥有框架一壁江山的势气.自己刚入道的时候本着代码任何一个字母都得自己敲出来挡我者 ...
- 自定义npm包的创建、发布、更新和撤销
大纲 1.准备2.自定义npm包3.发布自定义npm包4.引用npm包5.更新npm包6.撤销发布的npm包 简书原文 https://www.jianshu.com/p/d737bc5df5b7 1 ...
- 11.2 Android显示系统框架_android源码禁用hwc和GPU
2. 修改tiny4412_Android源码禁用hwc和gpu(厂家不会提供hwc和gpu的源代码,没有源代码就没法分析了,因此在这里禁用该功能并用软件库实现)最终源码: git clone htt ...
- 【习题 5-8 UVA - 230】Borrowers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...
- js静态私有变量(将方法变成原型模式,被所有实例共享,而方法操作变量,故变量是静态)
js静态私有变量(将方法变成原型模式,被所有实例共享,而方法操作变量,故变量是静态) 一.总结 1.js函数中的private和public:js函数中的私有变量 var 变量名,公有变量 this. ...
- 影响stm32仿真的因素
可能是因为电池电量不足??? 电量不足可能会妨碍SD卡的挂载
- swift学习第十五天:闭包
闭包 闭包的介绍 闭包和OC中的block非常相似 OC中的block是匿名的函数 Swift中的闭包是一个特殊的函数 block和闭包都经常用于回调 注意:闭包和block一样,第一次使用时可能不习 ...