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 相关文章推荐
django自定义Field实现一个字段存储以逗号分隔的字符串
Apr 27 Python
python利用MethodType绑定方法到类示例代码
Aug 27 Python
Python使用win32 COM实现Excel的写入与保存功能示例
May 03 Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
Jul 05 Python
Pycharm使用之设置代码字体大小和颜色主题的教程
Jul 12 Python
如何使用django的MTV开发模式返回一个网页
Jul 22 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
python3反转字符串的3种方法(小结)
Nov 07 Python
python实现扫雷游戏的示例
Oct 20 Python
python实现猜拳游戏项目
Nov 30 Python
python 爬虫网页登陆的简单实现
Nov 30 Python
python中os.path.join()函数实例用法
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实现下载限制速度示例分享
2014/02/13 PHP
浅析PHP关键词替换的类(避免重复替换,保留与还原原始链接)
2015/09/22 PHP
PHP数组游标实现对数组的各种操作详解
2016/01/26 PHP
php微信公众号开发之答题连闯三关
2018/10/20 PHP
关于js获取radio和select的属性并控制的代码
2011/05/12 Javascript
基于jquery的使ListNav兼容中文首字拼音排序的实现代码
2011/07/10 Javascript
jquery ajax实现下拉框三级无刷新联动,且保存保持选中值状态
2013/10/29 Javascript
jQuery实现的支持IE的html滑动条
2015/03/16 Javascript
深入浅析NodeJs并发异步的回调处理
2015/12/21 NodeJs
Bootstrap实现带动画过渡的弹出框
2016/08/09 Javascript
微信小程序之picker日期和时间选择器
2017/02/09 Javascript
解决nodejs中使用http请求返回值为html时乱码的问题
2017/02/18 NodeJs
详解angularjs利用ui-route异步加载组件
2017/05/21 Javascript
Angular中使用MathJax遇到的一些问题
2017/12/15 Javascript
linux 下以二进制的方式安装 nodejs
2020/02/12 NodeJs
详解Vue2的diff算法
2021/01/06 Vue.js
[03:08]迎霜节狂欢!2018年迎霜节珍藏Ⅰ一览
2018/12/25 DOTA
新手该如何学python怎么学好python?
2008/10/07 Python
Cpy和Python的效率对比
2015/03/20 Python
python制作花瓣网美女图片爬虫
2015/10/28 Python
python reverse反转部分数组的实例
2018/12/13 Python
numpy.random模块用法总结
2019/05/27 Python
python实现飞机大战游戏(pygame版)
2020/10/26 Python
python读取与处理netcdf数据方式
2020/02/14 Python
使用Python操作MySQL的小技巧
2020/09/10 Python
Html5游戏开发之乒乓Ping Pong游戏示例(一)
2013/01/21 HTML / CSS
基于HTML5+CSS3实现简单的时钟效果
2017/09/11 HTML / CSS
车间副主任岗位职责
2013/12/24 职场文书
企事业单位求职者的自我评价
2013/12/28 职场文书
四个太阳教学反思
2014/02/01 职场文书
户外宣传策划方案
2014/05/25 职场文书
计算机软件专业求职信
2014/06/10 职场文书
装修活动策划方案
2014/08/27 职场文书
党的群众路线剖析材料
2014/10/09 职场文书
旷课检讨书范文
2014/10/30 职场文书
Mysql如何查看是否使用到索引
2022/12/24 MySQL