链表<新>
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))
链表<新>的更多相关文章
- c++ 链表删除重复的数据
//List.h #include <iostream> typedef int dataType; struct Node{ Node():data(),pNextNode(NULL){ ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- linux内存源码分析 - 内存回收(lru链表)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 概述 对于整个内存回收来说,lru链表是关键中的关键,实际上整个内存回收,做的事情就是处理lru链表的收缩,所以 ...
- PAT 1025 反转链表
PAT (Basic Level) Practise 1025 Github链接:https://github.com/H-BING/object-oriented/tree/master/PAT10 ...
- (转)linux内存源码分析 - 内存回收(lru链表)
原文:http://www.cnblogs.com/tolimit/p/5447448.html 概述 对于整个内存回收来说,lru链表是关键中的关键,实际上整个内存回收,做的事情就是处理lru链表的 ...
- OptimalSolution(3)--链表问题(1)简单
单链表Node节点类 public class Node { public int val; public Node next; public Node(int val) { this.val = v ...
- java 8 jdk1.8 新特性
1Lambda表达式 2函数式接口 函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. java 8为函数式接口引入了一个新注解@Fu ...
- 【数据结构和算法】001 单链表 LinkedList
一.单链表(LinkedList)介绍和内存布局 链表是有序的列表,它在内存中的实际存储结构如下: 看上去虽然无序,但他是靠灭个链表节点元素的地址和next域来分清首尾相连的顺序,如下图所示,由头指针 ...
- 详细分析链表的数据结构的实现过程(Java 实现)
目录 链表的数据结构的实现过程(Java 实现) 前言 基本概念 链表的基本结构 链表的基本操作的实现 在链表中添加元素 在链表头添加元素 在链表指定位置处添加元素 链表的虚拟头节点 链表的查询和修改 ...
随机推荐
- stm8s103调试注意点
外设时钟的配置,有次ADC就是不工作,查问题查了很久,总是怀疑ADC配置问题,然后利用库函数的例程,发现就可以,最后发现,外设时钟没开启,外设时钟如下配置 CLK->PCKENR1 = 0x00 ...
- ZBrush通过显示与隐藏得到子物体
在ZBrush®中得到子物体的方法有很多,本文将为大家介绍一种新的创建子物体的方法,通过显示和隐藏得到子物. ZBrush 4R8中文版下载:http://wm.makeding.com/iclk/? ...
- 5G商用时代来临!这些产业将发生变革
5G商用时代来临!这些产业将发生变革 值得注意的是,在获得工信部发放的5G网络试验频率后,三大运营商已在各大城市建设5G基站,开展5G外场测试.华为亦适时表示,已经在中国40多个城市与中国三大运营商开 ...
- css——外部样式
外部样式 先建立一个css文件,如下: 然后开始写代码,不要加<style> 然后可以在html文件中的<head>内引用:<link rel="stylesh ...
- GRUB 引导流程
GRUB(bootloader)引导流程: GRUB,GRand Unified Bootlader ,是一个来自GUN项目的多操作系统启动程序.GRUB是多启动规范的实现,它允许用户可以在计算机内 ...
- python在不同情况下写入csv文件
情况一(解法一):将列表存储为csv文件.列表的每一项代表csv文件的一行. 列表中的每一项包含多个属性.list=[[属性1,属性2,属性3,……],[属性1,属性2,属性3,……],[属性1,属性 ...
- sqrt开平方算法的尝试,是的看了卡马克大叔的代码,我来试试用C#写个0x5f3759df和0x5f375a86跟System.Math.Sqrt到底哪个更强
今天笔试遇到一个代码题,要求写一个开平方算法,回来发现了雷神之锤里的一段神代码: float Q_rsqrt( float number ) { long i; float x2, y; const ...
- [luogu] P4155 [SCOI2015]国旗计划(贪心)
P4155 [SCOI2015]国旗计划 题目描述 A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此 ...
- JS中的DOM操作怎样添加、移除、移动、复制、创建和查找节点
DOM操作怎样添加.移除.移动.复制.创建和查找节点? (1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元 ...
- 前端和后台对接时对sign加密方法
前端和后台对接时对sign加密方法 /*后台php对接进行sign标签加密 1 获取向后台请求的数据data(key/value方式),可以是个对象(obj),也可以是数组(arr); 2 将数据的k ...