-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path232_Implement_Queue_using_Stacks.py
More file actions
43 lines (29 loc) · 1.02 KB
/
Copy path232_Implement_Queue_using_Stacks.py
File metadata and controls
43 lines (29 loc) · 1.02 KB
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
class MyQueue:
def __init__(self):
self.mainStack = []
self.secondaryStack = []
def push(self, x: int) -> None:
self.mainStack.append(x)
def pop(self) -> int:
while(len(self.mainStack) > 1):
self.secondaryStack.append(self.mainStack.pop())
popedItem = self.mainStack.pop()
while(len(self.secondaryStack) > 0):
self.mainStack.append(self.secondaryStack.pop())
return popedItem
def peek(self) -> int:
lastPopedItem = 0
while(len(self.mainStack) > 0):
lastPopedItem = self.mainStack.pop()
self.secondaryStack.append(lastPopedItem)
while(len(self.secondaryStack) > 0):
self.mainStack.append(self.secondaryStack.pop())
return lastPopedItem
def empty(self) -> bool:
return len(self.mainStack) == 0
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()