-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9_2stack2queue.py
More file actions
39 lines (30 loc) · 851 Bytes
/
9_2stack2queue.py
File metadata and controls
39 lines (30 loc) · 851 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
'''
用两个栈实现队列
'''
class stack:
def __init__(self):
self.stack = []
def append(self,x):
self.stack.append(x)
def pop(self):
self.stack.pop()
class Solution:
def __init__(self):
self.stack1 = stack().stack#本体
self.stack2 = stack().stack
def in_queue(self,input_node):
self.stack1.append(input_node)
def del_queue(self):
while(self.stack1):#全部倒在2
self.stack2.append(self.stack1.pop())
self.del_node = self.stack2.pop()
while(self.stack2):
self.stack1.append(self.stack2.pop())
return self.del_node
if __name__ == '__main__':
x = Solution()
x.in_queue(1)
x.in_queue(2)
x.in_queue(3)
print(x.del_queue())
print(x.stack1)