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中创建的数据彼此之间是没有关系的,所以也被称为是非关系型 ...
随机推荐
- 洛谷P4117 [Ynoi2018]五彩斑斓的世界 [分块,并查集]
洛谷 Codeforces 又是一道卡常题-- 思路 YNOI当然要分块啦. 分块之后怎么办? 零散块暴力,整块怎么办? 显然不能暴力改/查询所有的.考虑把相同值的用并查集连在一起,这样修改时就只需要 ...
- vue.js的学习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【翻译】关于vertical-align所有你需要知道的
本文是翻译过来的,如果有不对的地方还请指教~,原文链接:Vertical-Align: All You Need To Know 前面一些说明,可以略过不看吧 我经常需要对元素进行垂直方向上的布局. ...
- OSError: cannot identify image file
OSError: cannot identify image file <_io.BytesIO object at 0x00000236DD598BF8> 说一下为什么会出现OSErro ...
- hdu2121 最小树形图的虚根
/* 最小树形图的第二题,终于有了一些理解 具体看注释 */ /* 无定根的最小树形图 建立虚root 每次只找最短的那条入边 最小树形图理解: 第一步:寻找最短弧集E:扫一遍所有的边,找到每个点权值 ...
- 雅礼 noip2018 模拟赛day3 T2
典型的状压思想 设0表示黑球,1表示白球,用一串01序列代表剩下的球的状态,记f[i]表示在i状态下取球的最大期望 那么可以利用记忆化搜索更新,每一层枚举可能拿走的球然后向下搜索,同时记忆化即可 在状 ...
- 阿里云服务器配置SSL证书成功开启Https(记录趟过的各种坑)
环境: 阿里云云服务器 Windows Server 2008 标准版 SP2 中文版(趁1212优惠买的一年的水货配置) 阿里云购买的域名(已备案.已解析) 服务器:phpstudy:php5 ...
- python WebDriver如何处理右键菜单
WebDriver如何处理右键菜单 一.背景 在学习selenium webdriver的过程中,遇到这样一个问题.ActionChains类中提供了context_click的方法,它可以用来在we ...
- lisp : set 与setq 函数
在Lisp中,如果我们希望对一个变量赋值,可以使用set函数,用法如下: (set ‘my-value "my string") 上面的代码是对变量my-value进行赋值,值是& ...
- Idea和PyCharm激活破解
1. 先去百度去官网下载专业版IDE, Idea 和PyCharm激活方法一样 2. 下载破解包, 点击下载 3. 将下载的jar包放到这个安装目录的bin目录下面 4. 在bin目录下面的文件pyc ...