-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyQueue.py
54 lines (37 loc) · 1.04 KB
/
MyQueue.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
class Myqueue:
def __init__(self):
'''
in 主要负责 push, out 主要负责 pop
'''
self.stack_in = []
self.stack_out = []
def push(self, x):
'''
有新元素进来,就往in 里面push
'''
self.stack_in.append(x)
def pop(self):
'''
remove the element from in front of queue and returns that element.
'''
if self.empty():
return None
if self.stack.out:
return self.stack_out.pop()
else:
for i in range(len(self.stack_in)):
self.stack_out.append(self.stack_in.pop())
return self.stack_out.pop()
# peek: 返回队列首部元素
def peek(self):
'''
Get the front element
'''
ans = self.pop()
self.stack_out.append(ans)
return ans
def empty(self):
'''
只有 in 或者 out 有元素,说明队列不为空
'''
return not (self.stack_in or self.stack_out)