-
Notifications
You must be signed in to change notification settings - Fork 0
/
053.py
65 lines (47 loc) · 1.57 KB
/
053.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
"""
Problem:
Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, first-out)
data structure with the following methods: enqueue, which inserts an element into the
queue, and dequeue, which removes it.
"""
from DataStructures.Stack import Stack
class Queue:
def __init__(self) -> None:
self.stack1 = Stack()
self.stack2 = Stack()
def __str__(self) -> str:
return str(self.stack2[::-1] + self.stack1[::])
def enqueue(self, val: int) -> None:
self._transfer_to_stack1()
self.stack1.push(val)
def dequeue(self) -> int:
self._transfer_to_stack2()
if len(self.stack2) == 0:
raise RuntimeError("Cannot dequeue from a empty queue")
return self.stack2.pop()
def _transfer_to_stack2(self) -> None:
# helper function to transfer all items to the stack 1 from stack 2
while not self.stack1.is_empty():
self.stack2.push(self.stack1.pop())
def _transfer_to_stack1(self) -> None:
# helper function to transfer all items to the stack 2 from stack 1
while not self.stack2.is_empty():
self.stack1.push(self.stack1.pop())
if __name__ == "__main__":
queue = Queue()
print(queue)
queue.enqueue(1)
queue.enqueue(5)
queue.enqueue(9)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(0)
print(queue)
print(queue.dequeue())
print(queue.dequeue())
print(queue)
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
print(queue)