由于队列的动态由队头指针与队尾指针共同反映,所以我们在实现先入后出的同时还要实现队头元素与队尾元素的访问。对于普通的队列,我们使用列表实现其顺序存储,使用其它方法实现其链式存储。


顺序存储

由于我们使用list作为queue的底层、用Queue类对list进行了简单封装,所以在顺序存储结构中我们可以方便的利用列表的方法。具体代码:

class Queue(object):'定义队列。'def __init__(self):'初始化一个空列表。'self.items = []def IsEmpty(self):'方法:判断队列是否为空。'return self.items == []def get_size(self):'方法:求队列长度。'return len(self.items)def get_TopValue(self):'方法:访问队头元素。'if self.IsEmpty():return Noneelse:return self.items[0]def get_BottomValue(self):'方法:访问队尾元素。'if self.IsEmpty():return Noneelse:return self.items[-1]def push(self,data):'方法:进队。'self.items.append(data)def pop(self):'方法:出队。'if self.IsEmpty():raise IndexError('队列为空。')else:self.items.pop(0)

简单测试:

my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))[my_queue.push(i + 1) for i in range(10)]
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))for i in range(11):print('第{}次:'.format(i + 1))my_queue.pop()print('The top value is {}'.format(my_queue.get_TopValue()))print('The bottom value is {}'.format(my_queue.get_BottomValue()))

测试结果:

Is my queue empty?
True
The top value is 1
The bottom value is 10
第1次:
The top value is 2
The bottom value is 10
第2次:
The top value is 3
The bottom value is 10
第3次:
The top value is 4
The bottom value is 10
第4次:
The top value is 5
The bottom value is 10
第5次:
The top value is 6
The bottom value is 10
第6次:
The top value is 7
The bottom value is 10
第7次:
The top value is 8
The bottom value is 10
第8次:
The top value is 9
The bottom value is 10
第9次:
The top value is 10
The bottom value is 10
第10次:
The top value is None
The bottom value is None
第11次:
Traceback (most recent call last):File "带链的队列.py", line 58, in <module>my_queue.pop()File "带链的队列.py", line 44, in popraise IndexError('队列为空。')
IndexError: 队列为空。

链式存储

我们通过实例化一个Node类来表示节点,其中存储有数据值data和后件next,具体代码为

class Node(object):'定义节点。'def __init__(self):'初始化数据域和指针域。'self.data = Noneself.next = Noneclass Queue(object):'定义队列。'def __init__(self):'初始化队头top与队尾bottom。'self.top = Noneself.bottom = Noneself.size = 0def IsEmpty(self):'方法:判断是否为空队。'if self.size == 0:return Trueelse:return Falsedef get_size(self):'方法:得到队列大小。'return self.sizedef get_TopValue(self):'方法:访问队头元素。'if self.IsEmpty():return Noneelse:return self.top.datadef get_BottomValue(self):'方法:访问队尾元素。'if self.IsEmpty():return Noneelse:return self.bottom.datadef push(self,data):'方法:入队。'node = Node()node.data = dataif self.IsEmpty():self.top = nodeself.bottom = nodeelse:self.bottom.next = nodeself.bottom = nodeself.size += 1def pop(self):'方法:出队。'if not self.IsEmpty():self.top = self.top.nextelse:raise IndexError('队列为空。')self.size -= 1

简单测试:

my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))[my_queue.push(i + 1) for i in range(10)]
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
print('The size of my queue is {}'.format(my_queue.get_size()))for i in range(11):print('第{}次:'.format(i + 1))my_queue.pop()print('The top value is {}'.format(my_queue.get_TopValue()))print('The bottom value is {}'.format(my_queue.get_BottomValue()))print('The size of my queue is {}'.format(my_queue.get_size()))

测试结果:

Is my queue empty?
True
The top value is 1
The bottom value is 10
The size of my queue is 10
第1次:
The top value is 2
The bottom value is 10
The size of my queue is 9
第2次:
The top value is 3
The bottom value is 10
The size of my queue is 8
第3次:
The top value is 4
The bottom value is 10
The size of my queue is 7
第4次:
The top value is 5
The bottom value is 10
The size of my queue is 6
第5次:
The top value is 6
The bottom value is 10
The size of my queue is 5
第6次:
The top value is 7
The bottom value is 10
The size of my queue is 4
第7次:
The top value is 8
The bottom value is 10
The size of my queue is 3
第8次:
The top value is 9
The bottom value is 10
The size of my queue is 2
第9次:
The top value is 10
The bottom value is 10
The size of my queue is 1
第10次:
The top value is None
The bottom value is None
The size of my queue is 0
第11次:
Traceback (most recent call last):File "带链的队列.py", line 84, in <module>my_queue.pop()File "带链的队列.py", line 70, in popraise IndexError('队列为空。')
IndexError: 队列为空。

