#!/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. winform程序更新

    更新程序和主程序是分开的,得在做一个exe可执行更新程序. 主程序在登陆时判断是否需要更新. 我这边判断方式是直接在配置文件里面设置版本号,然后和服务器上面的版本对比,低于服务器版本就更新程序. // ...

  2. c# 字符串去掉两端空格,并且将字符串中多个空格替换成一个空格

    字符串去掉两端空格,并且将字符串中多个空格替换成一个空格: 主要还是考察使用字符串的方法: trim(); 去掉字符串两端空格 split(); 切割 string.join(); 连接 class ...

  3. openvswitch 源码分析 OVS_ACTION_ATTR_HASH action

    1.在ovs_dp_process_packet中查找kernel缓存流表,查到后执行ovs_execute_actions->do_execute_actions,其中有个actions是OV ...

  4. python 十进制 和 IP 地址互转

    #! /bin/python def ip2decimalism(ip): dec_value = 0 v_list = ip.split('.') v_list.reverse() t = 1 fo ...

  5. python web开发c6——阿里云上ubuntu+flask+gunicorn+nginx服务器部署(一)简单测试

    简述 Nginx在服务器部署中的作用 请求通过Nginx实现反向代理,将请求提交给代理服务器.本文中只用了一台服务器,所以是代理到本机. gunicorn的作用 作为服务器代码的容器.接收Nginx的 ...

  6. Ubuntu18.04 - 返回到Gnome经典桌面!

    Ubuntu18.04默认的桌面是定制版的Gnome,说实话,真的不喜欢,还是喜欢Gnome的经典桌面,那么如何进入呢?执行下面命令,执行完毕后注销,选择一下就可以了! sudo apt-get in ...

  7. python 简单搭建非阻塞式单进程,select模式,epoll模式服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 可以看我的上篇文章 <python 简单搭建阻塞式单进程,多进程, ...

  8. PHP中使用CURL模拟登录并获取数据实例

    cURL 是一个功能强大的PHP库,使用PHP的cURL库可以简单和有效地抓取网页并采集内容,设置cookie完成模拟登录网页,curl提供了丰富的函数,开发者可以从PHP手册中获取更多关于cURL信 ...

  9. CDN添加流程

    CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...

  10. 解决chrome无法启用印象笔记-剪藏功能

        新版Chrome浏览器安全的问题,导致从印象笔记官网下载的rxs(crx)文件无法直接通过drap&drop功能添加功能块.可以将rxs(crx)文件后缀名改成rar格式,然后解压,通 ...