-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueClass.py
179 lines (153 loc) · 4.69 KB
/
QueueClass.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
名称: 队列
定义: 具有一定操作约束的线性表,只在一端(栈顶,top)做插入,删除
先入先出(FIFO)
数据对象集:有0个或多个元素的有穷线性表
操作集:1.生成空队列,其最大长度为MaxSize
2.判断队列是否已满
3.入队
4.判断是否为空
5.删除并返回队头元素
"""
from DataStructure.ErrorClass import StorageSmallError
# SequenceQueue堆栈的顺序存储
# 循环队列
class SequenceQueue:
def __init__(self, maxsize, input_list=()):
self._maxsize = maxsize
if len(input_list) > maxsize - 1:
raise StorageSmallError
self._maxsize = maxsize
self._front = 0
self._rear = len(input_list)
self._sequence_queue = list(input_list)
# self._sequence_queue = [None]
# self._sequence_queue.extend(list(input_list))
self._sequence_queue.extend([None] * (maxsize - len(input_list)))
@property
def maxsize(self):
return self._maxsize
def __repr__(self):
if self._front <= self._rear:
return str(self._sequence_queue[self._front: self._rear])
else:
return str(self._sequence_queue[self._front:] + self._sequence_queue[: self._rear])
# 入队
def add(self, input_value):
if self.is_full():
print("队列满")
else:
self._sequence_queue[self._rear] = input_value
self._rear = (self._rear + 1) % self._maxsize
# 删除并返回队首元素
def delete(self):
if self.is_empty():
print("队列为空")
else:
val = self._sequence_queue[self._front]
self._sequence_queue[self._front] = None
self._front = (self._front + 1) % self._maxsize
return val
# 判断队列是否已满
def is_full(self):
return self._front == (self._rear + 1) % self._maxsize
# 判断是否为空
def is_empty(self):
return self._front == self._rear
def SequenceQueue_test():
new_queue = SequenceQueue(5, [1, 2, 3])
print(new_queue)
print(new_queue.is_empty())
print(new_queue.is_full())
new_queue.add(4)
new_queue.add(4)
new_queue.add(4)
print(new_queue)
print(new_queue.is_empty())
print(new_queue.is_full())
new_queue.delete()
new_queue.delete()
new_queue.delete()
print(new_queue)
print(new_queue.is_empty())
print(new_queue.is_full())
new_queue.delete()
new_queue.delete()
new_queue.delete()
print(new_queue)
print(new_queue.is_empty())
print(new_queue.is_full())
new_queue.add(4)
new_queue.add(4)
new_queue.add(4)
print(new_queue)
print(new_queue.is_empty())
print(new_queue.is_full())
from DataStructure.LinearList import ListNode
# LinkQueue堆栈的链式存储
class LinkQueue:
def __init__(self, input_list=(), head_value=None):
self._front = ListNode(head_value)
old_node = self._front
for value in input_list:
new_node = ListNode(value)
old_node.next = new_node
old_node = new_node
self._rear = old_node
def __repr__(self):
result = []
node_temp = self._front.next
while node_temp:
result.append(node_temp.val)
node_temp = node_temp.next
# result.reverse()
return str(result)
# 入队
def add(self, input_value):
node_temp = ListNode(input_value)
self._rear.next = node_temp
self._rear = node_temp
# 删除并返回队首元素
def delete(self):
if self.is_empty():
print("队列为空")
else:
node_temp = self._front.next
if node_temp == self._rear:
self._rear = self._front
self._front.next = node_temp.next
return node_temp.val
# 判断是否为空
def is_empty(self):
return bool(not self._front.next)
def LinkQueue_test():
new_queue = LinkQueue([1, 2, 3])
print(new_queue)
print(new_queue.is_empty())
new_queue.add(4)
new_queue.add(4)
new_queue.add(4)
print(new_queue)
print(new_queue.is_empty())
new_queue.delete()
new_queue.delete()
print(new_queue)
new_queue.delete()
print(new_queue)
print(new_queue.is_empty())
new_queue.delete()
new_queue.delete()
# new_queue.add(4)
# # new_queue.add(4)
new_queue.delete()
new_queue.delete()
print(new_queue)
print(new_queue.is_empty())
new_queue.add(4)
new_queue.add(4)
new_queue.add(4)
print(new_queue)
print(new_queue.is_empty())
if __name__ == "__main__":
# SequenceQueue_test()
LinkQueue_test()