class Node:
'''
节点类
链表节点结构 data next
data: 节点保存的数据
_next: 保存下一个节点对象
''' def __init__(self, data, pnext=None):
self.data = data
self._next = pnext def __repr__(self) -> str:
return str(self.data) 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的原因是不知道会有多少个
while _node._next:
# 得到后继为空的,也就是最后一个
_node = _node._next
# 将新的节点赋值给最后一个的_next属性
_node._next = item
self._length += 1 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_head = self._head
item._next = old_head
self._head = item
self._length += 1 else:
# 得到第一个node
_node = self._head
for i in range(index - 1):
# 得到插入位置的前驱
_node = _node._next
# 得到本来在指定位置的node
old_index_node = _node._next
# 给插入的node设置前驱
_node._next = item
# 给插入的元素设置后继
item._next = old_index_node
self._length += 1
return True 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=None):
""" :type int
:param index:
:return:
"""
# 先判断链表是否是空的
if self.isEmpty():
print("当前链表为空")
return False
if index is None:
# 不指定index的时候,直接弹出最后一个
index = self._length-1
if not isinstance(index, int):
raise TypeError("index应该为int类型")
if index < 0 or index >= self._length:
print("输入的索引不正确")
return False
# 获取head指向的node
first_node = self._head
_node = first_node
for i in range(index-1):
# 得到第index-1个位置的node
_node = _node._next if index==0:
self._head=first_node._next
self._length-=1
return first_node
else:
select_node = _node._next
_node._next= select_node._next
self._length-=1
return select_node # 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"""
# # 得到第0个node
# _node = self._head
# # 将head指向第二个node
# self._head = _node._next
# self._length -= 1
# return _node
# elif index == self._length - 1:
# """弹出最后一个node"""
# # 先找到head指向的node,即第0个node
# _node = self._head
# # 如果不是最后一个节点则一直往下找
#
# for i in range(index - 1):
# # 拿到倒数第二个node
# _node = _node._next
# end_node = _node._next
# # 给倒数第二个node设置新的后继,None
# _node._next = None
# self._length -= 1
# return end_node
# else:
# # 中间的node
# _node = self._head
# for j in range(index - 1):
# # 得到指定index的前驱
# _node = _node._next
# # 得到应得的node
# selected_node = _node._next
# # 将弹出的node的前驱的后继设置成要弹出的node的后继
# _node._next = selected_node._next
# self._length -= 1
# return selected_node def getNode(self, index):
"""
根据index得到节点
:type int
:param index: 索引
:type: Node
: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
# 得到第0个node
_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 clear(self):
self.head = None
self._length = 0
return True def printl(self):
for i in range(self._length):
print(self.getNode(i))

链表<新>的更多相关文章

  1. c++ 链表删除重复的数据

    //List.h #include <iostream> typedef int dataType; struct Node{ Node():data(),pNextNode(NULL){ ...

  2. 链表的基本操作(Basic Operations on a Linked List)

    链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...

  3. linux内存源码分析 - 内存回收(lru链表)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 概述 对于整个内存回收来说,lru链表是关键中的关键,实际上整个内存回收,做的事情就是处理lru链表的收缩,所以 ...

  4. PAT 1025 反转链表

    PAT (Basic Level) Practise 1025 Github链接:https://github.com/H-BING/object-oriented/tree/master/PAT10 ...

  5. (转)linux内存源码分析 - 内存回收(lru链表)

    原文:http://www.cnblogs.com/tolimit/p/5447448.html 概述 对于整个内存回收来说,lru链表是关键中的关键,实际上整个内存回收,做的事情就是处理lru链表的 ...

  6. OptimalSolution(3)--链表问题(1)简单

    单链表Node节点类 public class Node { public int val; public Node next; public Node(int val) { this.val = v ...

  7. java 8 jdk1.8 新特性

    1Lambda表达式 2函数式接口 函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. java 8为函数式接口引入了一个新注解@Fu ...

  8. 【数据结构和算法】001 单链表 LinkedList

    一.单链表(LinkedList)介绍和内存布局 链表是有序的列表,它在内存中的实际存储结构如下: 看上去虽然无序,但他是靠灭个链表节点元素的地址和next域来分清首尾相连的顺序,如下图所示,由头指针 ...

  9. 详细分析链表的数据结构的实现过程(Java 实现)

    目录 链表的数据结构的实现过程(Java 实现) 前言 基本概念 链表的基本结构 链表的基本操作的实现 在链表中添加元素 在链表头添加元素 在链表指定位置处添加元素 链表的虚拟头节点 链表的查询和修改 ...

随机推荐

  1. LeetCode(11)Container With Most Water

    题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...

  2. 04《UML大战需求分析》之四

    在学习完顺序图之后,流程分析的三种图,我已经学习完了我,但是我还需要大量地锻炼,这样才可以更加熟练地掌握几种图的使用和方法.接下来,我学习了用例图,用来描述系统的行为. 虽然是一同学习的,但是对用例图 ...

  3. Prime Distance POJ - 2689 线性筛

    一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...

  4. CF859C Pie Rules 动态规划 逆推_思维题

    题意:有 nnn 个物品,每个物品有不同的价值,物品按顺序分给两个人,有一块令牌,每回合拥有令牌的人拥有物品的分配权,但是该回合未获得物品的那个人会在下回合获得令牌,开始令牌在Bob手里,两个人都采取 ...

  5. C# 基础复习 四 多线程

    单线程和多线程的区别     单线程:         只用主线程处理,如果一个操作在占用主线程,那么其他操作则无法执行     多线程:         除了主线程外,还开启了子线程来执行操作,子线 ...

  6. css——外部样式

    外部样式 先建立一个css文件,如下: 然后开始写代码,不要加<style> 然后可以在html文件中的<head>内引用:<link rel="stylesh ...

  7. Ubuntu_18.04安装网易云音乐

    首先到网易云音乐官网下载网易云音乐,ubuntu版的,安装. 这时候的图标打不开,缺少libcanberra sudo apt install libcanberra-gtk-module 安装完了配 ...

  8. Tensorflow学习笔记----模型的保存和读取(4)

    一.模型的保存:tf.train.Saver类中的save TensorFlow提供了一个一个API来保存和还原一个模型,即tf.train.Saver类.以下代码为保存TensorFlow计算图的方 ...

  9. Java基础学习总结(58)——JAVA堆、栈详解

    关于堆栈的内容网上已经有很多资料了,这是我找的加上自己理解的一篇说明文: 一.内存区域类型 1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制: 1. 栈:存放基本类型的变量数 ...

  10. 将maven中央仓库不存在的jar包添加到本地仓库

    这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库. 要使用的 jar 不存在于 Maven 的中心储存库中. 您创建了一个自定义的 jar ,而另一个 Mave ...