链表<新>
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 实现) 前言 基本概念 链表的基本结构 链表的基本操作的实现 在链表中添加元素 在链表头添加元素 在链表指定位置处添加元素 链表的虚拟头节点 链表的查询和修改 ...
随机推荐
- LeetCode(15)3Sum
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...
- Java开发就业形势和面试技巧
如果从软件编程的就业来讲,如果你现在不懂架构,那么找到一份好工作还是比较难的,但是这里面有两点需要注意: 传统软件公司,这类公司还会使用最为原始的开发技术(SSH),但是这样的传统软件公司的招聘量已经 ...
- Java校验8位字符串是否为正确的日期格式
import java.text.ParseException; import java.text.SimpleDateFormat; /** * 校验8位字符串是否为正确的日期格式 * @autho ...
- C语言基本语法——变量
1.变量作用域 2.局部变量 3.全局变量 4.变量生命周期 5.auto关键字 6.static关键字 1.变量作用域 • 变量作用域是指变量的有效范围 • 变量作用域是定义变量从何处被创建,到何处 ...
- linux查看前几条命令记录
1.按上下箭头键2.history|more分页显示3.vi /etc/profile找HISTSIZE=1000,说明你最多能存1000条历史记录.4.!!执行最近执行的命令5.history|he ...
- webpack配置相关的页面异常
原文:https://www.cnblogs.com/Hsong/p/9023341.html 前言 在团队协作开发中,为了统一代码风格,避免一些低级错误,应该设有团队成员统一遵守的编码规范.很多语言 ...
- 【codeforces 810B】Summer sell-off
[题目链接]:http://codeforces.com/contest/810/problem/B [题意] 每天有ki件物品,你知道每天能卖掉li件; 然后让你选f天; 这f天,可以将ki乘上2; ...
- angular-HTML DOM
ng-disabled用法 <div ng-app="" ng-init="mySwitch=true"> <p> <button ...
- COGS——T 2342. [SCOI2007]kshort || BZOJ——T 1073
http://www.cogs.pro/cogs/problem/problem.php?pid=2342 ★★☆ 输入文件:bzoj_1073.in 输出文件:bzoj_1073.out ...
- 玩转oracle学习第六天
1.上节回想 2.PL/SQL的介绍 3.PL/SQL的基础 理解oracle的pl/sql概念 掌握PL/SQL编程技术(包含编写过程,函数,触发器.包... ) PL/SQL是什么? PL/ ...