根据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. Bmob实现android云端存储

    代码地址如下:http://www.demodashi.com/demo/12547.html 前言 一直很困惑,android到底能不能将本地数据传到一个公共的云端,让云端实现数据库功能,这样的话, ...

  2. css进阶读书笔记

    说明:努力在十一左右,最迟双11之前掌握所有css知识要点 一.摘自<写给大家看的CSS书(第2版)>(虽然书比较旧,09年版的,但对于我这种刚入门的小菜鸟 来说,能学到的还是挺多的) 1 ...

  3. Linux下安装jdk8的方法

    一.yum安装 只需要一条命令就可以安装jdk: yum install java--openjdk* -y 执行过这条命令无需配置,直接可以使用. 二.下载tar包安装 下载jdk8 登录网址:ht ...

  4. linux之ftp命令详解

    我们在使用ftp客户端访问到ftp服务器之后,往往需要进行相关操作,比如从远程机器上下载文件,或者将文件传输到远程机器上.需要使用ftp的相关命令,本文讲述了ftp常用的一些操作. 方法/步骤     ...

  5. C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1

    Chapter 1.1 1. 每个C++程序都必须有且只能有一个main函数,main函数的返回类型必须是int.操作系统通过调用main函数来运行C++程序. 2. 一个函数的定义包含四部分:返回类 ...

  6. kill 命令

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...

  7. ActivityLifecycleCallbacks 如何控制activity的生命周期

    Android开发 - ActivityLifecycleCallbacks使用方法初探 初识 ActivityLifecycleCallbacks 利用ActivityLifecycleCallba ...

  8. WEBserver、应用程序server、HTTPserver差别

    WEBserver.应用程序server.HTTPserver差别 WEBserver.应用程序server.HTTPserver有何差别?IIS.Apache.Tomcat.Weblogic.Web ...

  9. shader随记

    o.WorldPos = normalize(mul((float4x4)unity_ObjectToWorld, v.vertex)).xyz;

  10. js父页面和子页面之间传值

    今天和朋友一块讨论,怎样通过js在父页面和子页面之间传值的问题,总结例如以下: 需求描写叙述:父页面有多个子页面.实如今父页面点击子页面,传值到子页面. 看着非常easy,试了好久.主要纠结在怎样获取 ...