Python数据结构与算法基础|第三期:代码实现——顺序存储队列与链式存储队列相关推荐

  1. 队列的链式存储结构及其实现_了解队列数据结构及其实现

    队列的链式存储结构及其实现 A queue is a collection of items whereby its operations work in a FIFO - First In Firs ...

  2. python数据结构与算法知识点_数据结构和算法基础知识点(示例代码)

    数据结构和算法基础知识点 链表 1.链表是一种由节点组成的线性数据集合,每个节点通过指针指向下一个节点.它是 一种由节点组成,并能用于表示序列的数据结构. 2.单链表:每个节点仅指向下一个节点,最后一 ...

  3. Python数据结构与算法基础|第二期:代码实现——栈的顺序存储与链式存储

    顺序存储的栈 由于Python内置的列表是顺序存储的,所以我们直接使用列表作为顺序存储的栈的底层.具体代码如下: class Stack(object):'实现顺序栈.'def __init__(se ...

  4. Python数据结构与算法基础|第五期:代码实现——循环队列的链式存储结构

    在上一次,我们通过取余等数学方法实现了顺序存储的循环队列.由于我们使用的是Python内置的列表类型作为底层,实际上我们的存储空间并不是首尾相连的.下面,我们使用链式存储结构来实现一个真正首尾相连的循 ...

  5. 数据结构第六篇——顺序存储结构与链式存储结构的特点

    ♥注:未经博主同意,不得转载. 两者特点: 顺序表的特点是逻辑上相邻的数据元素,物理存储位置也相邻,并且,顺序表的存储空间需要预先分配. 它的优点: (1)方法简单,各种高级语言中都有数组,容易实现. ...

  6. 数据结构 3-2-1 队列的链式存储实现

    一.概念 队列也是一种受限的线性表,只允许一端插入另一段输出,就像其名字一样,队列具有先入先出的特点,这是由于其受限的特点所决定的.用链式结构实现队列,称为链队列,实际上是一个带有队头指针和队尾指针的 ...

  7. 第三章 数据结构 线性表的逻辑结构 和 线性表的顺序存储结构,链式存储结构

    文章目录 线性表的特点 引用 集合与线性表的区别在于元素是否可以重复. 线性表的顺序存储结构 顺序存储的优缺点: 一维数组来表示顺序表的数据存储区域. 线性表的链式存储结构 链式存储的优缺点 线性表的 ...

  8. 数据结构之顺序存储结构和链式存储结构分析 , 图文并茂 , 又涨姿势了

    在计算机中,数据元素并不是孤立.杂乱无序的,而是具有内在联系的数据集合.数据元素之间存在一种或多种特定关系,也就是数据的组织形式.为编写出一个 好"的程序,必须分析待处理对象的特性及各处理对 ...

  9. 从C语言的角度重构数据结构系列(三)- 顺序存储结构和链式存储结构之顺序表

    前言 在学习具体的数据结构和算法之前,每一位初学者都要掌握一个技能,即善于运用时间复杂度和空间复杂度来衡量一个算法的运行效率. 在这里给自己打个广告,需要的小伙伴请自行订阅. python快速学习实战 ...

最新文章

  1. 股市复盘:本周交易数据分析
  2. 2021CVPR顶会冠军带你解密图像分割
  3. SAP Spartacus auth.service.ts里的user调用触发的源代码位置
  4. 有趣java_有趣的Java
  5. idea创建springmvc项目
  6. freecplus框架-ftp客户端
  7. 近6年被引用次数最多的深度学习论文top100(附下载地址)
  8. python并发编程 协程_Python并发编程协程之Gevent
  9. 计算机科学技术学习引论
  10. matlab进行复数计算
  11. 微软project服务器搭建,安装和配置 Project Server 2013
  12. html转换到pdf转换器,HTML转换到PDF转换器
  13. 计科之路--linux学习
  14. python 字符串输出时 使用空格隔开
  15. 巨儒艮、漂泊信天翁……这位90后中科院动物所研究员用日历讲述“物种故事”...
  16. 2022中科院分区表弃用影响因子,多方官宣
  17. Kubectl :--v 接口调试,以及设置日志输出详细程度
  18. java基础(1~100以内的质数)
  19. 软件测试工程师涨薪攻略,1年多经验的测试从月薪8k-17k的转变
  20. 【网络协议详解】——GNS3的使用(学习笔记)

热门文章

  1. 线性布局管理器LinearLayout
  2. GreenDao3.0 使用(包括导入,具体方法,基本使用,加密,数据库升级等)
  3. 记录使用scrapy爬取新闻网站最新新闻存入MySQL数据库,每天定时爬取自动更新
  4. 2021年新版CDA LEVELⅠ 模拟题(二)
  5. [前端框架]-VUE(上篇)
  6. outlook2007 配置
  7. 小说里的编程 【连载之十七】元宇宙里月亮弯弯
  8. 程序员苹果电脑使用入门
  9. 苹果消息推送服务教程:第一二部分(共2部分)
  10. 海康摄像机web3.0控件