-
Notifications
You must be signed in to change notification settings - Fork 1
/
implement_stack.py
72 lines (59 loc) · 1.39 KB
/
implement_stack.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
#coding=utf-8
"""
Implement Stack using Queues
"""
from collections import deque
class MyStack(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.value = deque([])
def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
self.value.append(x)
def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
q1 = deque([])
while len(self.value) > 1:
q1.append(self.value.popleft())
top = self.value.popleft()
self.value = q1
return top
def top(self):
"""
Get the top element.
:rtype: int
"""
q1 = deque([])
while len(self.value) > 1:
q1.append(self.value.popleft())
top = self.value[0]
q1.append(self.value.popleft())
self.value = q1
return top
def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
if self.value:
return False
return True
# Your MyStack object will be instantiated and called as such:
obj = MyStack()
#obj.push(3)
#obj.push(4)
#param_2 = obj.pop()
#obj.pop()
#param_3 = obj.top()
param_4 = obj.empty()
print param_4
#print param_2, param_3, param_4