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实现拷贝多个文件到同一目录的方法
Sep 19 Python
Python tkinter模块中类继承的三种方式分析
Aug 08 Python
numpy中索引和切片详解
Dec 15 Python
Python之多线程爬虫抓取网页图片的示例代码
Jan 10 Python
Python脚本完成post接口测试的实例
Dec 17 Python
python实现Dijkstra静态寻路算法
Jan 17 Python
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
Feb 17 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
如何打包Python Web项目实现免安装一键启动的方法
May 21 Python
对Python 字典元素进行删除的方法
Jul 31 Python
python将YUV420P文件转PNG图片格式的两种方法
Jan 22 Python
python高温预警数据获取实例
Jul 23 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
php定界符
2014/06/19 PHP
ThinkPHP之M方法实例详解
2014/06/20 PHP
php实现redis数据库指定库号迁移的方法
2015/01/14 PHP
php实现SAE上使用storage上传与下载文件的方法
2015/06/29 PHP
在Laravel框架里实现发送邮件实例(邮箱验证)
2016/05/20 PHP
浅谈mysql_query()函数的返回值问题
2016/09/05 PHP
php 生成Tab键或逗号分隔的CSV
2016/09/24 PHP
php中html_entity_decode实现HTML实体转义
2018/06/13 PHP
PHP设计模式之策略模式原理与用法实例分析
2019/04/04 PHP
JS弹出窗口代码大全(详细整理)
2012/12/21 Javascript
jQuery的attr与prop使用介绍
2013/10/10 Javascript
php跨域调用json的例子
2013/11/13 Javascript
在每个匹配元素的外部插入新元素的方法
2013/12/20 Javascript
vue中七牛插件使用的实例代码
2017/07/28 Javascript
微信小程序 循环及嵌套循环的使用总结
2017/09/26 Javascript
详谈构造函数加括号与不加括号的区别
2017/10/26 Javascript
关于express与koa的使用对比详解
2018/01/25 Javascript
JS滚轮控制图片缩放大小和拖动的实例代码
2018/11/20 Javascript
Vue服务端渲染实践之Web应用首屏耗时最优化方案
2019/03/22 Javascript
jQuery操作选中select下拉框的值代码实例
2020/02/07 jQuery
JavaScript前后端JSON使用方法教程
2020/11/23 Javascript
python strip()函数 介绍
2013/05/24 Python
Python实现扫描指定目录下的子目录及文件的方法
2014/07/16 Python
pandas series序列转化为星期几的实例
2018/04/11 Python
python通过Windows下远程控制Linux系统
2018/06/20 Python
Python玩转Excel的读写改实例
2019/02/22 Python
python读写csv文件方法详细总结
2019/07/05 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
python语言的优势是什么
2020/06/17 Python
css3过渡_动力节点Java学院整理
2017/07/11 HTML / CSS
CSS3圆角和渐变2种常用功能详解
2016/01/06 HTML / CSS
会计专业自我鉴定范文
2013/10/06 职场文书
初入社会应届生求职信
2013/11/18 职场文书
小学师德师风演讲稿
2014/09/02 职场文书
2015年信访维稳工作总结
2015/04/07 职场文书
SQL注入的实现以及防范示例详解
2021/06/02 MySQL