# -*- 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. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

  2. vue 图片lazyload

    今天看到我一醉哥的一篇朋友圈分享:<不如我们从头来过 | 掘金> 看完之后,百感交集,互联网的浪潮使创业公司如雨后春笋般崛起, 每一个初创公司都会有一群怀着美好愿景的小伙伴, 但是当寒冬来 ...

  3. CSS笔记 - fgm练习 2-10 - 提示框效果 (清除子元素浮动高度塌陷的影响)

    CSS清除浮动方法参考: https://blog.csdn.net/promiseCao/article/details/52771856 <style> *{ margin: 0; p ...

  4. [Angular] Setup automated deployment with Angular, Travis and Firebase

    Automate all the things!! Automation is crucial for increasing the quality and productivity. In this ...

  5. 多类 SVM 的损失函数及其梯度计算

    CS231n Convolutional Neural Networks for Visual Recognition -- optimization 1. 多类 SVM 的损失函数(Multicla ...

  6. HDU 1214 圆桌会议 圆环逆序

    http://acm.hdu.edu.cn/showproblem.php?pid=1214 题目大意: 一群人围着桌子座,如果在一分钟内一对相邻的人交换位置,问多少分钟后才能得到与原始状态相反的座位 ...

  7. P2P网贷中的4种理财业务模式

     线上3种   直投标:线上理财人直接购买借款人的标,平台只是起个"撮合"作用,收点借款人的服务费.           借款人不还钱,有的平台会帮"借款人"还 ...

  8. 自己定义Dialog的具体步骤(实现自己定义样式一般原理)

    转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自己定义Dialog的具体步骤(实现自己定义样式一般原理)    ...

  9. [Angular] Step-By-Step Implementation of a Structural Directive - Learn ViewContainerRef

    For example we have two buttons: When we click nether one of those tow button, the modal should show ...

  10. mongodb查询部分满足条件的列

    db.tblorders.createIndex( { orderid : -1 },{background:true, name:"index_orderid"} ); db.o ...