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 相关文章推荐
Python3基础之输入和输出实例分析
Aug 18 Python
跟老齐学Python之??碌某?? target=
Sep 12 Python
Python的Urllib库的基本使用教程
Apr 30 Python
Python字符串格式化的方法(两种)
Sep 19 Python
教你用一行Python代码实现并行任务(附代码)
Feb 02 Python
一篇文章读懂Python赋值与拷贝
Apr 19 Python
Python 实现删除某路径下文件及文件夹的实例讲解
Apr 24 Python
python+mysql实现教务管理系统
Feb 20 Python
Python实现微信中找回好友、群聊用户撤回的消息功能示例
Aug 23 Python
Django+python服务器部署与环境部署教程详解
Mar 30 Python
Django使用list对单个或者多个字段求values值实例
Mar 31 Python
pyqt5中动画的使用详解
Apr 01 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/05/12 PHP
CodeIgniter框架数据库事务处理的设计缺陷和解决方案
2014/07/25 PHP
PHP实现克鲁斯卡尔算法实例解析
2014/08/22 PHP
ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整
2014/11/05 PHP
动态表单验证的操作方法和TP框架里面的ajax表单验证
2017/07/19 PHP
Jquery实现页面加载时弹出对话框代码
2013/04/19 Javascript
jquery 鼠标滑动显示详情应用示例
2014/01/24 Javascript
无刷新预览所选择的图片示例代码
2014/04/02 Javascript
ExtJS中设置下拉列表框不可编辑的方法
2014/05/07 Javascript
谈谈jQuery Ajax用法详解
2015/11/27 Javascript
JavaScript &amp; jQuery完美判断图片是否加载完毕
2017/01/08 Javascript
实例详解display:none与visible:hidden的区别
2017/03/30 Javascript
jQuery实现文章图片弹出放大效果
2017/04/06 jQuery
vue2的todolist入门小项目的详细解析
2017/05/11 Javascript
Vue.js组件间的循环引用方法示例
2017/12/27 Javascript
详解离线安装npm包的几种方法
2018/11/25 Javascript
vue中用 async/await 来处理异步操作
2020/07/18 Javascript
jQuery实现滑动开关效果
2020/08/02 jQuery
[01:10]DOTA2次级职业联赛 - Fly战队宣传片
2014/12/01 DOTA
Python基于dom操作xml数据的方法示例
2018/05/12 Python
对python抓取需要登录网站数据的方法详解
2018/05/21 Python
pandas 对每一列数据进行标准化的方法
2018/06/09 Python
理想高通滤波实现Python opencv示例
2019/01/30 Python
python的Jenkins接口调用方式
2020/05/12 Python
使用openCV去除文字中乱入的线条实例
2020/06/02 Python
纯CSS3单页切换导航菜单界面设计的简单实现
2016/08/16 HTML / CSS
世界顶级户外运动品牌折扣网站:LeftLane Sports
2019/06/12 全球购物
泰国国际航空公司官网:Thai Airways International
2019/12/04 全球购物
小学生打架检讨书
2014/01/26 职场文书
工作迟到检讨书
2014/02/21 职场文书
竞聘自述材料
2014/08/25 职场文书
解放思想演讲稿
2014/09/11 职场文书
清洁工工作总结
2015/08/11 职场文书
大学生创业计划书
2019/06/24 职场文书
关于Python中*args和**kwargs的深入理解
2021/08/07 Python
Golang 切片(Slice)实现增删改查
2022/04/22 Golang