|
1 | 1 | class StackUsingQueues:
|
2 |
| - def __init__(self): |
| 2 | + def __init__(self) -> None: |
3 | 3 | """
|
4 |
| - Initialising required queues using lists data structure |
| 4 | + Initialize the stack using two queues. |
| 5 | +
|
| 6 | + >>> stack = StackUsingQueues() |
5 | 7 | """
|
6 | 8 | self.queue1 = []
|
7 | 9 | self.queue2 = []
|
8 | 10 |
|
9 |
| - def push(self,item): |
10 |
| - while len(self.queue1)!= 0: |
11 |
| - """ |
12 |
| - enqueuing queue 2 using the values from queue 1 bringing them out in First |
13 |
| - In First Out (F.I.F.O) order. |
14 |
| - """ |
15 |
| - self.queue2.append(self.queue1[0]) |
16 |
| - self.queue1 .pop(0) |
17 |
| - """ |
18 |
| - adding the new value to queue 1 |
19 |
| - """ |
20 |
| - self.queue1.append(item) |
21 |
| - |
| 11 | + def push(self, item) -> None: |
22 | 12 | """
|
23 |
| - returning the values from queue 2 to queue 1 so as |
24 |
| - to replicate the stack data structure |
| 13 | + Push an item onto the stack. |
| 14 | +
|
| 15 | + >>> stack = StackUsingQueues() |
| 16 | + >>> stack.push(1) |
| 17 | + >>> stack.push(2) |
| 18 | + >>> stack.push(3) |
| 19 | + >>> stack.peek() |
| 20 | + 3 |
25 | 21 | """
|
26 |
| - while len(self.queue2)!= 0: |
| 22 | + while len(self.queue1) != 0: |
| 23 | + self.queue2.append(self.queue1[0]) |
| 24 | + self.queue1.pop(0) |
| 25 | + self.queue1.append(item) |
| 26 | + while len(self.queue2) != 0: |
27 | 27 | self.queue1.append(self.queue2[0])
|
28 | 28 | self.queue2.pop(0)
|
29 | 29 |
|
| 30 | + def pop(self) -> int: |
| 31 | + """ |
| 32 | + Pop the top item from the stack and return it. |
30 | 33 |
|
31 |
| - |
32 |
| - |
33 |
| - def pop(self): |
| 34 | + >>> stack = StackUsingQueues() |
| 35 | + >>> stack.push(1) |
| 36 | + >>> stack.push(2) |
| 37 | + >>> stack.pop() |
| 38 | + 2 |
| 39 | + >>> stack.pop() |
| 40 | + 1 |
| 41 | + >>> stack.pop() |
| 42 | + 0 |
| 43 | + """ |
34 | 44 | if len(self.queue1) != 0:
|
35 |
| - """ |
36 |
| - The first value of queue 1 is being popped here as it |
37 |
| - has been implemented in a way that the |
38 |
| - first value of the queue which gets popped is the last value of the stack. |
39 |
| - And since stack follows Last In First Out (L.I.F.O) order the |
40 |
| - following has been implemented. |
41 |
| - """ |
42 | 45 | popped = self.queue1.pop(0)
|
43 | 46 | return popped
|
44 | 47 | else:
|
45 |
| - return None |
46 |
| - |
47 |
| - |
48 |
| - |
| 48 | + return 0 |
49 | 49 |
|
50 |
| - |
51 |
| - |
52 |
| - |
53 |
| - def peek(self): |
| 50 | + def peek(self) -> int: |
54 | 51 | """
|
55 |
| - Function to see the last value inserted in the stack |
56 |
| - implemented using queue here, which has the last |
57 |
| - value of the stack as its first. |
| 52 | + Return the top item from the stack without removing it. |
| 53 | +
|
| 54 | + >>> stack = StackUsingQueues() |
| 55 | + >>> stack.push(1) |
| 56 | + >>> stack.push(2) |
| 57 | + >>> stack.peek() |
| 58 | + 2 |
| 59 | + >>> stack.pop() |
| 60 | + 2 |
| 61 | + >>> stack.peek() |
| 62 | + 1 |
| 63 | + >>> stack.pop() |
| 64 | + 1 |
| 65 | + >>> stack.peek() |
| 66 | + 0 |
58 | 67 | """
|
59 | 68 | if len(self.queue1) != 0:
|
60 | 69 | return self.queue1[0]
|
61 | 70 | else:
|
62 |
| - return None |
| 71 | + return 0 |
63 | 72 |
|
64 | 73 |
|
0 commit comments