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实现链表的更多相关文章

  1. Python3玩转单链表——逆转单向链表pythonic版

    [本文出自天外归云的博客园] 链表是由节点构成的,一个指针代表一个方向,如果一个构成链表的节点都只包含一个指针,那么这个链表就是单向链表. 单向链表中的节点不光有代表方向的指针变量,也有值变量.所以我 ...

  2. 反转链表(python3)

    问题描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL解法1: ...

  3. python3从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 方法一:通过栈实现 # -*- coding:utf-8 -*- # class ListNode: # def __ini ...

  4. LeetCode 206.反转链表(Python3)

    题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...

  5. LeetCode 237. 删除链表中的节点(Python3)

    题目: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head ...

  6. 链表题目汇总(python3)

    1.从头到尾打印链表 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. # -*- coding:utf-8 -*- class ListNode: def __init__(self ...

  7. LeetCode Reverse Linked List (反置链表)

    题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...

  8. s14 第4天 关于python3.0编码 函数式编程 装饰器 列表生成式 生成器 内置方法

    python3 编码默认为unicode,unicode和utf-8都是默认支持中文的. 如果要python3的编码改为utf-8,则或者在一开始就声明全局使用utf-8 #_*_coding:utf ...

  9. python3操作redis

    redis也被称为缓存 1.redis是一个key-value存储系统,没有ForeignKey和ManyToMany的字段. 2.在redis中创建的数据彼此之间是没有关系的,所以也被称为是非关系型 ...

随机推荐

  1. 【原创】大叔经验分享(38)beeline连接hiveserver2报错impersonate

    beeline连接hiveserver2报错 Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost: ...

  2. 使用SimHash进行海量文本去重[转]

    阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...

  3. for循环中按条件删除数据元素

    var managerList = [ { id: 0, title: '小小消息的标题1', small: '小小消息内容', newsFlag:true, }, { id: 1, title: ' ...

  4. nginx 配置白名单

    在http 模块 增加 geo $remote_addr $ip_whitelist{ default 0; include white_ip.conf; } 在location 模块 增加 (注意i ...

  5. swift 学习- 11 -- 属性

    // '属性'将值跟特定的类, 结构体或枚举关联, 存储属性常量或变量作为实例的一部分,而计算属性计算(不是存储) 一个值, 计算属性可以用于 类, 结构体, 枚举, 存储属性只能用于 类 和 结构体 ...

  6. 【MySql】join操作

    飞机票 飞机票 加油 INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIGHT JOIN( ...

  7. maven install 报错 No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    1.控制台打印信息 [INFO] Scanning for projects... [INFO] [INFO] ---------------------< org.cqupt.mauger:R ...

  8. Confluence 6 自定义站点和空间布局

    你可以通过编辑布局文件来修改 Confluence 的外观和感觉(也可以被称为装饰).编辑这些文件将会允许你对整个 Confluence 站点的外观和感觉进行修改或者仅仅是一个独立的空间. 当你对一个 ...

  9. Confluence 6 基于 Confluence 数据中心的 SAML 单点登录

    安全申明标记语言(Security Assertion Markup Language (SAML))是一个基于 XML 的数据格式,允许各个软件平台通过identity provider (IdP) ...

  10. django-admin的源码流程

    一.admin的源码流程 首先可以确定的是:路由关系一定对应一个视图函数 a.当点击运行的时候,会先找到每一个app中的admin.py文件,并执行 b.执行urls.py admin.site是什么 ...