python链表的实现
根据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链表的实现的更多相关文章
- Python链表的实现与使用(单向链表与双向链表)
参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...
- Python链表操作(实现)
Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- Python链表与反链表
# -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...
- python 链表
在C/C++中,通常采用“指针+结构体”来实现链表:而在Python中,则可以采用“引用+类”来实现链表. 节点类: class Node: def __init__(self, data): sel ...
- python链表的实现,有注释
class Node(): #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个 __slots__=['_item' ...
- python 链表的反转
code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self. ...
- python 链表、堆、栈
简介 很多开发在开发中并没有过多的关注数据结构,当然我也是,因此,我写这篇文章就是想要带大家了解一下这些分别是什么东西. 链表 概念:数据随机存储,并且通过指针表示数据之间的逻辑关系的存储结构. 链表 ...
- Add Two Numbers(from leetcode python 链表)
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
随机推荐
- charles用法详解
Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddler,同时是全平台支持,堪称圣杯级工具,唯一的缺陷是这货是收费的,而且是要¥50美元大洋…当然网上是有破解版的,鄙视下自己, ...
- 关于Linux网络配置
Linux网络配置 一:什么是网络接口卡以及如何查看网络接口的网络信息:在Linux系统中,主机的网络接口卡通常称为“网络接口”,我们可以使用ifconfig命令来查看网络 接口的信息(普通用户使用/ ...
- brew Error: Formulae found in multiple taps
Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...
- Thread 常搞混的几个概念sleep、wait、yield、interrupt
sleep:在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响.该线程不丢失任何监视器的所属权. 通过调用sleep使任务进入休眠状态,在这种情况下 ...
- [root@localhost ~]#各项解释
[root@localhost ~]# 解释: [登录用户@主机名 索引目录(~家目录,当前所在的目录)]#号代表超级用户,$普通用户
- Swift开发教程--怎样设置状态栏的文字颜色
第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO 第二步:在viewDidLoad中加一句 UIApplication.sh ...
- 【v2.x OGE教程 11】 动画编辑器帮助文档
] 动画编辑器帮助文档 版本号 日期 作者 说明 1.0 2014-9-3 橙子游戏 文档创建 一.简单介绍 动画编辑器用于游戏动画的可视化编辑,支持序列帧动画和关键帧动画.通过解析生成的 ...
- vs重复编译
VS用了这么久都没有这样的问题,昨天突然发现在自己电脑时间不对了,就调了下,以后这问题都来了.每次运行项目都要重新编译下,不管改不改底层代码.这让我很痛苦,浪费大量时间,找了好久才得到答案: .时间问 ...
- poj2349
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 0 Accepted: 0 Descript ...
- J - Max Sum
J - Max Sum Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...