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语言的 ...
随机推荐
- 41.关于Intellij IDEA菜单项中Compile、Make和Build的区别
转自:https://www.cnblogs.com/ini_always/archive/2011/10/23/2221985.html Compile.Make和Build的区别 针对Java ...
- 利用ServiceWorker实现页面的快速加载和离线访问
Service workers 本质上充当Web应用程序与浏览器之间的代理服务器,也可以在网络可用时作为浏览器和网络间的代理.它们旨在(除其他之外)使得能够创建有效的离线体验,拦截网络请求并基于网络是 ...
- MySQL系列之七:主从复制(转)
一:实验环境 IP 操作系统 mysql版本号 master 192.168.25.11 CentOS7 5.6.35 slave 192.168.25.10 win10 5.7.18 slave版本 ...
- Windows 64位下 python3.4.3 安装numpy scipy
Numpy: 1.在开始菜单搜索cmd打开 终端 2.在终端输入python -m pip install -U pip 3.到http://www.lfd.uci.edu/~gohlke/pytho ...
- 16进制串与ASCII字符串相互转换
提供两个函数,方便十六进制串与ASCII 字符串之间的相互转换,使用函数需要注意的是返回的串是在堆上通过 calloc 分配的,所以,记得使用完返回值释放该块,并且将指向该块的指针 =NULL .// ...
- vim 保存文件的回车换行模式
设置模式:unix,dos :set fileformat=unix fileforman可以直接缩写为ff
- 如何在Win8/Win10上开启 dotNetFramework 2.0/3.5 功能
问题: 在Windows 8.Windows 10上安装一些软件时,系统可能会报出如下错误:你的电脑上的应用需要使用以下Windows功能: 解决方式: 首先呢,你需要准备好一个Win8/ ...
- [RxJS] Conclusion: when to use Subjects
As a conclusion to this course about RxJS subjects, let's review when and why should you use them. F ...
- Qt 使用qDebug() 打印Qlist 容器数据(将QDebug()定义成某个类的友元函数)
当QList<T>容器中的数据用qDebug() 打印时 ,假如 T 是内置类型(int float ...)与 打印一个字符串使用完全一样,假如T 是一个CustomerClass 那 ...
- thinkphp3.2二维码扩展
//简易二维码 public function qrcode(){ Vendor('phpqrcode.phpqrcode'); //生成二维码图片 $object = new \QRcode(); ...