-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_using_queues.py
75 lines (63 loc) · 1.83 KB
/
stack_using_queues.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
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue1 = []
self.queue2 = []
def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
self.queue1.append(x)
def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
if len(self.queue1) >= 1:
while len(self.queue1) > 1:
head = self.queue1[0]
del self.queue1[0]
self.queue2.append(head)
res = self.queue1[0]
del self.queue1[0]
self.queue1, self.queue2 = self.queue2, self.queue1
return res
def top(self):
"""
Get the top element.
:rtype: int
"""
if len(self.queue1) >= 1:
while len(self.queue1) > 1:
head = self.queue1[0]
del self.queue1[0]
self.queue2.append(head)
res = self.queue1.pop()
#-------------至此 queue1 是空的----------------
self.queue2.append(res)
# 交换,保证 queue2 是空的
self.queue1, self.queue2 = self.queue2, self.queue1
return res
def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return len(self.queue1) == 0
# Your MyStack object will be instantiated and called as such:
obj = MyStack()
obj.push(2)
obj.push(1)
print(obj.queue1)
print(obj.queue2)
obj.top()
print(obj.queue1)
print(obj.queue2)
param_2 = obj.pop()
param_3 = obj.pop()
param_4 = obj.empty()
print(param_4)