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(17)Letter Combinations of a Phone Number

    题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...

  2. Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律

    题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...

  3. jq点击按钮打开和关闭弹出层,点击除了当前按钮以外的地方关闭弹出层

    1.html <a id="more" onclick="moreFun()">更多</a> <ul id="moreL ...

  4. python3配置爬虫开发环境

    爬虫:环境搭建 安装python3: 安装python版本:3.7.0 winsdows下的配置:

  5. 《Let's Build A Simple Interpreter》之 Golang 版

    一直以来对编译器/解释器等都较有兴趣.我非科班出身,当初还在大学时,只是马马虎虎看完了<编译原理>之类教材,上机非常少,对龙书之类圣经也只是浅尝辄止而已.工作至今,基本已将编译原理相关知识 ...

  6. JavaScript学习笔记(第二天)

    数组 为什么要学习数组 之前学习的数据类型,只能存储一个值(比如:Number/String.我们想存储班级中所有学生的姓名,此时该如何存储? 数组的概念 所谓数组,就是将多个元素(通常是同一类型)按 ...

  7. HDU 1756 Cupid's Arrow( 判断点在多边形的内外 )

    链接:传送门 思路:判断每支箭是否在多边形内,计算几何点定位中水题,不清楚下面的代码能不能适用于给定点的顺序不确定( 既不是顺时针又不是逆时针 ) /************************* ...

  8. ElasticSearch启动报错,bootstrap checks failed

    修改elasticsearch.yml配置文件,允许外网访问. vim config/elasticsearch.yml# 增加 network.host: 0.0.0.0 启动失败,检查没有通过,报 ...

  9. http://my.oschina.net/joanfen/blog/160156

    http://my.oschina.net/joanfen/blog/160156 http://code4app.com/ios/iOS7-Sampler/5254b2186803faba0d000 ...

  10. 紫书 例题7-14 UVa 1602(搜索+STL+打表)

    这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...