-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy patharray_queue.py
More file actions
43 lines (30 loc) · 847 Bytes
/
array_queue.py
File metadata and controls
43 lines (30 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 使用数组实现一个队列
class ArrayQueue:
def __init__(self, capacity: int):
self.data = []
self.size = 0
self.capacity = capacity
# 入队列
def enqueue(self, val: int):
if self.size >= self.capacity:
raise RuntimeError('the queue is full')
self.data.append(val)
self.size += 1
# 出队列
def dequeue(self):
if self.size <= 0:
raise RuntimeError('the queue is empty')
res = self.data[0]
self.data = self.data[1:]
self.size -= 1
return res
if __name__ == '__main__':
queue = ArrayQueue(10)
queue.enqueue(12)
queue.enqueue(19)
queue.enqueue(10)
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
queue.enqueue(100)
print(queue.dequeue())