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传递中文参数的问题
Aug 04 Python
怎样使用Python脚本日志功能
Aug 14 Python
matplotlib绘制符合论文要求的图片实例(必看篇)
Jun 02 Python
如何在Django中设置定时任务的方法示例
Jan 18 Python
详解【python】str与json类型转换
Apr 29 Python
Python实现多态、协议和鸭子类型的代码详解
May 05 Python
python爬取本站电子书信息并入库的实现代码
Jan 20 Python
Python利用Xpath选择器爬取京东网商品信息
Jun 01 Python
Python如何进行时间处理
Aug 06 Python
python中编写函数并调用的知识点总结
Jan 13 Python
python实现进度条的多种实现
Apr 29 Python
Python爬虫入门案例之回车桌面壁纸网美女图片采集
Oct 16 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实现12306火车票余票查询和价格查询(12306火车票查询)
2014/01/14 PHP
php判断是否为json格式的方法
2014/03/04 PHP
JS异常处理try..catch语句的作用和实例
2014/05/05 PHP
php+highchats生成动态统计图
2014/05/21 PHP
thinkPHP的表达式查询用法详解
2016/09/14 PHP
PHP7.1方括号数组符号多值复制及指定键值赋值用法分析
2016/09/26 PHP
jquery特效 幻灯片效果示例代码
2013/07/16 Javascript
javascript中怎么做对象的类型判断
2013/11/11 Javascript
跟我学习javascript的执行上下文
2015/11/18 Javascript
vue中使用axios post上传头像/图片并实时显示到页面的方法
2018/09/27 Javascript
详解vue-element Tree树形控件填坑路
2019/03/26 Javascript
Angular 2使用路由自定义弹出组件toast操作示例
2019/05/10 Javascript
Vue 页面权限控制和登陆验证功能的实例代码
2019/06/20 Javascript
利用d3.js制作连线动画图与编辑器的方法实例
2019/09/05 Javascript
vue简单练习 桌面时钟的实现代码实例
2019/09/19 Javascript
[03:08]Ti4观战指南上
2014/07/07 DOTA
跟老齐学Python之字典,你还记得吗?
2014/09/20 Python
Python列表解析配合if else的方法
2018/06/23 Python
Django框架实现的简单分页功能示例
2018/12/04 Python
详解用Python实现自动化监控远程服务器
2019/05/18 Python
python图片二值化提高识别率代码实例
2019/08/24 Python
Python 存取npy格式数据实例
2020/07/01 Python
Python实现PS滤镜中的USM锐化效果
2020/12/04 Python
CSS3实现div从下往上滑入滑出效果示例
2020/04/28 HTML / CSS
canvas 橡皮筋式线条绘图应用方法
2019/02/13 HTML / CSS
沃达丰英国有限公司:Vodafone英国
2019/04/16 全球购物
全球领先的中国制造商品在线批发平台:DHgate
2020/01/28 全球购物
Booking.com缤客中国:全球酒店在线预订网站
2020/05/03 全球购物
工程师岗位职责
2013/11/08 职场文书
技术总监的工作职责
2013/11/13 职场文书
《路旁的橡树》教学反思
2014/04/07 职场文书
学校群众路线专项整治方案
2014/10/31 职场文书
小学五一劳动节活动总结
2015/02/09 职场文书
初中生思想道德自我评价
2015/03/09 职场文书
傅雷家书读书笔记
2015/06/29 职场文书
一年级下册数学教学反思
2016/02/16 职场文书