[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. 以太网接口TCP/IP协议介绍,说的很容易懂了

      以太网接口TCP/IP协议介绍,说的很容易懂了  TCP/IP协议,或称为TCP/IP协议栈,或互联网协议系列. TCP/IP协议栈(按TCP/IP参考模型划分) 应用层 FTP SMTP HTT ...

  2. cc1: error: bad value (armv5) for -march= switch【转】

    本文转载自:https://stackoverflow.com/questions/23871924/cc1-error-bad-value-armv5-for-march-switch Ask Qu ...

  3. YTU 2899: D-险恶逃生 I

    2899: D-险恶逃生 I 时间限制: 1 Sec  内存限制: 128 MB 提交: 130  解决: 55 题目描述 Koha被邪恶的巫师困在一个m*n的矩阵当中,他被放在了矩阵的最左上角坐标( ...

  4. git 配置代理

    1.目的:配置proxy,使得git可以克隆github上的代码 2.方法:执行下面三条命令,配置下git的代理 git config --global https.proxy https://w00 ...

  5. 关于redis、memcache、mongoDB的对比

    从以下几个维度,对 Redis.memcache.MongoDB 做了对比.1.性能都比较高,性能对我们来说应该都不是瓶颈.总体来讲,TPS 方面 redis 和 memcache 差不多,要大于 m ...

  6. android短信拦截

    广播分2种,无序广播和有序广播.可以理解为散列和队列广播. 首先无序广播,不能中断,分发机制有点类似散列发送.这种广播的的发送为:context.sendBroadcast这种广播是不能中断的,请看A ...

  7. 第九周 Leetcode 502. IPO (HARD)

    Leetcode 502 一个公司 目前有资产W 可以选择实现K个项目,每个项目要求公司当前有一定的资产,且每个项目可以使公司的总资产增加一个非负数. 项目数50000 设计一个优先队列,对于当前状态 ...

  8. ODB(C++ ORM)用Mingw的完整编译过程

    用mingw官方的GCC4.7.2编译libodb后,并用odb compiler对hello示例生成odb的"包裹"代码,编译链接总是不能通过,下面是编译example/hell ...

  9. Python机器学习算法 — 决策树(Decision Tree)

    决策树 -- 简介         决策树(decision tree)一般都是自上而下的来生成的.每个决策或事件(即自然状态)都可能引出两个或多个事件,导致不同的结果,把这种决策分支画成图形很像一棵 ...

  10. bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】

    最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...