[Python数据结构] 使用 Circular List实现Queue

1. Queue
队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端进行删除操作。队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。

Queue[维基百科]

2. Queue ADT
队列是一种抽象数据类型,其实例Q需要支持两种方法:
  1)Q.enqueue(e)  : Add an element to the back of queue.
  2)Q.dequeue( )  : Remove and return the first element in the queue

另外,为了方便使用,我们还定义了以下方法:
  3)Q.first() : Return the element at the front of the queue
  4)Q.is_empty() : Return True if the queue is empty
  5)len(Q) : Return the number of elements in the queue

3. Queue Implementation

 class Empty(Exception):
"""Error attempting to access an element from an empty container"""
pass
 class ArrayQueue():
"""FIFO queue implementation using a python list as underlying storage."""
Default_capacity = 10 def __init__(self):
"""Create an empty queue"""
self.data = [None] * ArrayQueue.Default_capacity # reference to a list instance with a fixed capacity.
self.size = 0 # an integer representing the current number of elements stored in the queue.
self.front = 0 # an integer that represents the index within data of the first element of the queue. def __len__(self):
"""Return the number od elements in the queue."""
return self.size def is_empty(self):
"""Return True if the queue is empty."""
return self.size == 0 def first(self):
"""Return the element at the front of the queue. Raise Empty exception if the queue is empty."""
if self.is_empty():
raise Empty('the queue is empty')
return self.data[self.front] def dequeue(self):
"""Remove and return the first element in the queue. Raise Empty exception if the queue is empty."""
if self.is_empty():
raise Empty('the queue is empty')
answer = self.data[self.front]
self.data[self.front] = None
self.front = (self.front + 1) % len(self.data)
self.size -= 1
return answer def enqueue(self, e):
"""Add an element to the back of queue."""
if self.size == len(self.data):
self.resize(2 * len(self.data))
avail = (self.front + self.size) % len(self.data)
self.data[avail] = e
self.size += 1 def resize(self, cap):
"""Resize to a new list of capacity."""
old = self.data
self.data = [None] * cap
walk = self.front
for k in range(self.size):
self.data[k] = old[walk]
walk = (1 + walk) % len(old)
self.front = 0

4. 执行结果:

 q = ArrayQueue()
l = np.random.randint(0, 10, size=(20, ))
for i in l:
q.enqueue(i)
 q.data
[0, 1, 5, 3, 9, 5, 0, 9, 1, 0, 4, 6, 7, 0, 2, 5, 9, 3, 3, 9]
 q.dequeue()
0
 q.data
[None, 1, 5, 3, 9, 5, 0, 9, 1, 0, 4, 6, 7, 0, 2, 5, 9, 3, 3, 9]
 q.first()
1
 q.data
[None, 1, 5, 3, 9, 5, 0, 9, 1, 0, 4, 6, 7, 0, 2, 5, 9, 3, 3, 9]

[Python数据结构] 使用 Circular List实现Queue的更多相关文章

  1. python数据结构之栈与队列

    python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...

  2. Python - 数据结构 - 第十五天

    Python 数据结构 本章节我们主要结合前面所学的知识点来介绍Python数据结构. 列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和 ...

  3. Python数据结构汇总

    Python数据结构汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.线性数据结构 1>.列表(List) 在内存空间中是连续地址,查询速度快,修改也快,但不利于频繁新 ...

  4. python数据结构之二叉树的统计与转换实例

    python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...

  5. Python数据结构与算法之图的广度优先与深度优先搜索算法示例

    本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...

  6. python数据结构之图深度优先和广度优先实例详解

    本文实例讲述了python数据结构之图深度优先和广度优先用法.分享给大家供大家参考.具体如下: 首先有一个概念:回溯 回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到 ...

  7. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  8. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  9. python数据结构之图的实现

    python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...

随机推荐

  1. 学习 Shell —— 认识 shell

    0. 认识 shell shell 是一个命令行解释器(interpreter),它会输出一个提示符,等待输入一个命令,然后执行该命令.如果该命令行的第一个单词不是一个内置的 shell 命令,那么 ...

  2. 2018.09.09 DL24 Day2总结

    今天挂的有点惨…… T1.forging 这道题自己在考试的时候想出来了…… 这题是一个期望递推.我们首先考虑这么一件事,一枚硬币,你抛到正面停止,抛到反面继续抛,问期望抛的次数.是两次.我们假设期望 ...

  3. E20171212-hm

    odd   adj. 古怪的; 奇数的; 剩余的; 临时的; odd number 奇数 even adj. 偶数的 even number 偶数

  4. bzoj 1653: [Usaco2006 Feb]Backward Digit Sums【dfs】

    每个ai在最后sum中的值是本身值乘上组合数,按这个dfs一下即可 #include<iostream> #include<cstdio> using namespace st ...

  5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  6. CentOS 7 配置 Nginx 正向代理 http、https 最详解

    手头项目中有使用到 nginx,因为使用的三方云服务器,想上外网需要购买外网IP的,可是有些需要用到外网却不常用的主机也挂个外网IP有点浪费了,便想使用nginx的反向代理来实现多台内网服务器使用一台 ...

  7. idea下载安装指南

    官网地址 https://www.jetbrains.com/idea/ 点击download 有收费版本和社区免费版.我下载了免费的. 有zip和exe两个版本.我先下载了zip绿色版,发现用不了. ...

  8. Tuple类型的使用

    1.什么是Tuple Tuple类型,可以存放任何类型 2.Tuple有哪些分类 .Net 4.0 定义了8个泛型Tuple类,和一个Tuple静态类 3.Tuple的使用

  9. arp学习笔记(linux高性能服务编程)

    先看看arp的定义吧 现在linux运行这条命令 tcpdump -i eth0:1 -ent '(dst 192.168.5.190 and src 192.168.5.109)or( dst 19 ...

  10. Pycharm消除波浪线

    PyCharm使用了较为严格的PEP8检查规则,稍微有点错误就会出现波浪线提示.那么怎么消除这些波浪线呢?一个简单粗暴的方法就是:在编辑器的右下角有个小人形状的按钮,点开之后有个滚动条,将滚动条滑动到 ...