#!/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. linux系统编程之信号(一):中断与信号

    一,什么是中断? 1.中断的基本概念 中断是指计算机在执行期间,系统内发生任何非寻常的或非预期的急需处理事件,使得CPU暂时中断当前正在执行的程序而转去执行相应的事件处理程序,待处理完毕后又返回原来被 ...

  2. Spring Boot 2 实践记录之 Powermock 和 SpringBootTest

    由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...

  3. 拒绝“高冷”词汇!初学C#中的委托

    有一天,你写了好多好多带“形参”的构造函数(就是“方法”,同义),而且需要向这些构造函数里传递同样的“实参”,然后你就憨憨地一个一个函数的调用并赋予同样的“实参”,这一天就这么过去了... 又过了几天 ...

  4. .net core2.0 codefirst 创建数据库的问题!

    appsettings.json和Startup.cs就不记录了,网上很多!! 1.必须在有DbContext类的项目里添加这3个NuGet引用 Microsoft.EntityFrameworkCo ...

  5. Day 9 作业题(完成)

    # 练习题# 1.整理函数相关知识点,画思维导图,写博客 # 2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者.'''def func1(argv): f ...

  6. python寻找list中最大值、最小值并返回其所在位置

    c = [-10,-5,0,5,3,10,15,-20,25]   print c.index(min(c))  # 返回最小值 print c.index(max(c)) # 返回最大值  

  7. 《Python黑帽子:黑客与渗透测试编程之道》 Web攻击

    Web的套接字函数库:urllib2 一开始以urllib2.py命名脚本,在Sublime Text中运行会出错,纠错后发现是重名了,改过来就好: #!/usr/bin/python #coding ...

  8. ESP8266 wifi干扰、钓鱼实现

    说明:绝大部分都是对着下面的参考文章来做的.这里只把基本流程和我自己遇到的问题写一下.如有其他问题可以访问文章末的参考文章进行查找! esp8266模块 我们需要购买一块esp8266模块,如下图所示 ...

  9. github本地分支合并到线上主分支

    如果是在本地index-swiper分支上,已经写好了那么: 1,git add .             //提交到本地缓冲区 2,git commit -m "project init ...

  10. RabbitMQ Java实例

    引入RabbitMQ的jar包 <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amq ...