一、链表的定义

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

二、单链表的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. VUE.JS 窗口发生变化时,获取当前窗口的高度。

    VUE.JS # 窗口发生变化时,获取当前窗口的高度. mounted () { const that = this; window.onresize = () => { return (() ...

  2. JVM总结(一):概述--JVM运行时数据区

    大三下,趁着寒假重温一遍JVM,准备在一个系列来总价一下学习JVM的整个过程.争取在接下来的一个星期内更新完这一个系列,然后回家过年. JVM运行时数据区 线程私有的数据区 程序计数器 虚拟机栈 本地 ...

  3. Kafka 0.8 NIO通信机制

    一.Kafka通信机制的整体结构 同时,这也是SEDA多线程模型. 对于broker来说,客户端连接数量有限,不会频繁新建大量连接.因此一个Acceptor thread线程处理新建连接绰绰有余. K ...

  4. RESTful记录-RESTful内容

    什么是资源? REST架构对待每一个内容都作为一种资源.这些资源可以是文本文件,HTML网页,图片,视频或动态业务数据. REST服务器只是提供资源,REST客户端可访问和修改的资源.这里每个资源由U ...

  5. HDU 1730 类NIM模型

    两者间的间距就是可取石子数,因为对于行内黑白相连的局面该子游戏已经结束了因为此时不管先手再怎么移都是必败,SG=0的终止态 /** @Date : 2017-10-14 21:46:21 * @Fil ...

  6. C#_界面程序_数码游戏

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. Java入门系列(八)多线程

    基本线程类指的是Thread类,Runnable接口,Callable接口 典型多线程问题 生产者-消费者 死锁问题

  8. php拾遗: 类型约束

    突然间什么都不想干,感觉就像来大姨夫一样..但是又不能断了每个工作日都写博客的习惯..所以今天水一下吧. PHP用了快2年了,但是这东西竟然第一次看到,突然间,觉得自己有掉回战五渣的行列了.翻开官方文 ...

  9. [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

    [BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...

  10. 第9月第30天 MVP

    1. import UIKit struct Person { // Model let firstName: String let lastName: String } protocol Greet ...