class Node:
'''
节点类
链表节点结构 data next
data: 节点保存的数据
_next: 保存下一个节点对象
''' def __init__(self, data, pnext=None):
self.data = data
self._next = pnext
#
# def __str__(self) -> str:
# """
# 输出节点的信息
# :return: self.data
# """
# return str(self.data) def __repr__(self) -> str:
return str(self.data) # def _getindex(self):
# return Link_list() class Link_list:
"""
链表类:
属性: 1.链表头head 2.链表长度
方法: 1.是否为空 isEmpty 2.增加 append 3.删除节点 delete 4.修改(更新)节点 update
5.查找节点 getNode 6.获取节点的索引 getIndex 7.插入 insert 8.清空链表clear
""" def __init__(self) -> None:
"""
初始化链表,head信息为空,长度为0
"""
self._head = None
self._length = 0 def isEmpty(self):
"""
判断链表是否为空
:return:
"""
return self._length == 0 def append(self, item):
""" :param item: Node 或者 node的data信息
:return: None
"""
if not isinstance(item,Node):
item = Node(data=item)
if not self._head:
# head为Node对象
# head ---> data + nextNode
self._head = item
self._length += 1
else:
# 取到当前的Node对象
_node = self._head
# 如果不是最后一个节点则一直往下找
while _node._next:
_node = _node._next
# 将新的节点赋值给最后一个的_next属性
_node._next = item
self._length += 1 def delete(self,index):
"""
根据索引删除节点
:param index: 索引
:return: bool
"""
if not isinstance(index,int):
raise TypeError("index应该为int类型")
if self.isEmpty():
print("当前链表为空")
return False
if index<0 or index>=self._length:
print("输入的索引不正确")
return False
elif index==0:
self._head=self._head._next
self._length-=1
return True
elif index==self._length-1:
_node = self._head
# 如果不是最后一个节点则一直往下找
for i in range(index):
_node = _node._next
_node._next=None
self._length-=1
return True
else:
_node = self._head
for j in range(index-1):
_node = _node._next
_node._next=_node._next._next
self._length-=1
return True def pop(self,index):
"""
根据索引删除节点,并返回
:param index: 索引
:return: bool
"""
if not isinstance(index,int):
raise TypeError("index应该为int类型")
if self.isEmpty():
print("当前链表为空")
return False
if index<0 or index>=self._length:
print("输入的索引不正确")
return False
elif index==0:
_node = self._head
self._head=self._head._next
self._length-=1
return _node
elif index==self._length-1:
_node = self._head
# 如果不是最后一个节点则一直往下找
for i in range(index):
_node = _node._next
__node = _node._next
_node._next=None
self._length-=1
return __node
else:
_node = self._head
for j in range(index-1):
_node = _node._next
__node = _node._next
_node._next=_node._next._next
self._length-=1
return __node def getNode(self,index):
"""
根据index得到节点
:param index: 索引
:return: Node对象
""" if not isinstance(index, int):
raise TypeError("index应该为int类型")
if self.isEmpty():
print("当前链表为空")
return False
if index < 0 or index >= self._length:
print("输入的索引不正确")
return False
_node = self._head
for i in range(index):
_node = _node._next
return _node def update(self,index,data):
"""
更新节点
:param index: 索引
:param data: 节点信息
:return: 返回修改后的节点
"""
if not isinstance(index, int):
raise TypeError("index应该为int类型")
if self.isEmpty():
print("当前链表为空")
return False
if index < 0 or index >= self._length:
print("输入的索引不正确")
return False
_node = self._head
for i in range(index):
_node = _node._next
_node.data = data
return _node def getIndex(self,node):
"""
根据节点得到节点索引
:param node:节点
:return:index
"""
if isinstance(node, Node):
for i in range(self._length):
if node is self.getNode(i):
return i
print("node异常")
return
else:
raise TypeError("类型不正确") def insert(self,index,item):
if not isinstance(item, Node):
item = Node(data=item)
if not isinstance(index,int):
raise TypeError("index应该为int类型")
if index<0 or index>=self._length:
print("输入的索引不正确")
return False
if index==0:
old_next = self._head
item._next= old_next
self._head=item
self._length+=1 else:
_node = self._head
for i in range(index-1):
_node = _node._next
old_next = _node._next
_node._next = item
item._next = old_next
self._length += 1
return True def clear(self):
self.head = None
self.length = 0
return True def printl(self):
for i in range(self._length):
print(self.getNode(i))

