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的Flask框架中的Jinja2模板引擎学习教程
Jun 30 Python
Python中datetime模块参考手册
Jan 13 Python
Apache如何部署django项目
May 21 Python
Python探索之自定义实现线程池
Oct 27 Python
Pycharm取消py脚本中SQL识别的方法
Nov 29 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 Python
python处理document文档保留原样式
Sep 23 Python
python判断两个序列的成员是否一样的实例代码
Mar 01 Python
python实现飞船游戏的纵向移动
Apr 24 Python
Pytorch转keras的有效方法,以FlowNet为例讲解
May 26 Python
keras 两种训练模型方式详解fit和fit_generator(节省内存)
Jul 03 Python
python logging模块的使用
Sep 07 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 水平的题目
2007/05/30 PHP
兼容PHP5的PHP目录管理函数库
2008/07/10 PHP
php imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
使用PHP获取当前url路径的函数以及服务器变量
2013/06/29 PHP
PHP循环函数使用介绍之PHP基础入门教程
2013/09/21 PHP
php遍历文件夹下的所有文件和子文件夹示例
2014/03/20 PHP
PHP中批量生成静态html(命令行下运行PHP)
2014/04/19 PHP
PHP FTP操作类代码( 上传、拷贝、移动、删除文件/创建目录)
2014/05/10 PHP
php获取文件类型和文件信息的方法
2015/07/10 PHP
thinkphp5实现微信扫码支付
2019/12/23 PHP
PHP标准库 (SPL)――Countable用法示例
2020/06/05 PHP
PHP常量及变量区别原理详解
2020/08/14 PHP
Javascript实例教程(19) 使用HoTMetal(2)
2006/12/23 Javascript
Bootstrap教程JS插件弹出框学习笔记分享
2016/05/17 Javascript
JavaScript鼠标特效大全
2016/09/13 Javascript
Bootstrap CSS布局之表单
2016/12/17 Javascript
Angular 4.0学习教程之架构详解
2017/09/12 Javascript
VUE重点问题总结
2018/03/19 Javascript
在移动端使用vue-router和keep-alive的方法示例
2018/12/02 Javascript
vue拖拽组件 vuedraggable API options实现盒子之间相互拖拽排序
2019/07/08 Javascript
React实现全选功能
2020/08/25 Javascript
[01:14:12]2018DOTA2亚洲邀请赛4.7 总决赛 LGD vs Mineski 第二场
2018/04/09 DOTA
[04:51]TI10典藏宝瓶Ⅱ外观视频展示
2020/08/15 DOTA
Python Socket编程入门教程
2014/07/11 Python
Python中用altzone()方法处理时区的教程
2015/05/22 Python
如何使用七牛Python SDK写一个同步脚本及使用教程
2015/08/23 Python
Python机器学习之决策树算法实例详解
2017/12/06 Python
Python实现的维尼吉亚密码算法示例
2018/04/12 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
2018/07/06 Python
Python反射和内置方法重写操作详解
2018/08/27 Python
Python 微信爬虫完整实例【单线程与多线程】
2019/07/06 Python
Django用户认证系统 Web请求中的认证解析
2019/08/02 Python
详解Django自定义图片和文件上传路径(upload_to)的2种方式
2020/12/01 Python
html Table 表头固定的实现
2019/01/22 HTML / CSS
2016党员三严三实心得体会
2016/01/15 职场文书
jdbc中自带MySQL 连接池实践示例
2022/07/23 MySQL