链表<新>
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 实现) 前言 基本概念 链表的基本结构 链表的基本操作的实现 在链表中添加元素 在链表头添加元素 在链表指定位置处添加元素 链表的虚拟头节点 链表的查询和修改 ...
随机推荐
- 去除input 在 webkit内核浏览器 选择历史时,有一个黄色背景
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; } http://www.imooc.com/arti ...
- 别让好想法埋没:如何进行APP开发?
经常和许多创业者讨论,发现很多人想法都不错,但是都不知道该如何实现,今天就如何引导大家走出第一步写一篇文章,很多人通常误会一件事情,认为程序员开发可以一步步把自己的想法拼接出来,我有一个功能点,就让程 ...
- RocketMQ学习笔记(11)----RocketMQ的PushConsumer和PullConsumer
1. PushConsumer 推,Broker主动向Consumer推消息,它Consumer的一种,应用通常向对象注册一个Listener接口,一旦接收到消息,Consumer对象立刻回调Lins ...
- 面试题sql
查询书的价格10到20 之前显示10to20 没有显示unknown CREATE TABLE book(price INT,NAME VARCHAR(20)) SELECT NAME AS '名字' ...
- zabbix-server端监控MySQL服务
Zabbix 监控MySQL数据库 为server.zabbix,com 添加服务模块 创建MySQL服务图形 Server.zabbix.com 服务器操作 [root@server ~]# cd ...
- org.xml.sax.SAXParseException: Failed to read schema document 的原因分析与解决方法
现象: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema documen t 'http://www.s ...
- Nginx 实现反向代理
Nginx 实现反向代理 两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容 两个域名分别是 www.test1.com www.test2.com 1.准备工作 下载及安装 ...
- ubuntu设置PATH
试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用sudo su 进入到root用户权限,设置完成的. 重新使用sudo -s进入root用户权限 ...
- WinServer-服务器管理器-从入门到放弃
WIN7 远程服务器管理工具 看看这篇帖子,他们说可以在WIN7上通过服务器管理工具来管理服务器上的软件 https://social.technet.microsoft.com/Forums/zh- ...
- ASP.NET-HTTP响应标头
Reponse Headers 理论上所有的响应头信息都应该是回应请求头的.但是服务端为了效率,安全,还有其他方面的考虑,会添加相对应的响应头信息,从上图可以看到: Cache-Control:mus ...