Python链表的更多相关文章

  1. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  2. Python链表操作(实现)

    Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...

  3. python 链表表达式 map、filter易读版

    链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...

  4. Python链表与反链表

    # -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...

  5. python链表的实现

    根据Problem Solving with Algorithms and Data Structures using Python 一书用python实现链表 书籍在线网址http://intera ...

  6. python 链表

    在C/C++中,通常采用“指针+结构体”来实现链表:而在Python中,则可以采用“引用+类”来实现链表. 节点类: class Node: def __init__(self, data): sel ...

  7. python链表的实现,有注释

    class Node():                   #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个  __slots__=['_item' ...

  8. python 链表的反转

    code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self. ...

  9. python 链表、堆、栈

    简介 很多开发在开发中并没有过多的关注数据结构,当然我也是,因此,我写这篇文章就是想要带大家了解一下这些分别是什么东西. 链表 概念:数据随机存储,并且通过指针表示数据之间的逻辑关系的存储结构. 链表 ...

  10. Add Two Numbers(from leetcode python 链表)

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

随机推荐

  1. Caffe Batch Normalization推导

    Caffe BatchNormalization 推导 总所周知,BatchNormalization通过对数据分布进行归一化处理,从而使得网络的训练能够快速并简单,在一定程度上还能防止网络的过拟合, ...

  2. 常用的Linux命令汇总

    1. 进入某个文件夹 2.查找某个文件或内容 3.查看文件内容 4.kill进程 启动tomcat  停止tomcat 1. 进入某个文件夹 比如有个目录,路径是:   /home/user1/doc ...

  3. 迁移IPv6:6To4隧道技术

    1. IPv6 路由选择协议 首先要讨论的是RIPng(下一代).RIP非常适合用于小型网络.这正是它没有惨遭淘汰,继续用于 IPV6网络的原因.另外,还有EIGRPv6,因为它有独立于协议的模块,只 ...

  4. luogu P1354 房间最短路问题 计算几何_Floyd_线段交

    第一次写计算几何,还是很开心的吧(虽然题目好水qaq) 暴力枚举端点,暴力连边即可 用线段交判一下是否可行. Code: #include <cstdio> #include <al ...

  5. MongoDB入门 常用命令以及增删改查的简单操作

    1,运行MongoDB服务mongod --dbpath=/usr/local/developmentTool/mongo/data/db/然后启动客户端mongo2,sudo service mon ...

  6. Python学习笔记(4)列表

    2019-02-26 列表(list):①创建方法:用‘[ ]’,将数据包括起来,数据之间用逗号隔开.②空列表:empty = []③增删改查: 1)增加: a.append()方法——将元素添加到列 ...

  7. python hashlib、configparse、logging

    一.hashlib 1.Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等.     2.摘要算法 通过摘要函数f()对任意长度的数据data计算出固定长度的摘要digest,目 ...

  8. (转)Epoll模型详解

    1. 内核中提高I/O性能的新方法epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.要使用epoll只需要这三个系统调 用:epoll_create(2),  ...

  9. javascript-js中技巧集合

    1.值的转换 在JavaScript中,一共有两种类型的值:原始值(primitives)和对象值(objects).原始值有:undefined, null, 布尔值(booleans), 数字(n ...

  10. android自己定义Application全局变量不能类型转换的问题

    今天弄了个全局变量AppContext ,但一直出现例如以下错误,原来继承 Application的得在清单文件声明. java.lang.RuntimeException: Unable to st ...