一、链表的定义

  用链接关系显式表示元素之间顺序关系的线性表称为链接表或链表。

二、单链表的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基础下的数据结构与算法之链表的更多相关文章

  1. python基础下的数据结构与算法之顺序表

    一.什么是顺序表: 线性表的两种基本的实现模型: 1.将表中元素顺序地存放在一大块连续的存储区里,这样实现的表称为顺序表(或连续表).在这种实现中,元素间的顺序关系由它们的存储顺序自然表示. 2.将表 ...

  2. python 下的数据结构与算法---1:让一切从无关开始

    这段时间把<Data Structure and Algorithms with python>以及<Problem Solving with  Algorithms and Dat ...

  3. python基础语法、数据结构、字符编码、文件处理 练习题

    考试范围 '''1.python入门:编程语言相关概念2.python基础语法:变量.运算符.流程控制3.数据结构:数字.字符串.列表.元组.字典.集合4.字符编码5.文件处理''' 考试内容 1.简 ...

  4. C语言 - 基础数据结构和算法 - 单向链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  5. C语言 - 基础数据结构和算法 - 企业链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  6. Java数据结构和算法(四)--链表

    日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...

  7. python 下的数据结构与算法---8:哈希一下【dict与set的实现】

    少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...

  8. python 下的数据结构与算法---4:线形数据结构,栈,队列,双端队列,列表

    目录: 前言 1:栈 1.1:栈的实现 1.2:栈的应用: 1.2.1:检验数学表达式的括号匹配 1.2.2:将十进制数转化为任意进制 1.2.3:后置表达式的生成及其计算 2:队列 2.1:队列的实 ...

  9. python 下的数据结构与算法---3:python内建数据结构的方法及其时间复杂度

    目录 一:python内部数据类型分类 二:各数据结构 一:python内部数据类型分类 这里有个很重要的东西要先提醒注意一下:原子性数据类型和非原子性数据类型的区别 Python内部数据从某种形式上 ...

随机推荐

  1. NOIP2017 列队——动态开点线段树

    Description: Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为  ...

  2. 本地如何连接虚拟机上的MySql

    今天在本地链接虚拟机上的MySql,然而链接失败了!甚是尴尬! 首先想一想是什么原因导致链接失败: 基础环境:在Linux上安装mysql 1.检查虚拟机IP在本地是否可以ping 通过 虚拟机IP: ...

  3. 利用CSS3实现简书中点击“喜欢”时的动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. SpringMVC关于ajax提交400错误(后台获取为null)

    400错误有三种情况 1:请求的数据量过大,不过这种情况一般很少见. 2:请求的data参数有误,确保每一个参数都能请求到. 注释:之前小白出现400错误,后台获取参数为null是因为第三种情况,经过 ...

  5. 卸载并安装指定版本Angular CLI

    1.卸载之前的版本 npm uninstall -g @angular/cli 2.清除缓存,确保卸载干净 npm cache clean 3.检查是否卸载干净 输入命令 ng -v 若显示comma ...

  6. bzoj千题计划175:bzoj1303: [CQOI2009]中位数图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1303 令c[i]表示前i个数中,比d大的数与比d小的数的差,那么如果c[l]=c[r],则[l+1, ...

  7. python 基础 元组()

    # 元组 应用场景 # 尽管 Python的列表中可以存储不同类型的数据 # 但是在开发中,更多的应用场景是 # 1.列表存储相同类型的数据 # 2.通过迭代遍历,在循环体内部,针对列表中的每一项元素 ...

  8. 【整理】HTML5游戏开发学习笔记(5)- 猜谜游戏

    距上次学习笔记已有一个多月过去了,期间由于新项目赶进度,以致该学习计划给打断,十分惭愧.书本中的第六章的例子相对比较简单.所以很快就完成. 1.预备知识html5中video标签的熟悉 2.实现思路对 ...

  9. react componentWillReceiveProps 使用注意

    componentWillReceiveProps(nextProps) 请务必使用 nextProps  不要使用 this.props 1个小时的教训...

  10. 解读Android LOG机制的实现【转】

    转自:http://www.cnblogs.com/hoys/archive/2011/09/30/2196199.html http://armboard.taobao.com/ Android提供 ...