队列 / Queue
数组队列
数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构。
Queue:
<--| 1 | 2 | 3 | 4 | 5 |<--
下面将使用Python中的list来替代C语言中的数组实现数组队列的数据结构。
Note: 这里的实现并没有像C语言中的申请一块固定大小的数组,手动的定制数组中队列的头尾位置,而是利用list的特性直接完成,因此较为简单。
数组队列的实现与数组栈的实现基本类似,同时入列和出列也十分简单,仅需要对数组进行操作即可。
这里直接给出完整代码和运行结果,实现过程可参考数组栈的实现。
后续再补充固定数组大小的队列实现。
完整代码
1 class QueueEmptyException(Exception): pass 2 3 4 class QueueFullException(Exception): pass 5 6 7 class Queue: 8 """ 9 Queue: 10 <--| 1 | 2 | 3 | 4 | 5 |<-- 11 """ 12 def __init__(self, max=0): 13 self.queue = [] 14 self._max = max 15 self.max = max 16 17 def __str__(self): 18 return ' | '.join(map(str, self.queue)) 19 20 def init(self, iterable=()): 21 if not iterable: 22 return 23 self.queue.extend(list(iterable)) 24 25 @property 26 def max(self): 27 return self._max 28 29 @max.setter 30 def max(self, m): 31 m = int(m) 32 if m < self.length: 33 raise Exception('Resize queue failed, please dequeue some elements first.') 34 self._max = m 35 if self._max < 0: 36 self._max = 0 37 38 def show(self): 39 print(self) 40 41 @property 42 def length(self): 43 return len(self.queue) 44 45 @property 46 def is_empty(self): 47 return not bool(self.queue) 48 49 @property 50 def is_full(self): 51 return bool(self._max and self.length == self._max) 52 53 def enqueue(self, item): 54 if self.is_full: 55 raise QueueFullException('Error: trying to enqueue element into a full queue') 56 self.queue.append(item) 57 58 def dequeue(self): 59 if self.is_empty: 60 raise QueueEmptyException('Error: trying to dequeue element from an empty queue') 61 front = self.queue[0] 62 self.queue = self.queue[1:] 63 return front 64 65 def clear(self): 66 self.queue = [] 67 68 69 def test(queue): 70 print('\nInit queue:') 71 queue.init([1, 2, 3, 4, 5, 6, 7]) 72 queue.show() 73 74 print('\nEnqueue element to queue:') 75 queue.enqueue('like') 76 queue.show() 77 78 print('\nDequeue element from queue:') 79 e = queue.dequeue() 80 print('Element %s deququed,' % e) 81 queue.show() 82 83 print('\nSet queue max size:') 84 try: 85 queue.max = 1 86 except Exception as e: 87 print(e) 88 89 print('\nSet queue max size:') 90 queue.max = 7 91 print(queue.max) 92 93 print('\nEnqueue full queue:') 94 try: 95 queue.enqueue(7) 96 except QueueFullException as e: 97 print(e) 98 99 print('\nClear queue:') 100 queue.clear() 101 queue.show() 102 103 print('\nQueue is empty:') 104 print(queue.is_empty) 105 106 print('\nDequeue empty queue:') 107 try: 108 queue.dequeue() 109 except QueueEmptyException as e: 110 print(e) 111 112 if __name__ == '__main__': 113 test(Queue())
运行得到结果
Init queue: 1 | 2 | 3 | 4 | 5 | 6 | 7 Enqueue element to queue: 1 | 2 | 3 | 4 | 5 | 6 | 7 | like Dequeue element from queue: Element 1 deququed, 2 | 3 | 4 | 5 | 6 | 7 | like Set queue max size: Resize queue failed, please dequeue some elements first. Set queue max size: 7 Enqueue full queue: Error: trying to enqueue element into a full queue Clear queue: Queue is empty: True Dequeue empty queue: Error: trying to dequeue element from an empty queue
相关阅读
1 数组栈