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中的闭包详细介绍和实例
Nov 21 Python
python文件与目录操作实例详解
Feb 22 Python
Python中内置的日志模块logging用法详解
Jul 12 Python
Python3调用微信企业号API发送文本消息代码示例
Nov 10 Python
django Serializer序列化使用方法详解
Oct 16 Python
python使用 __init__初始化操作简单示例
Sep 26 Python
Python散点图与折线图绘制过程解析
Nov 30 Python
简单介绍一下pyinstaller打包以及安全性的实现
Jun 02 Python
基于Python快速处理PDF表格数据
Jun 03 Python
使用pycharm和pylint检查python代码规范操作
Jun 09 Python
如何基于Python爬虫爬取美团酒店信息
Nov 03 Python
Python pandas求方差和标准差的方法实例
Aug 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
PHP+DBM的同学录程序(5)
2006/10/09 PHP
PHP4在Windows2000下的安装
2006/10/09 PHP
smtp邮件发送一例
2006/10/09 PHP
PHP中用hash实现的数组
2011/07/17 PHP
Thinkphp模板中截取字符串函数简介
2014/06/17 PHP
Smarty日期时间操作方法示例
2016/11/15 PHP
laravel框架中间件 except 和 only 的用法示例
2019/07/12 PHP
Yii2.0框架behaviors方法使用实例分析
2019/09/30 PHP
js tab 选项卡
2009/04/26 Javascript
详解js闭包
2014/09/02 Javascript
js跨域问题浅析及解决方法优缺点对比
2014/11/08 Javascript
JavaScript实现同步于本地时间的动态时间显示方法
2015/02/02 Javascript
jquery.validate提示错误信息位置方法
2016/01/22 Javascript
js转html实体的方法
2016/09/27 Javascript
Bootstrap选项卡学习笔记分享
2017/02/13 Javascript
Koa项目搭建过程详细记录
2018/04/12 Javascript
基于vue循环列表时点击跳转页面的方法
2018/08/31 Javascript
node中使用es6/7/8(支持性与性能)
2019/03/28 Javascript
jQuery事件绑定和解绑、事件冒泡与阻止事件冒泡及弹出应用示例
2019/05/13 jQuery
javascript实现简单搜索功能
2020/03/26 Javascript
使用Python编写爬虫的基本模块及框架使用指南
2016/01/20 Python
Python Selenium 之关闭窗口close与quit的方法
2019/02/13 Python
pytorch 准备、训练和测试自己的图片数据的方法
2020/01/10 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
2020/05/19 Python
Python 在局部变量域中执行代码
2020/08/07 Python
pandas参数设置的实用小技巧
2020/08/23 Python
python 发送邮件的示例代码(Python2/3都可以直接使用)
2020/12/03 Python
HTML5 Canvas中绘制矩形实例
2015/01/01 HTML / CSS
自我鉴定思想方面
2013/10/07 职场文书
努力学习演讲稿
2014/05/10 职场文书
档案保密承诺书
2014/06/03 职场文书
城管执法人员个人对照检查材料思想汇报
2014/09/29 职场文书
公安领导班子四风问题个人整改措施思想汇报
2014/10/09 职场文书
岁月神偷观后感
2015/06/11 职场文书
企业愿景口号
2015/12/25 职场文书
java高级用法JNA强大的Memory和Pointer
2022/04/19 Java/Android