# -*- 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. libcurl 上传文件至 web服务器

    测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码  下载地址:  http://download.csdn.ne ...

  2. 2. Dubbo和Zookeeper的关系

    转自:https://www.cnblogs.com/hirampeng/p/9540243.html Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用: ...

  3. golang encoding/json

    package main import ( "bytes" "encoding/json" "fmt" ) type ColorGroup ...

  4. 关于CSDN2013博客之星的一些看法

    最近一个周,最火的话题当然要数CSDN2013博客之星拉票了. 实话实说,从12月14日开始,我连续5天拉票. 通过QQ群.QQ好友.CSDN文章.给CSDN粉丝发私信等多种方式拉票,真是累死我了. ...

  5. USB串行端口

    USB-SERIAL CH341A(COM22)USB串行端口

  6. Tomcat redis 配置

    http://www.cnblogs.com/interdrp/p/4868740.html http://blog.csdn.net/qq584852076/article/details/4650 ...

  7. 【Android开发经验】我们要友好的告诉用户,程序要崩溃了

        转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992     尽管我们的程序在正式上线之前,都会经过严格的測试.从而保证程序的健壮性和良好的用户体验,可是 ...

  8. iOS8: 企业开发的终结?

    iOS 8 的公布(如今是 iOS8.1),并非对全部人来说都是值得高兴的事情. 对那些使用企业部署(不经过商店公布)的 app 开发人员来说,又被苹果坑到了. 由于 iOS 8 的一个Bug.导致企 ...

  9. 【习题 3-8 UVA - 202】Repeating Decimals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...

  10. 洛谷—— P1091 合唱队形

    https://www.luogu.org/problem/show?pid=1091#sub  ||  http://codevs.cn/problem/1058/ 题目描述 N位同学站成一排,音乐 ...