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标准库与第三方库详解
Jul 22 Python
Python入门篇之编程习惯与特点
Oct 17 Python
Python中的anydbm模版和shelve模版使用指南
Jul 09 Python
Python基于回溯法子集树模板解决找零问题示例
Sep 11 Python
Python学习之Django的管理界面代码示例
Feb 10 Python
python format 格式化输出方法
Jul 16 Python
django 发送邮件和缓存的实现代码
Jul 18 Python
python语音识别实践之百度语音API
Aug 30 Python
python3 实现调用串口功能
Dec 26 Python
python 破解加密zip文件的密码
Apr 22 Python
python opencv通过按键采集图片源码
May 20 Python
python文本处理的方案(结巴分词并去除符号)
May 26 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数字格式化
2006/12/06 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
2015/12/11 PHP
CodeIgniter集成smarty的方法详解
2016/05/26 PHP
使用JavaScript创建新样式表和新样式规则
2016/06/14 PHP
Thinkphp连表查询及数据导出方法示例
2016/10/15 PHP
深入讲解PHP的对象注入(Object Injection)
2017/03/01 PHP
javascript 鼠标拖动图标技术
2010/02/07 Javascript
无闪烁更新网页内容JS实现
2013/12/19 Javascript
常用jQuery选择器总结
2014/07/11 Javascript
js实现图片在未加载完成前显示加载中字样
2014/09/03 Javascript
浅谈jQuery中对象遍历.eq().first().last().slice()方法
2014/11/26 Javascript
Javascript基础教程之定义和调用函数
2015/01/18 Javascript
JavaScript中的时间处理小结
2016/02/24 Javascript
canvas时钟效果
2017/02/16 Javascript
vue 解决addRoutes动态添加路由后刷新失效问题
2018/07/02 Javascript
JS跨域请求的问题解析
2018/12/03 Javascript
小程序多图列表实现性能优化的方法步骤
2019/05/28 Javascript
[03:26]《DAC最前线》之EG经理自述DOTA2经历
2015/02/02 DOTA
[02:13] 完美世界DOTA2联赛PWL DAY5集锦
2020/11/03 DOTA
Python发送以整个文件夹的内容为附件的邮件的教程
2015/05/06 Python
浅析Python数据处理
2018/05/02 Python
Python Json模块中dumps、loads、dump、load函数介绍
2018/05/15 Python
python 画条形图(柱状图)实例
2020/04/24 Python
怎么快速自学python
2020/06/22 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
HTML5中使用postMessage实现两个网页间传递数据
2016/06/22 HTML / CSS
沙龙级头发造型工具:FOXYBAE
2018/07/01 全球购物
程序运行正确, 但退出时却"core dump"了,怎么回事
2014/02/19 面试题
下面关于"联合"的题目的输出是什么
2013/08/06 面试题
给海归自荐信的建议
2013/12/13 职场文书
个人函授自我鉴定
2014/03/25 职场文书
机械工程师岗位职责
2014/06/16 职场文书
中学清明节活动总结
2014/07/04 职场文书
竞选班干部演讲稿300字
2014/08/20 职场文书
大学生学期个人总结
2015/02/12 职场文书
springboot临时文件存储目录配置方式
2021/07/01 Java/Android