python---自己实现双向链表常用功能
这个和单向链表有几个功能是同样的代码。
但在add,insert,append,remove时,由于node拥有prev指针,
所以操作不一样。注意看注释。
# coding = utf-8
# 双向链表
class Node:
# 双向链表存在两个指标,一个向前,一个向后
def __init__(self, new_data):
self.data = new_data
self.prev = None
self.next = None
def get_data(self):
return self.data
def set_data(self, new_data):
self.data = new_data
def get_next(self):
return self.next
def set_next(self, new_next):
self.next = new_next
def get_prev(self):
return self.prev
def set_prev(self, new_prev):
self.prev = new_prev
class DoubleList:
def __init__(self):
self.head = None
# 作头插入时,需要要注意插入顺序,
# 总之。要注意不要丢失next和prev。
def add(self, item):
node = Node(item)
if not self.is_empty():
# 待插入节点的后继区指向原头节点
node.set_next(self.head)
# 原头节点的前驱区指向待插入节点
self.head.set_prev(node)
# 将head指向node
self.head = node
# 作尾插入时,需要先判断是否为空列表。因为空列表时,没有next。
# 为空列表时,尾插入和头插入代码相同。
# 当不为空时,需要循环到底,再作插入处理
def append(self, item):
node = Node(item)
if self.is_empty():
# 将head指向node
self.head = node
else:
# 移动到链表尾部
current = self.head
while current.get_next() is not None:
current = current.get_next()
# 将尾节点cur的next指向node
current.set_next(node)
# 将node的prev指向cur
node.set_prev(current)
# 指定位置插入节点
def insert(self, pos, item):
# 相当于头插入
if pos <= 0:
self.add(item)
# 相当于尾插入
elif pos >= self.size():
self.append(item)
else:
node = Node(item)
count = 0
current = self.head
# 先将游标指到要插入位置
while count < pos - 1:
count += 1
current = current.get_next()
# 将node的prev指向cur
node.set_prev(current)
# 将node的next指向cur的下一个节点
node.set_next(current.get_next())
# 将cur的下一个节点的prev指向node
current.get_next().set_prev(node)
# 将cur的next指向node
current.set_next(node)
# 删除指定节点数据
def remove(self, item):
# 删除时,不需要再使用一个prev变量了。因为双向链表本身有这个指针
current = self.head
while current is not None:
if current.get_data() == item:
# 在找到节点之后,需要判断是否为首节点
if current == self.head:
self.head = current.get_next()
# 判断链表是否只有一个结点
if current.get_next():
current.get_next().set_prev(None)
else:
current.get_prev().set_next(current.get_next())
# 如果存在下一个结点,则设置下一个结点
if current.get_next():
current.get_next().set_prev(current.get_prev())
break
else:
current = current.get_next()
# 查找指定数据是否存在
def search(self, item):
current = self.head
found = False
while current is not None:
if current.get_data() == item:
found = True
current = current.get_next()
return found
def is_empty(self):
return self.head is None
def __len__(self):
return self.size()
def size(self):
count = 0
current = self.head
while current is not None:
count += 1
current = current.get_next()
return count
def show(self):
current = self.head
while current is not None:
print(current.get_data(), end=' ')
current = current.get_next()
print('\n')
if __name__ == '__main__':
d_list = DoubleList()
print(d_list.is_empty())
d_list.add(5)
d_list.add(4)
d_list.add(76)
d_list.add(23)
d_list.show()
d_list.append(48)
d_list.show()
d_list.append(234)
d_list.show()
d_list.insert(2, 100)
d_list.show()
d_list.remove(5)
d_list.show()
print(d_list.search(48))
C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py True True Process finished with exit code
python---自己实现双向链表常用功能的更多相关文章
- Python 基本数据类型_常用功能整理
一.字符串 字符串 s ="axle" #去两端空格 s.split() #以什么开头 s.startswith("a") #查找子序列,"12&qu ...
- 【python】【logging】python日志模块logging常用功能
logging模块:应用程序的灵活事件日志系统,可以打印并自定义日志内容 logging.getLogger 创建一个log对象 >>> log1=logging.getLogger ...
- python中time模块常用功能
import time time模块提供了大量对时间进行处理的方法 time.time() # 获取当前时间戳,得到自1970年开始的秒数 >>>time.time() 155487 ...
- python基本数据类型及常用功能
1.数字类型 int -int(将字符串转换为数字) a = " print(type(a),a) b = int(a) print(type(b),b) num = " v = ...
- Python常用功能函数
Python常用功能函数汇总 1.按行写字符串到文件中 import sys, os, time, json def saveContext(filename,*name): format = '^' ...
- python轻量级orm框架 peewee常用功能速查
peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...
- Python常用功能函数总结系列
Python常用功能函数系列总结(一) 常用函数一:获取指定文件夹内所有文件 常用函数二:文件合并 常用函数三:将文件按时间划分 常用函数四:数据去重 Python常用功能函数系列总结(二) 常用函数 ...
- Python OS模块常用功能 中文图文详解
一.Python OS模块介绍 OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> i ...
- python爬虫:一些常用的爬虫技巧
python爬虫:一些常用的爬虫技巧 1.基本抓取网页 get方法: post方法: 2.使用代理IP 在开发爬虫过程中经常会遇到IP被封掉的情况,这时就需要用到代理IP; 在urllib2包中有Pr ...
随机推荐
- Python中的 一些常用技巧函数[.join()]
1.str.join(item)字符串操作函数,参数item可以是字符串.元组.字典,示例 ','.join('abc') [','.join('abc')] 输出: 'a,b,c'['a', 'b' ...
- nginx(二)nginx的安装
下载 nginx官网下载地址 把源码解压缩之后,在终端里运行如下命令: ./configure make make install 默认情况下,Nginx 会被安装在 /usr/local/nginx ...
- Raspberry pi connect temperature and humidity to onenet (移动云平台)
工具 树莓派3 modelB 一个 dht11温湿度传感器一个 onenet平台 安装好requests库的python(一定要安装好不然代码不能正确运行,可以参考我的另一篇博文点击打开链接) 树莓 ...
- pymysql模块
一.pymysql模块 1.说明: 想在python代码中连接上mysql数据库,就需要使用pymysql模块, pymysql是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,在 ...
- 使用with语句优化pymysql的操作
一.with语句的好处 with语句的好处在于,它可以自动帮我们释放上下文,就比如文件句柄的操作,如果你不使用with语句操作,你要先open一个文件句柄,使用完毕后要close这个文件句柄,而使用w ...
- Jupyter Notebook(推荐使用Anaconda安装)
一.Jupyter Notebook介绍 1.简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文档编写.运行代码和展示结果. 简而言之,Jupy ...
- HTML div 盒子 添加/删除——浮层
1.clear语法:clear : none | left|right| both 2.clear参数值说明:none : 允许两边都可以有浮动对象both : 不允许有浮动对象left : 不允许左 ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 使用Spring Boot Actuator将指标导出到InfluxDB和Prometheus
使用Spring Boot Actuator将指标导出到InfluxDB和Prometheus Spring Boot Actuator是Spring Boot 2发布后修改最多的项目之一.它经过 ...
- Unity 动画系统
Legacy动画系统:Animation组件(旧) Mecanim动画系统:Animator组件(新) 动画播放过程: //动画片段 [System.Serializable] public clas ...