python3实现链表
1、链表的实现
a、链表的结构为:

b、链表的实现方法;
#链表结构实现 私有属性_pro_item是指向下个节点的指针,_item为此节点的值
class ChainDemo(): def __init__(self,item = None,pos_item=None): self._item = item
self._pos_item = pos_item if __name__ == '__main__':
chain = ChainDemo('A',(ChainDemo('B',ChainDemo('C',ChainDemo('D')))))
while True:
print(chain._item)
if chain._pos_item != None:
chain = chain._pos_item
else:
break
2、实现对链表的操作(增删)
#链表节点结构实现 私有属性_pro_item是指向下个节点的指针,_item为此节点的值
class Node(): def __init__(self,item = None,pos_item=None): self._item = item
self._next = pos_item def __repr__(self):
'''
用来定义Node的字符输出,
print为输出item
'''
return str(self._item) #单链表实现
class Chain(): def __init__(self):
self._head = None
self.length = 0 #判空
def isEmpty(self):
return self.length == 0 #链表结尾插入
def append(self,item): if isinstance(item,Node):
node = item
else:
node = Node(item) if self._head == None:
self._head = node
else:
be_node = self._head
while be_node._next:
be_node = be_node._next
be_node._next = node
self.length += 1 #插入数据
def insert(self,index,item): if self.isEmpty():
print('this chain table is empty')
return if index<0 or index >= self.length:
print("error: out of index")
return in_node = Node(item)
node = self._head
count = 1 while True:
node = node._next
count += 1
if count == index: next_node = node._next
node._next = in_node
in_node._next = next_node
self.length += 1
return # node = s #删除数据
def delete(self,index): if self.isEmpty():
print('this chain table is empty')
return if index<0 or index >= self.length:
print("error: out of index")
return
# if index == 0
# self._head = None
else:
node = self._head
count = 0
while True:
count += 1
if index == count:
node._next = node._next._next
break
node = node._next self.length -= 1 def __repr__(self):
if self.isEmpty():
print("the chain table is empty")
return
nlist = ""
node = self._head
while node:
nlist += node._item +''
node = node._next
return nlist if __name__ == '__main__':
chain = Chain()
chain.append('A')
chain.append('B')
chain.append('C')
chain.append('D')
chain.append('E')
chain.append('F')
chain.append('G')
chain.insert(4,'p')
chain.delete(3)
print(chain,chain._head._item,chain.length)
python3实现链表的更多相关文章
- Python3玩转单链表——逆转单向链表pythonic版
[本文出自天外归云的博客园] 链表是由节点构成的,一个指针代表一个方向,如果一个构成链表的节点都只包含一个指针,那么这个链表就是单向链表. 单向链表中的节点不光有代表方向的指针变量,也有值变量.所以我 ...
- 反转链表(python3)
问题描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL解法1: ...
- python3从尾到头打印链表
题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 方法一:通过栈实现 # -*- coding:utf-8 -*- # class ListNode: # def __ini ...
- LeetCode 206.反转链表(Python3)
题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...
- LeetCode 237. 删除链表中的节点(Python3)
题目: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head ...
- 链表题目汇总(python3)
1.从头到尾打印链表 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. # -*- coding:utf-8 -*- class ListNode: def __init__(self ...
- LeetCode Reverse Linked List (反置链表)
题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...
- s14 第4天 关于python3.0编码 函数式编程 装饰器 列表生成式 生成器 内置方法
python3 编码默认为unicode,unicode和utf-8都是默认支持中文的. 如果要python3的编码改为utf-8,则或者在一开始就声明全局使用utf-8 #_*_coding:utf ...
- python3操作redis
redis也被称为缓存 1.redis是一个key-value存储系统,没有ForeignKey和ManyToMany的字段. 2.在redis中创建的数据彼此之间是没有关系的,所以也被称为是非关系型 ...
随机推荐
- 让NotePad++添加到右键快捷方式
添加后的效果图: 操作方式: 第一步:在桌面上新建一个txt文本文档,然后将写入如下内容 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT ...
- 前端 ---jQuery的补充
15-jQuery补充 jquery内容补充 jquery除了咱们上面讲解的常用知识点之外,还有jquery 插件.jqueryUI知识点 jqueryUI 官网: https://jqueryu ...
- winform中textbox提示框
在winform中向textbox输入内容时下面有提示信息,效果如图所示: private void Form1_Load(object sender, EventArgs e) { Auto ...
- Ubuntu16.04安装JDK8与Tomcat7
本篇教程在示例步骤中使用了以下版本的软件.操作时,请您以实际软件版本为准. 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) ...
- Confluence 6 为登录失败配置使用验证码
如果你具有 Confluence 管理员的权限,你可以限制 Confluence 登录失败的最大尝试次数.在给予最大登录失败尝试(默认为 3 次)次数后,Confluence 将会在用户进行再次尝试的 ...
- Linux端BaiduPCS-Go使用方法
下载https://pan.baidu.com/s/1RFHTRE1c_JlP8rrZiERsTg 运行 ./BaiduPCS-Go 可能更新:update 登录:login 下载: d xxx 更多 ...
- Java编程之前的复习和练习
日期:2018.7.14 星期六 博客期:001 今天先是试着写一下博客,最近去青海旅游了,学习时间有点少,但空余时间还是有学习的,不管怎么样吧!先说一下我的这几天的成果——“Bignum”类,虽然很 ...
- yum安装软件内容
linux yum源改为阿里yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...
- Python全栈习题一
1.执行 Python 脚本的两种方式 a../run.py 直接在命令行调用python脚本: b.python run.py 调用python解释器来调用Python脚本. 2.简述位.字节的关 ...
- ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: YES
一.有时可以直接输入命令: mysql进入数据库 启动数据库:# mysqld_safe & 二.查看用户命令: mysql> use mysql; Reading table info ...