Python实现数据结构线性链表(单链表)算法示例


Posted in Python onMay 04, 2019

本文实例讲述了Python实现数据结构线性链表(单链表)算法。分享给大家供大家参考,具体如下:

初学python,拿数据结构中的线性链表存储结构练练手,理论比较简单,直接上代码。

#!/usr/bin/python
# -*- coding:utf-8 -*-
# Author: Hui
# Date:  2017-10-13
# 结点类,
class Node:
  def __init__(self, data):
    self.data = data      # 数据域
    self.next = None      # 指针域
  def get_data(self):
    return self.data
# 链表类
class List:
  def __init__(self, head):
    self.head = head      # 默认初始化头结点
  def is_empty(self):     # 空链表判断
    return self.get_len() == 0
  def get_len(self):     # 返回链表长度
    length = 0
    temp = self.head
    while temp is not None:
      length += 1
      temp = temp.next
    return length
  def append(self, node):     # 追加结点(链表尾部追加)
    temp = self.head
    while temp.next is not None:
      temp = temp.next
    temp.next = node
  def delete(self, index):      # 删除结点
    if index < 1 or index > self.get_len():
      print "给定位置不合理"
      return
    if index == 1:
      self.head = self.head.next
      return
    temp = self.head
    cur_pos = 0
    while temp is not None:
      cur_pos += 1
      if cur_pos == index-1:
        temp.next = temp.next.next
      temp = temp.next
  def insert(self, pos, node):     # 插入结点
    if pos < 1 or pos > self.get_len():
      print "插入结点位置不合理..."
      return
    temp = self.head
    cur_pos = 0
    while temp is not Node:
      cur_pos += 1
      if cur_pos == pos-1:
        node.next = temp.next
        temp.next =node
        break
      temp = temp.next
  def reverse(self, head):     # 反转链表
    if head is None and head.next is None:
      return head
    pre = head
    cur = head.next
    while cur is not None:
      temp = cur.next
      cur.next = pre
      pre = cur
      cur = temp
    head.next = None
    return pre
  def print_list(self, head):      # 打印链表
    init_data = []
    while head is not None:
      init_data.append(head.get_data())
      head = head.next
    return init_data
if __name__ == '__main__':
  head = Node("head")
  list = List(head)
  print '初始化头结点:\t', list.print_list(head)
  for i in range(1, 10):
    node = Node(i)
    list.append(node)
  print '链表添加元素:\t', list.print_list(head)
  print '链表是否空:\t', list.is_empty()
  print '链表长度:\t', list.get_len()
  list.delete(9)
  print '删除第9个元素:\t',list.print_list(head)
  node = Node("insert")
  list.insert(3, node)
  print '第3个位置插入‘insert'字符串 :\t', list.print_list(head)
  head = list.reverse(head)
  print '链表反转:', list.print_list(head)

执行结果:

Python实现数据结构线性链表(单链表)算法示例

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python 调用DLL操作抄表机
Jan 12 Python
Python中replace方法实例分析
Aug 20 Python
简单介绍Python下自己编写web框架的一些要点
Apr 29 Python
python文件与目录操作实例详解
Feb 22 Python
python3.4用函数操作mysql5.7数据库
Jun 23 Python
python生成随机图形验证码详解
Nov 08 Python
Python爬虫_城市公交、地铁站点和线路数据采集实例
Jan 10 Python
python实现日常记账本小程序
Mar 10 Python
python 不同方式读取文件速度不同的实例
Nov 09 Python
python pytest进阶之fixture详解
Jun 27 Python
Python 计算任意两向量之间的夹角方法
Jul 05 Python
Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解
Mar 30 Python
Python实现html转换为pdf报告(生成pdf报告)功能示例
May 04 #Python
Python实现将HTML转成PDF的方法分析
May 04 #Python
Python第三方库face_recognition在windows上的安装过程
May 03 #Python
Python人脸识别第三方库face_recognition接口说明文档
May 03 #Python
Python使用到第三方库PyMuPDF图片与pdf相互转换
May 03 #Python
利用python将图片版PDF转文字版PDF
May 03 #Python
Python3.0中普通方法、类方法和静态方法的比较
May 03 #Python
You might like
利用Memcached在php下实现session机制 替换PHP的原生session支持
2010/08/21 PHP
解决PHP超大文件下载,断点续传下载的方法详解
2013/06/06 PHP
推荐一款PHP+jQuery制作的列表分页的功能模块
2014/10/14 PHP
PHP嵌套输出缓冲代码实例
2015/05/12 PHP
php实时倒计时功能实现方法详解
2017/02/27 PHP
PHP标准库(PHP SPL)详解
2019/03/16 PHP
laravel admin实现分类树/模型树的示例代码
2020/06/10 PHP
javaScript 关闭浏览器 (不弹出提示框)
2010/01/31 Javascript
16个最流行的JavaScript框架[推荐]
2011/05/29 Javascript
js获取某元素的class里面的css属性值代码
2014/01/16 Javascript
JavaScript中Number.MAX_VALUE属性的使用方法
2015/06/04 Javascript
JavaScript实现添加及删除事件的方法小结
2015/08/04 Javascript
JavaScript中点击事件的写法
2016/06/28 Javascript
JS 实现微信扫一扫功能
2018/09/14 Javascript
jquery多级树形下拉菜单的实例代码
2019/07/09 jQuery
JS工厂模式开发实践案例分析
2019/10/17 Javascript
js实现踩五彩块游戏
2020/02/08 Javascript
JS实现简易贪吃蛇游戏
2020/08/24 Javascript
js实现飞机大战小游戏
2020/08/26 Javascript
vue项目实现减少app.js和vender.js的体积操作
2020/11/12 Javascript
python33 urllib2使用方法细节讲解
2013/12/03 Python
python得到单词模式的示例
2018/10/15 Python
python实现定时压缩指定文件夹发送邮件
2020/12/22 Python
python3实现弹弹球小游戏
2019/11/25 Python
Python安装whl文件过程图解
2020/02/18 Python
python 中关于pycharm选择运行环境的问题
2020/10/31 Python
Big Green Smile德国网上商店:提供各种天然产品
2018/05/23 全球购物
JSF面试题:Jsf中导航的标签是什么
2013/04/20 面试题
当当网软件测试笔试题
2015/11/24 面试题
合作意向书范本
2014/03/31 职场文书
经销商年会策划方案
2014/05/29 职场文书
群众路线四风对照检查材料
2014/11/04 职场文书
关于远足的感想
2015/08/10 职场文书
2016年党校科级干部培训班学习心得体会
2016/01/06 职场文书
PHP实现创建以太坊钱包转账等功能
2021/04/21 PHP
多人盗宝《绿林侠盗》第三赛季4.5上线 跨平台实装
2022/04/03 其他游戏