根据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. SDUT 2766-小明传奇2(母函数)

    小明传奇2 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limit 65536K ...

  2. 【Lucene】Apache Lucene全文检索引擎架构之构建索引2

    上一篇博文中已经对全文检索有了一定的了解,这篇文章主要来总结一下全文检索的第一步:构建索引.其实上一篇博文中的示例程序已经对构建索引写了一段程序了,而且那个程序还是挺完善的.不过从知识点的完整性来考虑 ...

  3. 使用mescroll实现上拉加载与下拉刷新

    现在上拉加载与下拉刷新几乎已经是移动端必备功能之一了,自己实现一个太麻烦,但是好用的插件又非常少.之前看到网上很多人都在用iScroll,于是也尝试用它做了几个DEMO,但或多或少都有一些问题,比如这 ...

  4. [效果不错] nginx 高并发参数配置及linux内核参数优化,完整的内核优化设置。PHP-FPM高负载解决办法。

    背景:对vps小资源的实践中对,https://justwinit.cn/post/7536/ 的再优化,再实践,再优化,特别是Nginx,PHP,内核: 零)Nginx: error_log /da ...

  5. HTML DOM节点的增删改查

    上篇博客中,我们已经初步接触了DOM基础,可是我们学习是为了可以更好地应用,今天我们就来看看DOM节点的增删改查. 无论在哪里,我们想要操作一个东西,总是应该先去获得它.那么我们怎么获得呢? HTML ...

  6. 查看tcp各个连接状态的数量

    4. 查看tcp各个连接状态的数量 下面对的 netstat -tan|awk '$1~/tcp/{aa[$NF]++}END{for (h in aa)print h,aa[h]}' SYN_SEN ...

  7. 【转】【Python学习】之哪些 Python 库让你相见恨晚?

    感谢作者:赖明星 文章链接地址:<哪些 Python 库让你相见恨晚?>

  8. 【JMeter4.0学习(九)】之定时器

    目录: 固定定时器 高斯随机定时器 附 一.固定定时器 1.添加线程组 2.添加固定定时器 3.添加HTTP请求 4.添加结果树以及运行  二.高斯随机定时器 1.添加线程组 2.添加高斯随机定时器 ...

  9. erlang 求N以内的质数

    素数,又称质数,在一个大于1的自然数中,除了1和此整数自身之外,不能被其他自然数整除的数. 比1大但不是素数的数称为合数. 1和0既不是素数,也不是合数. 算术基本定理证明每个大于1的正整数都可以写成 ...

  10. [c++]对象指针,引用的操作

    1.time类保存在"htime.h"中.要求: ⑴ 数据成员包括时(hour).分(minute).秒(second),为私有成员: ⑵ 能给数据成员提供值的成员函数(默认值为0 ...