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导出数据到Excel可读取的CSV文件的方法
May 12 Python
Python中的hypot()方法使用简介
May 18 Python
python实现支持目录FTP上传下载文件的方法
Jun 03 Python
Python的string模块中的Template类字符串模板用法
Jun 27 Python
Python进度条实时显示处理进度的示例代码
Jan 30 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
Feb 25 Python
python实现最速下降法
Mar 24 Python
实例代码讲解Python 线程池
Aug 24 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
Nov 12 Python
Python图像识别+KNN求解数独的实现
Nov 13 Python
Python3中FuzzyWuzzy库实例用法
Nov 18 Python
python爬虫beautifulsoup解析html方法
Dec 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
thinkphp验证码显示不出来的解决方法
2014/03/29 PHP
大家须知简单的php性能优化注意点
2016/01/04 PHP
PHP二维数组去重实例分析
2016/11/18 PHP
PHP获取对象属性的三种方法实例分析
2019/01/03 PHP
使用Javascript和DOM Interfaces来处理HTML
2006/10/09 Javascript
xheditor与validate插件冲突的解决方案
2010/04/15 Javascript
跨浏览器的事件对象介绍
2012/06/27 Javascript
Javascript Request获取请求参数如何实现
2012/11/28 Javascript
HTML页面滚动时获取离页面顶部的距离2种实现方法
2013/09/05 Javascript
js给页面加style无效果的解决方法
2014/01/20 Javascript
如何在MVC应用程序中使用Jquery
2014/11/17 Javascript
js时钟翻牌效果实现代码分享
2020/07/31 Javascript
angularjs学习笔记之完整的项目结构
2015/09/26 Javascript
安装使用Mongoose配合Node.js操作MongoDB的基础教程
2016/03/01 Javascript
jQuery过滤特殊字符及JS字符串转为数字
2016/05/26 Javascript
jQuery源码解读之extend()与工具方法、实例方法详解
2017/03/30 jQuery
VUE axios上传图片到七牛的实例代码
2017/07/28 Javascript
js 获取json数组里面数组的长度实例
2017/10/31 Javascript
vue 2.5.1 源码学习 之Vue.extend 和 data的合并策略
2019/06/04 Javascript
webpack3.0升级4.0的方法步骤
2020/04/02 Javascript
python中异常捕获方法详解
2017/03/03 Python
对numpy中数组元素的统一赋值实例
2018/04/04 Python
python3使用SMTP发送简单文本邮件
2018/06/19 Python
Python使用pickle模块实现序列化功能示例
2018/07/13 Python
Python3.4学习笔记之列表、数组操作示例
2019/03/01 Python
Python加密模块的hashlib,hmac模块使用解析
2020/01/02 Python
python opencv圆、椭圆与任意多边形的绘制实例详解
2020/02/06 Python
Python判断字符串是否为空和null方法实例
2020/04/26 Python
keras 简单 lstm实例(基于one-hot编码)
2020/07/02 Python
详解CSS3中border-image的使用
2015/07/18 HTML / CSS
大队干部竞选演讲稿
2014/04/28 职场文书
离职报告格式
2014/11/04 职场文书
兼职安全员岗位职责
2015/02/15 职场文书
高中生社会实践心得体会
2016/01/14 职场文书
写一个Python脚本下载哔哩哔哩舞蹈区的所有视频
2021/05/31 Python
部分武汉产收音机展览
2022/04/07 无线电