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内部数据从某种形式上 ...
随机推荐
- tomcat 性能调优
1. 内存 windows在bin/catalina.bat的注释下第一行加入 set JAVA_OPTS=-Xms2048m -Xmx2048m -Xss128K -XX:PermSize=64m ...
- c++并发编程之thread::join()和thread::detach()
thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...
- MySQL删除所有表的外键约束、禁用外键约束
转: MySQL删除所有表的外键约束.禁用外键约束 2017年10月27日 00:11:34 李阿飞 阅读数:4512 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- PowerDesigner 打印错误
PowerDesigner打开pdm文件时报“打印错误”(解决) 原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://b ...
- create-react-app脚手架使用
1.安装脚手架和路由 npm i -g create-react-app npm i -S react-router react-router-dom 2.创建新项目 create-react-app ...
- spring cloud 微服务架构 简介
Spring Cloud 1. Spring Cloud 简介 Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集,为开发人员提供快速建立分布式系统中的 ...
- [六字真言]6.吽.SpringMVC中上传大小异常填坑
最近在讲课的时候,遇到了关于上传文件过大的时候浏览器无法响应的问题,配置了捕获异常,有的学生浏览器好使,有的学生浏览器不好用!莫名其妙! MaxUploadSizeExceededException进 ...
- Java SSM框架之MyBatis3(四)MyBatis之一对一、一对多、多对多
项目搭建Springboot 1.5 pom.xml <?xml version="1.0" encoding="UTF-8"?> <pro ...
- 一些javascript的工具书
http://pan.baidu.com/s/1jGj9CvO
- Linux - awk 文本处理工具六 - 日志关键字筛选
查看多少行 ? awk '{print NR}' access.log |tail -n1 日期时间筛选检测 awk '/Dec 10/ {print $0}' /opt/mongod/log/mon ...