侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python 实现链表实例代码

Python  /  管理员 发布于 7年前   167

Python 实现链表实例代码

前言

算法和数据结构是一个亘古不变的话题,作为一个程序员,掌握常用的数据结构实现是非常非常的有必要的。

实现清单

实现链表,本质上和语言是无关的。但是灵活度却和实现它的语言密切相关。今天用Python来实现一下,包含如下操作:

['addNode(self, data)']['append(self, value)']['prepend(self, value)']['insert(self, index, value)']['delNode(self, index)']['delValue(self, value)']['isempty(self)']['truncate(self)']['getvalue(self, index)']['peek(self)']['pop(self)']['reverse(self)']['delDuplecate(self)']['updateNode(self, index, value)']['size(self)']['print(self)']

生成这样的一个方法清单肯定是不能手动写了,要不然得多麻烦啊,于是我写了个程序,来匹配这些自己实现的方法。代码比较简单,核心思路就是匹配源文件的每一行,找到符合匹配规则的内容,并添加到总的结果集中。

代码如下:

# coding: utf8# @Author: 郭 璞# @File: getmethods.py         # @Time: 2017/4/5      # @Contact: [email protected]# @blog: http://blog.csdn.net/marksinoberg# @Description: 获取一个模块或者类中的所有方法及参数列表import redef parse(filepath, repattern):  with open(filepath, 'rb') as f:    lines = f.readlines()  # 预解析正则  rep = re.compile(repattern)  # 创建保存方法和参数列表的结果集列表  result = []  # 开始正式的匹配实现  for line in lines:    res = re.findall(rep, str(line))    print("{}的匹配结果{}".format(str(line), res))    if len(res)!=0 or res is not None:      result.append(res)    else:      continue  return [item for item in result if item !=[]]if __name__ == '__main__':  repattern = "def (.[^_0-9]+\(.*?\)):"  filepath = './SingleChain.py'  result = parse(filepath, repattern)  for item in result:    print(str(item))

链表实现

