-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement-queue.py
More file actions
41 lines (32 loc) · 923 Bytes
/
Implement-queue.py
File metadata and controls
41 lines (32 loc) · 923 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
40
41
class MyQueue:
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, x: int) -> None:
if len(self.stack1) == 0:
self.stack1.append(x)
else:
for i in self.stack1[::-1]:
self.stack2.append(i)
self.stack1.clear()
self.stack1.append(x)
for i in self.stack2[::-1]:
self.stack1.append(i)
self.stack2.clear()
def pop(self) -> int:
e = self.stack1[-1]
self.stack1.pop()
return e
def peek(self) -> int:
return self.stack1[-1]
def empty(self) -> bool:
if len(self.stack1) == 0:
return True
else:
return False
# 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()