根据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. 未能从程序集“Oracle.ManagedDataAccess”加载 “OracleInternal.Common.ConfigBaseClass”

    使用VS2015做项目的过程中一直使用的服务器上的oracle数据库,后来想学习一下oracle,就在本机安装了oracle.可没想到本来运行好好的项目,现在不能运行了.项目是使用的Abp框架,当运行 ...

  2. Unable to connect to a repository at URL 解决方法

    提示"Unable to connect to a repository at URL 'svn://localhost/project1/'" or “Can't connect ...

  3. asp.net repeater Container.ItemIndex

    <asp:Repeater ID="myRepeater" runat="server"> <HeaderTemplate> <t ...

  4. java使用命令wsimport构建WebService客户端

    wsimport -d d: -keep -extension -p com.demo.client http://192.168.33.3//RECEPTIONws.ASMX?WSDL 客户端:在J ...

  5. jsp 页面导出excel时字符串数字变成科学计数法的解决方法

    web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html   当我们把web页面上的数据 ...

  6. ubuntu安装分区

    ♠ 文件系统 windows下常见的文件系统有FAT, FAT32, NTFS 在linux里可使用的文件系统: Ext2: 早期的格式,不支持日志. Ext3: 是ext2改良版,增加了日志功能, ...

  7. 【SQLServer2008】之Win10 安装 SQL Server 2008

    查看安装步骤链接: http://jingyan.baidu.com/article/1709ad8092be974634c4f0e7.html

  8. Python鸡汤

    标准库 很正确 外部库 有一些风险,可能有bug,可能文档不全,可能长时间未更新. ipython 1 pip 这应该是安装Python后第一个需要的命令 pip install -i -i, --i ...

  9. android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag

    记录学习,网络摘抄 碰到这个异常其实这个上面说的很清楚,加个flag 从一个Activity中要通过intent调出另一个Activity的话,需要使用 FLAG_ACTIVITY_NEW_TASK ...

  10. 嵌入式开发之字符叠加---gb2313 国标码,utf8 国际码,unicode 无码

    (1)国标码简介 (2)编码转换 (3)时间获取 (4)显示切换 最近做了个字符叠加,包括时间叠加,字符中文叠加,位置移动,等功能开启.因为一般的字符叠加的点阵式16位,然后填充着16位的编码是gb2 ...