#!/usr/bin/env python3
# -*- coding:utf-8 -*- class LNode:
"""
结点类
""" def __init__(self, elem, next_=None):
self.elem = elem
self.next = next_ class LinkedListUnderflow(ValueError):
"""
自定义异常
"""
pass class LList:
"""
链表类
""" def __init__(self):
self._head = None def is_empty(self):
return self._head is None # 操作pop删除表头结点并返回这个结点里的数据
def pop(self):
if self._head is None:
raise LinkedListUnderflow("in pop")
e = self._head.elem
self._head = self._head.next
return e # 在表头插入元素
def prepend(self, elem):
self._head = LNode(elem, self._head) # 在链表最后插入元素
def append(self, elem):
if self._head is None:
self._head = LNode(elem)
return None
# 如果链表为空则直接把表头指向需要插入的元素即可在。实际上是在操作_head域 p = self._head
while p.next is not None:
p = p.next
p.next = LNode(elem)
# 如果不为空,则先从头扫描链表,找到最后的结点,然后把最后结点的next指向需要插入的元素即可。实际上是在操作next域 # 删除最后一个结点
def pop_last(self):
if self._head is None: # 空表
raise LinkedListUnderflow("in pop_last")
p = self._head
if p.next is None: # 如果表长为1,则清空之,并返回原来的元素
e = p.elem
self._head = None
return e
while p.next.next is not None:
p = p.next
e = p.next.elem
p.next = None
return e
# 如果表长大于1,则从头扫描,找到倒数第二个结点,把倒数第二个结点的next域置空,并返回最后一个结点 def printall(self): # 扫描打印链表的每个元素
p = self._head
while p is not None:
print(p.elem, end='')
if p.next is not None:
print(',', end='')
p = p.next def elements(self): # 写一个生成器,使链表支持for操作
p = self._head
while p is not None:
yield p.elem
p = p.next class LList1(LList): # 派生一个变形单链表类
def __init__(self):
LList.__init__(self)
self._rear = None def prepend(self, elem): # 重构prepend方法
if self._rear is None:
self._head = LNode(elem, self._head)
self._rear = self._head
else:
self._head = LNode(elem, self._head) def append(self, elem):
if self._head is None:
self._head = LNode(elem, self._head)
self._rear = self._head
else:
self._rear.next = LNode(elem)
self._rear = self._rear.next def pop_last(self):
if self._head is None:
raise LinkedListUnderflow("in pop_last")
p = self._head
if p.next is None:
self._head = None
return p.elem
while p.next.next is not None:
p = p.next
e = p.next.elem
p.next = None
self._rear = p
return e

python单链表的更多相关文章

  1. 用最简单的方式学Python单链表

    Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...

  2. python单链表的基本操作思路

    单链表: 1.定义链表 class ListNode: # 定义节点 def __init__(self, x): self.val = x # 节点当前值 self.next = None # 指向 ...

  3. Python单链表实现

    class Node(): def __init__(self,InitDate): self.Date=InitDate self.next=None def setNext(self,newnex ...

  4. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  5. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

  6. 数据结构:单链表结构字符串(python版)

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  7. Python 之简易单链表

    单链表的基本要素有 2 个,数据项和连接项.这两项在 Python 中可以通过对象及其属性来实现. class Node: def __init__ (self, data): self.data = ...

  8. python 数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  9. python实现数据结构单链表

    #python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...

随机推荐

  1. Visual Studio模板

    转载自MSDN,此文仅作参考. http://msdn.microsoft.com/zh-cn/library/6db0hwky(VS.80).aspx 1. 如何导入“项目模板(Project Te ...

  2. 【原创】Self-Contained Container(自包含容器)

    自包含容器是一种自包含的结构,很有趣.需要使用模板类,但只有在模板类型是该类的子类时,才使该类具有自包含的结构.这种结构从数据结构的角度看比较有用.通常的树类中的模板通常是“数据”的概念,即模板不参与 ...

  3. K8S+GitLab-自动化分布式部署ASP.NET Core(二) ASP.NET Core DevOps

    一.介绍 前一篇,写的K8S部署环境的文章,简单的介绍下DevOps(Development和Operations的组合词),高效交付, 自动化流程,来减少软件开发人员和运维人员的沟通.Martin ...

  4. 修改ActiveReports验证文字“给不能为 null 的参数指定一个 null 值”

    转:http://gcdn.gcpowertools.com.cn/showtopic-13759.html ActiveReports官方网站:http://www.gcpowertools.com ...

  5. Redirect与Transfer 的区别

    共同点: 都是重定向: 不同点: redirect: 1发生在客户端: 2.发送两次请求,第一次请求原始页面,当调用此方法时,创建一个应答头,返回状态码302,第二次请求重定向的页面: 3.得不到任何 ...

  6. 理解DDoS防护本质:基于资源较量和规则过滤的智能化系统

    本文由  网易云发布. 随着互联网生态逐渐形成,DDoS防护已经成为互联网企业的刚需要求,网易云安全(易盾)工程师根据DDoS的方方面面,全面总结DDoS的攻防对抗. 1.什么是DDoS DDoS全称 ...

  7. RabbitMQ交换机规则实例

    RabbitMQ Exchange分发消息时根据类型的不同分发策略有区别,目前共四种类型:direct.fanout.topic.headers .headers 匹配 AMQP 消息的 header ...

  8. PHP开发接口,封装方法

    接口的主要功能是从服务器端获取数据,然后渲染到客户端 其主要的实现流程一般会经历这样的几个阶段服务器端----> 数据库|缓存 ----> 调用接口 ---->客户端 在接口数据传输 ...

  9. Redis初探,写个HelloWorld

    资源获取 https://redis.io/download 从官网上下载redis的源码,使用gcc的安装方式. 安装 make make install 需要达到的效果是,在/usr/local/ ...

  10. 快速滑动时 `cellForRow` 的调用次数

    问题 有一个 1000 个 cell 的 tableView,刚刚进入界面时,contentOffset 为 0.用手快速滑动 tableView,直至最下面一个 cell 显示在屏幕上. 这个过程中 ...