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内部数据从某种形式上 ...
随机推荐
- (转)在Eclipse中用TODO标签管理任务(Task)
背景:eclipse是一款功能十分强大的编辑,如果能够熟练运用,必定事半功倍,但如果不求甚解,无疑是给自己制造麻烦. 1 标签的使用 1.1 起因 如上图所示,在程序中有很多todo的标签出现,但是却 ...
- Hadoop生态圈-Kafka常用命令总结
Hadoop生态圈-Kafka常用命令总结 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.管理Kafka服务的命令 1>.开启kafka服务 [yinzhengjie@s ...
- C语言复习---用筛选法求100之内的素数
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int i, j; ] ...
- bzoj千题计划189:bzoj1867: [Noi1999]钉子和小球
http://www.lydsy.com/JudgeOnline/problem.php?id=1867 dp[i][j] 落到(i,j)的方案数 dp[i][j]=0.5*dp[i-1][j] ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams
http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...
- Spring RedisTemplate操作-事务操作(9)
@Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...
- Webx示例-PetStore分析1
1. 下载源码 2. 启动容器,加载组件--WebxContextLoaderListener WebxContextLoaderListener继承自org.springframework.web. ...
- 内置函数bytes()
a=b'\x00\x9c@c' print a[3]#99,c的ascii码是99 print a[1]#156 并且byte是无法修改的 c[1]=155 Traceback (most recen ...
- python3转变exe的方法
python开发的代码可能在其他windows上并不能使用用,除非别人的环境中也有python. 下面是如何将python开发的东西转为exe格式 1.安装pyinstaller pip instal ...
- MTD应用学习札记【转】
转自:https://blog.csdn.net/lh2016rocky/article/details/70885421 今天做升级方案用到了mtd-utils中的flash_eraseall和fl ...