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实现远程调用MetaSploit的方法
Aug 22 Python
Python中input与raw_input 之间的比较
Aug 20 Python
python3 发送任意文件邮件的实例
Jan 23 Python
Python3.5内置模块之time与datetime模块用法实例分析
Apr 27 Python
Django数据库类库MySQLdb使用详解
Apr 28 Python
python绘制地震散点图
Jun 18 Python
python跳出双层for循环的解决方法
Jun 24 Python
pandas取出重复数据的方法
Jul 04 Python
详解python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决
Aug 27 Python
python3实现raspberry pi(树莓派)4驱小车控制程序
Feb 12 Python
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
Apr 21 Python
python 日志模块logging的使用场景及示例
Jan 04 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
全国FM电台频率大全 - 14 江西省
2020/03/11 无线电
PHP获取url的函数代码
2011/08/02 PHP
php截取字符串函数分享
2015/02/02 PHP
JAVASCRIPT对象及属性
2007/02/13 Javascript
IE浏览器PNG图片透明效果代码
2008/09/02 Javascript
十个优秀的Ajax/Javascript实例网站收集
2010/03/31 Javascript
jquery操作select option 的代码小结
2011/06/21 Javascript
深入理解Javascript作用域与变量提升
2013/12/09 Javascript
快速解决jQuery与其他库冲突的方法介绍
2014/01/02 Javascript
jQuery动态创建html元素的常用方法汇总
2014/09/05 Javascript
关于获取DIV内部内容报错的原因分析及解决办法
2016/01/29 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
2016/04/17 Javascript
浅析JS获取url中的参数实例代码
2016/06/14 Javascript
Vue computed计算属性的使用方法
2017/07/14 Javascript
jQuery获取复选框选中的当前行的某个字段的值
2017/09/15 jQuery
JS实现websocket长轮询实时消息提示的效果
2017/10/10 Javascript
vue-cli脚手架引入弹出层layer插件的几种方法
2019/06/24 Javascript
javascript 内存模型实例详解
2020/04/18 Javascript
在Python的Django框架中编写编译函数
2015/07/20 Python
Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)
2017/08/23 Python
Python使用Flask-SQLAlchemy连接数据库操作示例
2018/08/31 Python
关于python多重赋值的小问题
2019/04/17 Python
python-django中的APPEND_SLASH实现方法
2019/06/21 Python
pytorch 输出中间层特征的实例
2019/08/17 Python
详解Python 重学requests发起请求的基本方式
2020/02/07 Python
Python tkinter实现简单加法计算器代码实例
2020/05/13 Python
在线学习西班牙语、法语或其他语言:Babbel.com
2018/02/07 全球购物
Sneaker Studio捷克:购买运动鞋
2018/07/08 全球购物
预备党员转正思想汇报
2014/01/12 职场文书
2015年客服工作总结范文
2015/04/02 职场文书
2015年中秋放假通知范文
2015/08/18 职场文书
如何写好一份优秀的工作总结?
2019/06/21 职场文书
vue如何批量引入组件、注册和使用详解
2021/05/12 Vue.js
经典《舰娘》游改全新动画预告 预定11月开播
2022/04/01 日漫
Python 使用 Frame tkraise() 方法在 Tkinter 应用程序中的Frame之间切换
2022/04/24 Python
html原生table实现合并单元格以及合并表头的示例代码
2023/05/07 HTML / CSS