python基础下的数据结构与算法之链表
一、链表的定义
用链接关系显式表示元素之间顺序关系的线性表称为链接表或链表。
二、单链表的python实现
class Node(object):
"""定义节点""" def __init__(self, elem):
self.elem = elem
self.next = None class SingleLinkList(object):
"""单链表操作""" def __init__(self, node=None):
"""初始化"""
self.__head = node def is_empty(self):
"""链表是否为空"""
return self.__head == None def length(self):
"""链表长度"""
# 定义游标,用来移动遍历节点
cur = self.__head
# 定义计数
count = 0
while cur != None:
count += 1
cur = cur.next
return count def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur != None:
print(cur.elem,end=" ")
cur = cur.next
print("") def add(self, item):
"""链表头部添加元素,头插法"""
node = Node(item)
node.next = self.__head
self.__head = node def append1(self, item):
"""链表尾部添加元素,尾插法"""
node = Node(item)
# if self.__head == None:
if self.is_empty():
self.__head = node
return
else:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node def insert(self, pos, item):
"""指定位置添加元素
pos 从0开始
"""
if pos < 0 :
# 输入值小于0,默认为头插法
return self.add(item)
elif pos > self.length()-1:
# 输入值大于列表长度,默认为尾插法
self.append1(item)
else:
node = Node(item)
cur = self.__head
count = 0
while count < (pos-1):
count += 1
cur = cur.next
# 当循环退出时,cur指向pos-1的位置
node.next = cur.next
cur.next = node def remove(self, item):
"""删除节点,只删除第一个找到的数据"""
cur = self.__head
pre = None
while cur != None:
if cur.elem == item:
# 先判断此节点是否是头节点
# if pre == None:
if cur == self.__head:
self.__head = cur.next
# 删除节点
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next
return def search(self, item):
"""查找节点是否存在"""
cur = self.__head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
三、单循环链表的python实现:
class Node(object):
"""定义节点""" def __init__(self, elem):
self.elem = elem
self.next = None class SingleCycleList(object):
"""单向循环链表操作""" def __init__(self, node=None):
"""初始化"""
self.__head = node
if node:
node.next = node def is_empty(self):
"""链表是否为空"""
return self.__head == None def length(self):
"""链表长度"""
# 定义游标,用来移动遍历节点
if self.is_empty():
return 0
cur = self.__head
# 定义计数
count = 1
while cur.next != self.__head:
count += 1
cur = cur.next
return count def travel(self):
"""遍历整个链表"""
if self.is_empty():
return 0
cur = self.__head
while cur.next != self.__head:
print(cur.elem, end=" ")
cur = cur.next
# 退出循环时,cur指向尾节点,但未打印尾节点元素
print(cur.elem) def add(self, item):
"""链表头部添加元素,头插法"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = node
return
cur = self.__head
while cur.next != self.__head:
cur = cur.next
# 退出循环时,cur指向尾节点
node.next = self.__head
self.__head = node
cur.next = node def append1(self, item):
"""链表尾部添加元素,尾插法"""
node = Node(item)
# if self.__head == None:
if self.is_empty():
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
cur.next = node
node.next = self.__head def insert(self, pos, item):
"""指定位置添加元素
pos 从0开始
"""
if pos < 0 :
# 输入值小于0,默认为头插法
return self.add(item)
elif pos > self.length()-1:
# 输入值大于列表长度,默认为尾插法
self.append1(item)
else:
node = Node(item)
cur = self.__head
count = 0
while count < (pos-1):
count += 1
cur = cur.next
# 当循环退出时,cur指向pos-1的位置
node.next = cur.next
cur.next = node def remove(self, item):
"""删除节点,只删除第一个找到的数据"""
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next != self.__head:
if cur.elem == item:
# 先判断此节点是否是头节点
# if pre == None:
if cur == self.__head:
# 头结点的情况,找尾节点
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
# 删除节点
else:
# 中间节点
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 退出循环时,cur指向尾节点
if cur.elem == item:
if cur == self.__head:
# 链表只有一个节点
self.__head = None
else:
pre.next = cur.next def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.elem == item:
return True
else:
cur = cur.next
# 退出循环时,cur指向尾节点
if cur.elem == item:
return True
return False
四、双循环链表的python实现:
class Node(object):
"""定义节点""" def __init__(self, item):
self.elem = item
self.next = None
self.prev = None class DoubleLinkList(object):
"""双链表操作""" def __init__(self, node=None):
"""初始化,构造"""
self.__head = node def is_empty(self):
"""链表是否为空"""
return self.__head == None def length(self):
"""链表长度"""
# 定义游标,用来移动遍历节点
cur = self.__head
# 定义计数
count = 0
while cur != None:
count += 1
cur = cur.next
return count def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur != None:
print(cur.elem, end=" ")
cur = cur.next
print("") def add(self, item):
"""链表头部添加元素,头插法"""
node = Node(item)
node.next = self.__head
self.__head = node
node.next.prev = node def append1(self, item):
"""链表尾部添加元素,尾插法"""
node = Node(item)
# if self.__head == None:
if self.is_empty():
self.__head = node
return
else:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
node.prev = cur def insert(self, pos, item):
"""指定位置添加元素
pos 从0开始
"""
if pos < 0:
# 输入值小于0,默认为头插法
return self.add(item)
elif pos > self.length() - 1:
# 输入值大于列表长度,默认为尾插法
self.append1(item)
else:
node = Node(item)
cur = self.__head
count = 0
while count < pos:
count += 1
cur = cur.next
# 当循环退出时,cur指向pos的位置
node.next = cur
node.prev = cur.prev
cur.prev.next = node
cur.prev = node def remove(self, item):
"""删除节点,只删除第一个找到的数据"""
cur = self.__head
while cur != None:
if cur.elem == item:
# 先判断此节点是否是头节点
# if pre == None:
if cur == self.__head:
self.__head = cur.next
if cur.next:
# 判断链表是否只有一个节点
cur.next.prev = None
else:
cur.prev.next = cur.next
if cur.next:
# 判断是否为尾节点
cur.next.prev = cur.prev
break
else:
cur = cur.next
return def search(self, item):
"""查找节点是否存在"""
cur = self.__head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
python基础下的数据结构与算法之链表的更多相关文章
- python基础下的数据结构与算法之顺序表
一.什么是顺序表: 线性表的两种基本的实现模型: 1.将表中元素顺序地存放在一大块连续的存储区里,这样实现的表称为顺序表(或连续表).在这种实现中,元素间的顺序关系由它们的存储顺序自然表示. 2.将表 ...
- python 下的数据结构与算法---1:让一切从无关开始
这段时间把<Data Structure and Algorithms with python>以及<Problem Solving with Algorithms and Dat ...
- python基础语法、数据结构、字符编码、文件处理 练习题
考试范围 '''1.python入门:编程语言相关概念2.python基础语法:变量.运算符.流程控制3.数据结构:数字.字符串.列表.元组.字典.集合4.字符编码5.文件处理''' 考试内容 1.简 ...
- C语言 - 基础数据结构和算法 - 单向链表
听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...
- C语言 - 基础数据结构和算法 - 企业链表
听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...
- Java数据结构和算法(四)--链表
日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...
- python 下的数据结构与算法---8:哈希一下【dict与set的实现】
少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...
- python 下的数据结构与算法---4:线形数据结构,栈,队列,双端队列,列表
目录: 前言 1:栈 1.1:栈的实现 1.2:栈的应用: 1.2.1:检验数学表达式的括号匹配 1.2.2:将十进制数转化为任意进制 1.2.3:后置表达式的生成及其计算 2:队列 2.1:队列的实 ...
- python 下的数据结构与算法---3:python内建数据结构的方法及其时间复杂度
目录 一:python内部数据类型分类 二:各数据结构 一:python内部数据类型分类 这里有个很重要的东西要先提醒注意一下:原子性数据类型和非原子性数据类型的区别 Python内部数据从某种形式上 ...
随机推荐
- Linux上printf命令的用法
printf格式化输出 基本格式 printf [format] [文本1] [文本2] .. 常用格式替换符 %s 字符串 %f 浮点格式 %c ASCII字符,即显示对应参数的第一个字符 %d,% ...
- SQL优化:索引的重要性
开篇小测验 下面这样一个小SQL 你该怎么样添加最优索引 两个表上现在只有聚集索引 bigproduct 表上已经有聚集索引 ProductID bigtransactionhistory 表上已经有 ...
- 使用tushare的pandas进行to_sql操作时的No module named 'MySQLdb'错误处理
先写在前面,用tushare获取财经类数据时,完全没有必要用python3版本 py2功能没差别,但是py3有很多地方需要修改参数才能成功运行,无端造成时间的浪费 下面进入正题,这个问题困扰了我一个下 ...
- numpy中的arg系列函数
numpy中的arg系列函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 不定期更新,现学现卖 numpy中arg系列函数被经常使用,通常先进行排序然后返回原数组特定的索引. argmax ...
- AttributeError: 'module' object has no attribute 'X509_up_ref'
主要报错: AttributeError: 'module' object has no attribute 'X509_up_ref' 1 解决办法 卸载再重装pyOpenSSL pip unins ...
- Java中多个异常的捕获顺序(多个catch)
import java.io.IOException; public class ExceptionTryCatchTest { public void doSomething() throws IO ...
- bzoj千题计划257:bzoj4199: [Noi2015]品酒大会
http://www.lydsy.com/JudgeOnline/problem.php?id=4199 求出后缀数组的height 从大到小枚举,合并 维护组内 元素个数,最大.次大.最小.次小 # ...
- javascript设计模式开篇:Javascript 接口的实现
javascript语言不像java. c#. c++等面向对象语言那样有完备的接口支持,在javascript中,接口的实现有三种方式,分别为注释描述.属性检查.鸭式变形.注释描述实现起来最为简单, ...
- Chrome插件LiveStyle结合Sublime Text3编辑器实现高效可视化开发
LiveStyle是Chrome中提高开发效率的一款CSS编辑器插件.利用LiveStyle和Sublime Text3编辑器结合可实现可视化开发,一次配置,简单易用! 本文由前端交流QQ群管理员—— ...
- JavaScript继承详解(四)
在本章中,我们将分析Douglas Crockford关于JavaScript继承的一个实现 - Classical Inheritance in JavaScript. Crockford是Java ...