Skip to content

Commit c36e0c4

Browse files
committed
Added feature Stack Using Two Queues
1 parent 596d934 commit c36e0c4

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"githubPullRequests.ignoredPullRequestBranches": [
33
"master"
4-
]
4+
],
5+
"python.analysis.typeCheckingMode": "basic"
56
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class StackUsingQueues:
2+
def __init__(self):
3+
"""
4+
Initialising required queues using lists data structure
5+
"""
6+
self.queue1 = []
7+
self.queue2 = []
8+
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+
22+
"""
23+
returning the values from queue 2 to queue 1 so as
24+
to replicate the stack data structure
25+
"""
26+
while len(self.queue2)!= 0:
27+
self.queue1.append(self.queue2[0])
28+
self.queue2.pop(0)
29+
30+
31+
32+
33+
def pop(self):
34+
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+
popped = self.queue1[0]
43+
self.queue1.pop(0)
44+
return popped
45+
else:
46+
return None
47+
48+
49+
50+
51+
52+
53+
54+
def peek(self):
55+
"""
56+
Function to see the last value inserted in the stack
57+
implemented using queue here, which has the last
58+
value of the stack as its first.
59+
"""
60+
if len(self.queue1) != 0:
61+
return self.queue1[0]
62+
else:
63+
return None
64+
65+

0 commit comments

Comments
 (0)