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. 前端-----JavaScript 初识基础

    JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:操作网页上的 ...

  2. 查看ubuntu版本信息

    参考文章:https://blog.csdn.net/qq_27818541/article/details/75207986 版本信息lsb_release -a No LSB modules ar ...

  3. Python-数据类型 主键auto_increment

    MySQL数据操作: DML========================================================在MySQL管理软件中,可以通过SQL语句中的DML语言来实 ...

  4. 24)django-信号

    目录 1)django信号简介 2)django内置信号 3)django自定义信号 一:django信号简介 Django中提供了“信号调度”,用于在框架执行操作时解耦. 通俗来讲,就是一些动作发生 ...

  5. 【进阶1-4期】JavaScript深入之带你走进内存机制(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记 https://mp.weixin.qq.com/s/yK4DPKhkmkiroasWJMrJcw 阅读笔记 JS内存空间分为栈(stack).堆 ...

  6. chrome调试工具怎么限制网速

    在做项目的时候,我们测试的时候有时需要限制网速

  7. swift 实践- 03 -- UILabel

    class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 标签 let ...

  8. C# TTS 文字和英文

    using System;using System.Globalization;using System.Linq;using System.Speech.Synthesis;using System ...

  9. 第十八单元 nginx服务

    安装python 源 wget   网上下载 原码 编译    安装的流程 (gcc glic) 版本更新时起冲突,删除原版本(或者reinstall重新安装) 可执行文件运行   ./ 软连接 查看 ...

  10. DSB

    Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...