[Python数据结构] 使用 Circular List实现Queue
2024-10-12 19:03:42
[Python数据结构] 使用 Circular List实现Queue
1. Queue
队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端进行删除操作。队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。
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]
最新文章
- Weblogic集群
- CACHE COHERENCE AND THE MESI PROTOCOL
- AngularJS in Action读书笔记5(实战篇)——在directive中引入D3饼状图显示
- 多国语言文档识别 ABBYY FineReader Corporate v12.0.101.388.7z 绿色破解版
- 安卓开发_浅谈Android动画(三)
- 将XML解析成DOM文档
- Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用
- vsftp不同帐号的目录和权限
- poj1651
- Mysql 中bitwise对效率的影响??
- 转几篇WPF文章
- 排名第一、第二的OCR软件
- idmap_ad — Samba's idmap_ad Backend for Winbind《转载》
- 深入浅出Redis-redis底层数据结构(下)
- FZU 1502 Letter Deletion(DP)
- js原型链的深度理解!
- PHP中empty,isset,is_null的区别
- Java:内部接口
- [每天解决一问题系列 - 0007] 如何创建Catalog并用其签名
- 使用openpyxl实现excel文件的读取操作
热门文章
- 利用JProfile 7分析内存OOM
- android压力测试命令monkey详解【转】
- Ubuntu14.04 x64 zabbix 3.0 安装
- Oracle存储过程(增、删、改)写法、oracle执行存储过程
- [译]NUnit--Installation(三)
- BZOJ_4753_[Jsoi2016]最佳团体_树形背包+01分数规划
- Spark 分布式环境---slave节点无法启动(已解决)
- 【废弃】【WIP】JavaScript Object
- MQTT + apache-apollo服务器初学使用
- JS自定义右键菜单案例