class Node(object):
"""结点""" def __init__(self, data):
self.data = data
self.pre = None
self.next = None class DoubleLinkList(object):
"""双链表""" def __init__(self, node=None):
self.__head = node def is_empty(self):
"""判断链表是否为空"""
return self.__head is None def length(self):
"""链表的长度"""
count = 0
cur = self.__head
while cur is not None:
count += 1
cur = cur.next
return count def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur is not None:
print(cur.data, end="")
cur = cur.next
print() def add(self, item):
"""头插"""
node = Node(item)
# 空链表头插
if self.is_empty():
self.__head = node
# 非空链表头插
else:
node.next = self.__head
self.__head.pre = node
self.__head = node def append(self, item):
"""尾插"""
node = Node(item)
# 空链表尾插
if self.is_empty():
self.__head = node
# 非空链表尾插
else:
cur = self.__head
# 找到最后一个结点
while cur.next is not None:
cur = cur.next
cur.next = node
node.pre = cur def insert(self, pos, item):
"""指定位置插入"""
if pos <= 0:
self.add(item)
elif pos > self.length() - 1:
self.append(item)
else:
node = Node(item)
cur = self.__head
count = 0
# 找到指定位置的前一个结点
while count < pos - 1:
cur = cur.next
count += 1
node.next = cur.next
node.pre = cur
cur.next = node
node.next.pre = node def remove(self, item):
"""删除指定结点"""
cur = self.__head
while cur is not None:
# 找到要删除的结点
if cur.data == item:
# 如果要删除的结点是头结点
if cur == self.__head:
self.__head = cur.next
# 判断链表是否只有一个结点
if cur.next:
cur.next.pre = None
# 要删除的结点不是头结点
else:
cur.pre.next = cur.next
# 判断要删除的结点是否是尾结点
if cur.next:
cur.next.pre = cur.pre
return
else:
cur = cur.next def search(self, item):
"""查找结点是否存在"""
cur = self.__head
while cur is not None:
if cur.data == item:
return True
else:
cur = cur.next
return False if __name__ == '__main__':
sll = DoubleLinkList()
print(sll.is_empty())
print(sll.length())
sll.add(1)
sll.remove(1)
print(sll.is_empty())
print(sll.length())
sll.append(2)
sll.append(3)
sll.append(4)
sll.append(5)
sll.travel() # 2345
print(sll.length()) # 4
sll.add(99) # 99 2345
sll.travel()
print(sll.length()) # 5
print(sll.search(6)) # False
print(sll.search(4)) # True
sll.remove(5)
sll.travel() # 99 234

python---双链表的常用操作的更多相关文章

  1. 超详细!盘点Python中字符串的常用操作

    在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...

  2. Python 基礎 - 字符串常用操作

    字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...

  3. python基础之列表常用操作及知识点小结

    列表(list) List(列表) 是 Python 中使用最频繁的数据类型.列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(所谓嵌套).列表用[ ]标识,是pyth ...

  4. python数据类型:字典dict常用操作

    字典是Python语言中的映射类型,他是以{}括起来,里面的内容是以键值对的形式储存的: Key: 不可变(可哈希)的数据类型.并且键是唯一的,不重复的. Value:任意数据(int,str,boo ...

  5. Python列表类型及常用操作

    Python列表类型 1.用途: 存放多个值,可以根据索引存取值 2.定义方式: 在[ ]内用逗号分割开多个任意类型的值 l=['yven','law','lyf'] #l=list(['yven', ...

  6. 『无为则无心』Python序列 — 18、Python列表概念及常用操作API

    目录 1.列表的概念 (1)列表的定义 (2)列表的应用场景 (3)列表的定义格式 2.列表的常用操作 (1)列表的查找 1)通过下标查找 2)通过方法查找 3)判断是否存在 (2)列表的增加 @1. ...

  7. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  8. Python基础灬文件常用操作

    文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...

  9. python字符串、元组常用操作

    常用字符串操作函数: #Author:CGQ name="I \tam ChenGuoQiang" print(name.capitalize())#首字母大写,其他都小写 pri ...

随机推荐

  1. 手把手教你写一个SpringMVC框架

    一.介绍 在日常的 web 开发中,熟悉 java 的同学一定知道,Spring MVC 可以说是目前最流行的框架,之所以如此的流行,原因很简单:编程简洁.上手简单! 我记得刚开始入行的时候,最先接触 ...

  2. 写博客的技巧整理——基于Markdown

    我们需要掌握各种技巧,这样才能在写博客时游刃有余,以下内容觉得不错就点个赞吧 文章目录 1.目录与目录跳转 目录一(示例用勿点) 目录二(示例用勿点) 目录三(示例用勿点) 2.文字与图片 3.引用 ...

  3. test 分支强制替换master 分支的办法

    test分支改动太多,并且master 分支好久没有改动.直接合并到master 分支的话,会产生很多冲突,几十个文件,修复冲突会花很多时间,并且是没有意义的.因此只能使用test 分支强制替换. 代 ...

  4. 理解 MVCC

    MongoDB.MySQL.Oracle.PostgreSQL 等事务型数据库都有 mvcc 的概念. MVCC: 即多版本并发控制,主要是为了提高数据库的读写性能,让数据库在读写的时候不用去加锁.m ...

  5. BadImageFormatException异常

    访问页面时,抛出BadImageFormatException异常: 1.如果您的应用程序使用了 32 位组件,请确保该应用程序始终采用 32 位应用程序的运行方式. 如果应用程序项目的"平 ...

  6. mybatis连接sql

    mysql6以上  com.mysql.cj.jdbc.Driver

  7. python爬取网络中的QQ号码

    import urllib.request import ssl import re import os #博客地址:https://blog.csdn.net/qq_36374896 def wri ...

  8. Python GUI tkinter 学习笔记(三)

    草稿 # -*- coding: utf-8 -*- from Tkinter import * root = Tk() Label(root, text = "First").g ...

  9. Linux系统常用的命令

    1.查看本机IP地址:ifconfig 2.查看当前所在路径:pwd 3.查看指定名称线程:ps -ef | grep tomcat 4.查看当前目录结构:ll 或者 ls 5.杀死指定线程:kill ...

  10. 羽夏看Win系统内核——调试篇

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...