# coding: utf8# @Author: 郭 璞# @File: SingleChain.py         # @Time: 2017/4/5      # @Contact: [email protected]# @blog: http://blog.csdn.net/marksinoberg# @Description: 单链表实现class Node(object):  def __init__(self, data, next):    self.data = data    self.next = nextclass LianBiao(object):  def __init__(self):    self.root = None  # 给单链表添加元素节点  def addNode(self, data):    if self.root==None:      self.root = Node(data=data, next=None)      return self.root    else:      # 有头结点,则需要遍历到尾部节点,进行链表增加操作      cursor = self.root      while cursor.next!= None:        cursor = cursor.next      cursor.next = Node(data=data, next=None)      return self.root  # 在链表的尾部添加新节点,底层调用addNode方法即可  def append(self, value):    self.addNode(data=value)  # 在链表首部添加节点  def prepend(self, value):    if self.root == None:      self.root = Node(value, None)    else:      newroot = Node(value, None)      # 更新root索引      newroot.next = self.root      self.root = newroot  # 在链表的指定位置添加节点  def insert(self, index, value):    if self.root == None:      return    if index<=0 or index >self.size():      print('index %d 非法, 应该审视一下您的插入节点在整个链表的位置!')      return    elif index==1:      # 如果index==1, 则在链表首部添加即可      self.prepend(value)    elif index == self.size()+1:      # 如果index正好比当前链表长度大一,则添加在尾部即可      self.append(value)    else:      # 如此,在链表中部添加新节点,直接进行添加即可。需要使用计数器来维护插入未知      counter = 2      pre = self.root      cursor = self.root.next      while cursor!=None:        if counter == index:          temp = Node(value, None)          pre.next = temp          temp.next = cursor          break        else:          counter += 1          pre = cursor          cursor = cursor.next  # 删除指定位置上的节点  def delNode(self, index):    if self.root == None:      return    if index<=0 or index > self.size():      return    # 对第一个位置需要小心处理    if index == 1:      self.root = self.root.next    else:      pre = self.root      cursor = pre.next      counter = 2      while cursor!= None:        if index == counter:          print('can be here!')          pre.next = cursor.next          break        else:          pre = cursor          cursor = cursor.next          counter += 1  # 删除值为value的链表节点元素  def delValue(self, value):    if self.root == None:      return    # 对第一个位置需要小心处理    if self.root.data == value:      self.root = self.root.next    else:      pre = self.root      cursor = pre.next      while cursor!=None:        if cursor.data == value:          pre.next = cursor.next          # 千万记得更新这个节点,否则会出现死循环。。。          cursor = cursor.next          continue        else:          pre = cursor          cursor = cursor.next  # 判断链表是否为空  def isempty(self):    if self.root == None or self.size()==0:      return True    else:      return False  # 删除链表及其内部所有元素  def truncate(self):    if self.root == None or self.size()==0:      return    else:      cursor = self.root      while cursor!= None:        cursor.data = None        cursor = cursor.next      self.root = None      cursor = None  # 获取指定位置的节点的值  def getvalue(self, index):    if self.root is None or self.size()==0:      print('当前链表为空!')      return None    if index<=0 or index>self.size():      print("index %d不合法!"%index)      return None    else:      counter = 1      cursor = self.root      while cursor is not None:        if index == counter:          return cursor.data        else:          counter += 1          cursor = cursor.next  # 获取链表尾部的值,且不删除该尾部节点  def peek(self):    return self.getvalue(self.size())  # 获取链表尾部节点的值,并删除该尾部节点  def pop(self):    if self.root is None or self.size()==0:      print('当前链表已经为空!')      return None    elif self.size()==1:      top = self.root.data      self.root = None      return top    else:      pre = self.root      cursor = pre.next      while cursor.next is not None:        pre = cursor        cursor = cursor.next      top = cursor.data      cursor = None      pre.next = None      return top  # 单链表逆序实现  def reverse(self):    if self.root is None:      return    if self.size()==1:      return    else:      # post = None      pre = None      cursor = self.root      while cursor is not None:        # print('逆序操作逆序操作')        post = cursor.next        cursor.next = pre        pre = cursor        cursor = post      # 千万不要忘记了把逆序后的头结点赋值给root,否则无法正确显示      self.root = pre  # 删除链表中的重复元素  def delDuplecate(self):    # 使用一个map来存放即可,类似于变形的“桶排序”    dic = {}    if self.root == None:      return    if self.size() == 1:      return    pre = self.root    cursor = pre.next    dic = {}    # 为字典赋值    temp = self.root    while temp!=None:      dic[str(temp.data)] = 0      temp = temp.next    temp = None    # 开始实施删除重复元素的操作    while cursor!=None:      if dic[str(cursor.data)] == 1:        pre.next = cursor.next        cursor = cursor.next      else:        dic[str(cursor.data)] += 1        pre = cursor        cursor = cursor.next  # 修改指定位置节点的值  def updateNode(self, index, value):    if self.root == None:      return    if index<0 or index>self.size():      return    if index == 1:      self.root.data = value      return    else:      cursor = self.root.next      counter = 2      while cursor!=None:        if counter == index:          cursor.data = value          break        cursor = cursor.next        counter += 1  # 获取单链表的大小  def size(self):    counter = 0    if self.root == None:      return counter    else:      cursor = self.root      while cursor!=None:        counter +=1        cursor = cursor.next      return counter  # 打印链表自身元素  def print(self):    if(self.root==None):      return    else:      cursor = self.root      while cursor!=None:        print(cursor.data, end='\t')        cursor = cursor.next      print()if __name__ == '__main__':  # 创建一个链表对象  lianbiao = LianBiao()  # 判断当前链表是否为空  print("链表为空%d"%lianbiao.isempty())  # 判断当前链表是否为空  lianbiao.addNode(1)  print("链表为空%d"%lianbiao.isempty())  # 添加一些节点,方便操作  lianbiao.addNode(2)  lianbiao.addNode(3)  lianbiao.addNode(4)  lianbiao.addNode(6)  lianbiao.addNode(5)  lianbiao.addNode(6)  lianbiao.addNode(7)  lianbiao.addNode(3)  # 打印当前链表所有值  print('打印当前链表所有值')  lianbiao.print()  # 测试对链表求size的操作  print("链表的size: "+str(lianbiao.size()))  # 测试指定位置节点值的获取  print('测试指定位置节点值的获取')  print(lianbiao.getvalue(1))  print(lianbiao.getvalue(lianbiao.size()))  print(lianbiao.getvalue(7))  # 测试删除链表中指定值, 可重复性删除  print('测试删除链表中指定值, 可重复性删除')  lianbiao.delNode(4)  lianbiao.print()  lianbiao.delValue(3)  lianbiao.print()  # 去除链表中的重复元素  print('去除链表中的重复元素')  lianbiao.delDuplecate()  lianbiao.print()  # 指定位置的链表元素的更新测试  print('指定位置的链表元素的更新测试')  lianbiao.updateNode(6, 99)  lianbiao.print()  # 测试在链表首部添加节点  print('测试在链表首部添加节点')  lianbiao.prepend(77)  lianbiao.prepend(108)  lianbiao.print()  # 测试在链表尾部添加节点  print('测试在链表尾部添加节点')  lianbiao.append(99)  lianbiao.append(100)  lianbiao.print()  # 测试指定下标的插入操作  print('测试指定下标的插入操作')  lianbiao.insert(1, 10010)  lianbiao.insert(3, 333)  lianbiao.insert(lianbiao.size(), 99999)  lianbiao.print()  # 测试peek 操作  print('测试peek 操作')  print(lianbiao.peek())  lianbiao.print()  # 测试pop 操作  print('测试pop 操作')  print(lianbiao.pop())  lianbiao.print()  # 测试单链表的逆序输出  print('测试单链表的逆序输出')  lianbiao.reverse()  lianbiao.print()  # 测试链表的truncate操作  print('测试链表的truncate操作')  lianbiao.truncate()  lianbiao.print()

代码运行的结果如何呢?是否能满足我们的需求,且看打印的结果:

D:\Software\Python3\python.exe E:/Code/Python/Python3/CommonTest/datastructor/SingleChain.py链表为空1链表为空0打印当前链表所有值1  2  3  4  6  5  6  7  3  链表的size: 9测试指定位置节点值的获取136测试删除链表中指定值, 可重复性删除can be here!1  2  3  6  5  6  7  3  1  2  6  5  6  7  去除链表中的重复元素1  2  6  5  7  指定位置的链表元素的更新测试1  2  6  5  7  测试在链表首部添加节点108 77 1  2  6  5  7  测试在链表尾部添加节点108 77 1  2  6  5  7  99 100 测试指定下标的插入操作10010  108 333 77 1  2  6  5  7  99 99999  100 测试peek 操作10010010  108 333 77 1  2  6  5  7  99 99999  100 测试pop 操作10010010  108 333 77 1  2  6  5  7  99 99999  测试单链表的逆序输出99999  99 7  5  6  2  1  77 333 108 10010  测试链表的truncate操作Process finished with exit code 0

刚好实现了目标需求。

总结

今天的内容还是比较基础,也没什么难点。但是看懂和会写还是两码事,没事的时候写写这样的代码还是很有收获的。


  • 上一条:
    python使用opencv进行人脸识别
    下一条:
    python爬虫框架scrapy实战之爬取京东商城进阶篇
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客