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. vuex——做简单的购物车功能

    购物车组件 <template> <div> <h1>vuex-shopCart</h1> <div class="shop-listb ...

  2. Jenkins二 安装gitlab及其使用

    git --version 如果没有安装git直接源码安装即可,如果安装了先删除原来的git. yum -y remove git先安装编译git需要的包. yum install zlib-deve ...

  3. Confluence 6 基本性能问题诊断步骤

    基本性能问题诊断步骤 开始下面的程序: 进入 Troubleshooting Confluence hanging or crashing页面找到已知的主要性能问题. 进行页面 Performance ...

  4. json的转换操作

    toJSON 把JS对象{ 'x': 2, 'y': 3 }转为JSON对象格式的字符串   不能转化字符串 比如"{ 'x': 2, 'y': 3 }" 可以转格式不标准的jso ...

  5. mysql老是停止运行该怎么解决

    你可能还会遇到无法启动mysql的错误 解决方法如下:

  6. 自己没有记住的一点小知识(ORM查询相关)

    一.多对多的正反向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名& ...

  7. select下拉框使用完毕后,重置按钮使其清空

    需求描述:select下拉框后边有两个按钮,一个查询,一个重置,点击重置,select会清空之前选择的那个查询条件 解决思路:卧槽,这不so easy 么,用那个jQ封装的trigger函数搞定啊,对 ...

  8. collections模块

    collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.defaultdict.named ...

  9. PDF怎么去除页眉页脚,PDF页眉页脚编辑方法

    我们在使用文件的时候需要编辑页眉页脚的时候,这个时候我们应该怎么做呢,相信别的文件大家都知道怎么编辑了,PDF文件大家都知道吗,最开始接触这个文件的时候小编觉得很难,之后找到技巧之后也并没有很难,今天 ...

  10. Burp Scanner Report

    1.使用application web 漏洞平台,除此之外还有一款类似的工具 叫做mulidata,其实mulidata功能更好一点. 2.配置之前的问题处理 安装之前要确认 自己之前是否安装过 Ap ...