[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. redis集群在window下安装

    1.下载安装单机版:  https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi ...

  2. HDU 5752Sqrt Bo

    Sqrt Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  3. YTU 2520: 小慧唱卡拉OK

    2520: 小慧唱卡拉OK 时间限制: 1 Sec  内存限制: 128 MB 提交: 478  解决: 207 题目描述 小慧唱歌非常好听,小鑫很喜欢听小慧唱歌,小鑫最近又想听小慧唱歌了,于是小鑫请 ...

  4. bzoj 1924 所驼门王的宝藏

    题目大意: 有一个r*c的矩阵,上面有n个点有宝藏 每个有宝藏的点上都有传送门 传送门有三种:第一种可以传到该行任意一个有宝藏的点,第二种可以传到该列任意一个有宝藏的点,第三种可以传到周围的八连块上有 ...

  5. easyui 生成tas方式

    1.采用<a>标签形式 <div id="tabs" style="width:100%;"> <ul> <li id ...

  6. DNS的主从、子域授权和转发服务器

    DNS的主从.子域授权和转发服务器 主从DNS 注意: 1.全局配置options{} 里面的内容,其中 listen-on port 53 {any or local:}:或者直接注释掉,或删掉 a ...

  7. (进制)51NOD 1057 N的阶乘

    输入N求N的阶乘的准确值.   Input 输入N(1 <= N <= 10000) Output 输出N的阶乘 Input示例 5 Output示例 120解:这其实是MOD进制,将一个 ...

  8. tp5增加验证的自定义规则

  9. 使用wkwebview后,页面返回不刷新的问题

    onpageshow 事件在用户浏览网页时触发. onpageshow 事件类似于 onload 事件,onload 事件在页面第一次加载时触发, onpageshow 事件在每次加载页面时触发,即 ...

  10. LN : leetcode 263 Ugly Number

    lc 263 Ugly Number lc 263 Ugly Number Write a program to check whether a given number is an ugly num ...