-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
75 lines (57 loc) · 1.54 KB
/
Copy pathqueue.py
File metadata and controls
75 lines (57 loc) · 1.54 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Queue(object):
"""
循环队列
默认队列容量为4,可在实例化时自定义
"""
def __init__(self, capacity=4):
self.capacity = capacity
self.index = -1
self.head = 0
self.tail = 0
self.queue = []
self.length = 0
# 计数,返回长度
def count(self):
return self.length
# 判满
def full(self):
if self.count() == self.capacity:
return True
return False
# 判空
def empty(self):
if self.count() == 0:
return True
return False
# 清除队列
def clear(self):
self.head = 0
self.tail = 0
self.queue = []
self.length = 0
# 插入队尾
def put_in(self, value):
if self.full():
return False
self.queue.append(value)
self.tail = (self.tail + 1) % self.capacity
self.length += 1
return True
# 冒出队头
def get_out(self):
if self.empty():
return False
frist = self.queue[0]
del(self.queue[0])
self.head = (self.head + 1) % self.capacity
self.length -= 1
return frist
# 实现迭代
def __iter__(self):
self.index = -1
return self
def __next__(self):
self.index += 1
if self.index == self.length:
raise StopIteration
return self.queue[self.index]