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 相关文章推荐
pyqt4教程之实现半透明的天气预报界面示例
Mar 02 Python
python itchat实现微信好友头像拼接图的示例代码
Aug 14 Python
解决Python requests库编码 socks5代理的问题
May 07 Python
Python连接Redis的基本配置方法
Sep 13 Python
python paramiko利用sftp上传目录到远程的实例
Jan 03 Python
树莓派采用socket方式文件传输(python)
Jun 22 Python
Python 线程池用法简单示例
Oct 02 Python
Python turtle画图库&amp;&amp;画姓名实例
Jan 19 Python
tensorflow 模型权重导出实例
Jan 24 Python
pandas 强制类型转换 df.astype实例
Apr 09 Python
pandas将list数据拆分成行或列的实现
Dec 13 Python
python函数超时自动退出的实操方法
Dec 28 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
Yii2使用自带的UploadedFile实现的文件上传
2016/06/20 PHP
用javascript自动显示最后更新时间
2007/03/15 Javascript
自写简单JS判断是否已经弹出页面
2010/10/20 Javascript
Javascript的常规数组和关联数组对比小结
2012/05/24 Javascript
JavaScript mapreduce工作原理简析
2012/11/25 Javascript
Java File类的常用方法总结
2015/03/18 Javascript
Angular.JS学习之依赖注入$injector详析
2016/10/20 Javascript
javascript入门之window对象【新手必看】
2016/11/22 Javascript
微信小程序中使用javascript 回调函数
2017/05/11 Javascript
探讨Vue.js的组件和模板
2017/10/27 Javascript
vue.js使用3DES加密的方法示例
2018/05/18 Javascript
react实现点击选中的li高亮的示例代码
2018/05/24 Javascript
使用vue-cli webpack 快速搭建项目的代码
2018/11/21 Javascript
JS实现的定时器展示简单秒表、页面弹框及跳转操作完整示例
2020/01/26 Javascript
Python 执行字符串表达式函数(eval exec execfile)
2014/08/11 Python
python获得两个数组交集、并集、差集的方法
2015/03/27 Python
TensorFlow实现Batch Normalization
2018/03/08 Python
python对csv文件追加写入列的方法
2019/08/01 Python
解决python replace函数替换无效问题
2020/01/18 Python
python将音频进行变速的操作方法
2020/04/08 Python
基于Python测试程序是否有错误
2020/05/16 Python
python爬虫基础之urllib的使用
2020/12/31 Python
美国顶级品牌男士大码服装店:DXL
2017/08/30 全球购物
丝芙兰中国官方商城:SEPHORA中国
2018/01/10 全球购物
西班牙在线宠物食品和配件商店:bitiba
2019/10/11 全球购物
旷课检讨书3000字
2014/02/04 职场文书
一年级语文教学反思
2014/02/13 职场文书
软件毕业生个人鉴定
2014/03/03 职场文书
小学生差生评语
2014/12/29 职场文书
2015年酒店工作总结范文
2015/04/07 职场文书
烈士陵园观后感
2015/06/08 职场文书
2016年大学生实习单位评语
2015/12/01 职场文书
MySQL查看表和清空表的常用命令总结
2021/05/26 MySQL
Python&Matlab实现樱花的绘制
2022/04/07 Python
python中 .npy文件的读写操作实例
2022/04/14 Python
python playwrigh框架入门安装使用
2022/07/23 Python