根据Problem Solving with Algorithms and Data Structures using Python 一书用python实现链表

书籍在线网址http://interactivepython.org/runestone/static/pythonds/index.html

中文翻译书籍:https://facert.gitbooks.io/python-data-structure-cn/

 class Node:     #链表中单个节点的实现
def __init__(self,initdata):
self.data=initdata
self.next=None def getDate(self): #获取当前节点数据域值
return self.data def getNext(self): #获取指向下一个节点的指针域值
return self.next def setData(self,newdata): #重新设置当前节点数据域值
self.data=newdata def setNext(self,newnext): #重新设置当前节点指针域指针指向
self.next=newnext class UnorderedList:
def __init__(self): #初始化无序列空表
self.head=None def isEmpty(self):
return self.head==None def add(self,item): #使用头插法将节点插入链表
temp=Node(item)
temp.setNext(self.head)
self.head=temp def append(self,item): #使用尾插法将节点插入链表
temp=Node(item)
if self.isEmpty(): #链表为空直接将头指针指向新的节点
self.head=temp
else:
current=self.head
while current.getNext()!=None: #遍历一遍链表中节点
current=current.getNext()
current.setNext(temp) #最后将节点插入链表尾部 def size(self): #统计节点数
current=self.head
count=0
while current!=None:
count+=1
current=current.getNext()
return count def travel(self): #遍历链表
print('遍历链表')
current=self.head
list1=[]
while current!=None:
list1.append(current.getDate())
current=current.getNext()
print(list1) def search(self,item): #搜索节点,返回Boolean值类型
current=self.head
found=False
while current!=None and not found:
if current.getDate() ==item:
found=True
else:
current=current.getNext()
return found def index(self,item): #搜索节点,返还节点所在索引值
current=self.head
count=0
found=None
while current!=None and not found:
count+=1
if current.getDate()==item:
found=True
else:
current=current.getNext()
if found:
return count
else:
str1='%s is not in theList'%item
return str1 def remove(self,item):
current=self.head
previous=None
found=False
while not found:
if current.getDate()==item:
found=True
else:
previous=current
current=current.getNext()
if previous==None: #删除的为第一个节点
self.head=current.getNext()
else: #删除的不是第一个节点
previous.setNext(current.getNext()) def insert(self,pos,item): #在链表指定位置插入节点
if pos<=1: #需要在第一个索引处插入节点,只需使用头插法将节点插入链表
self.add(item)
elif pos>self.size(): #如果插入的位置大于链表长度,尾插法插入节点
self.append(item)
else:
temp=Node(item)
count=1
pre=None
current=self.head
while count<pos: #在链表中间插入需要用一个前置指针和当前指针分别遍历指向插入点和插入点前部
count+=1
pre=current
current=current.getNext()
pre.setNext(temp)
temp.setNext(current) if __name__=='__main__':
a=UnorderedList()
for i in range(10):
a.append(i)    #此处用尾插法构造链表
#a.add(i)     #头插法创建链表
print('无序链表的大小为',a.size())
a.travel()
print('*********************')
print('搜索无序链表中节点4',a.search(4))
print('搜索无序链表中节点4的索引',a.index(4))
print('移除无序链表中节点7')
a.remove(7)
a.travel()
print('在索引5插入值为90的节点')
a.insert(5,90)
a.travel() #无序链表的大小为 10
# 遍历链表
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# *********************
# 搜索无序链表中节点4 True
# 搜索无序链表中节点4的索引 5
# 移除无序链表中节点7
# 遍历链表
# [0, 1, 2, 3, 4, 5, 6, 8, 9]
# 在索引5插入值为90的节点
# 遍历链表
# [0, 1, 2, 3, 90, 4, 5, 6, 8, 9]

python链表的实现的更多相关文章

  1. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  2. Python链表操作(实现)

    Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...

  3. python 链表表达式 map、filter易读版

    链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...

  4. Python链表与反链表

    # -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...

  5. python 链表

    在C/C++中,通常采用“指针+结构体”来实现链表:而在Python中,则可以采用“引用+类”来实现链表. 节点类: class Node: def __init__(self, data): sel ...

  6. python链表的实现,有注释

    class Node():                   #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个  __slots__=['_item' ...

  7. python 链表的反转

    code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self. ...

  8. python 链表、堆、栈

    简介 很多开发在开发中并没有过多的关注数据结构,当然我也是,因此,我写这篇文章就是想要带大家了解一下这些分别是什么东西. 链表 概念:数据随机存储,并且通过指针表示数据之间的逻辑关系的存储结构. 链表 ...

  9. Add Two Numbers(from leetcode python 链表)

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

随机推荐

  1. (十)Thymeleaf用法——Themeleaf内联

    5. 内联 [[...]]是内联文本的表示格式,但需要使用th:inline属性(分为text,javascript,none)激活. 5.1 文本内联    <p th:inline=&quo ...

  2. IDEA报compilation failed:internal java compiler error解决方法

    java complier 设置的问题  ,项目中有的配jdk1.6,有的配jdk1.7,版本不一样,导致这样的错误,提示这样的报错时,从file-Settings进入

  3. java System属性列表

    http://blog.csdn.net/bryanliu1982/article/details/5205636 比如获取windows登录用户主目录 String usreHome = Syste ...

  4. flex-direction用法解释

    语法: flex-direction:row | row-reverse | column | column-reverse 默认值:row 适用于:flex容器 继承性:无 动画性:否 计算值:指定 ...

  5. Java 下载JDK账号

    目前在官网下载低于jdk1.8的Javajdk的时候需要登陆,这边分享一个账号,方便下载 账号:2696671285@qq.com 密码:Oracle123 javajdk下载地址:点击打开链接

  6. 解决scrollView中嵌套viewPager的冲突问题

    很简单,在外层ScrollView中添加android:fillViewport="true"属性,然后给viewPager添加一个固定高度

  7. vi/vim复制粘贴命

    1. 选定文本块.使用v进入可视模式,移动光标键选定内容. 2.复制的命令是y,即yank(提起) ,常用的命令如下:     y      在使用v模式选定了某一块的时候,复制选定块到缓冲区用:   ...

  8. emacs的常用配置备份

    据说有人搞丢了自己的emacs的配置,然后一怒之下抛弃了emacs投身vim,我还是做个emacs配置的备份吧, 虽然我现在也算不上emacs的发烧友. 这里的配置大多是从网上参考的,最多的是下面的链 ...

  9. iOS系列译文:自定义Collection View布局

    原文出处: Ole Begemann   译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...

  10. Teradata架构

    Teradata在整体上是按Shared Nothing 架构体系进行组织的,他的定位就是大型数据仓库系统,定位比较高,他的软硬件都是NCR自己的,其他的都不识别:所以一般的企业用不起,价格很贵.由于 ...