-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackUsing2Queue.py
55 lines (44 loc) · 1.06 KB
/
stackUsing2Queue.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
import queue
class Stack:
def __init__(self):
self.q1 = queue.Queue()
self.q2 = queue.Queue()
def push(self, ele):
self.q1.put(ele)
def pop(self):
if (self.size() == 0):
return "Stack is Empty!!"
while self.q1.qsize() != 1:
self.q2.put(self.q1.get())
ans = self.q1.get()
while not self.q2.empty():
self.q1.put(self.q2.get())
return ans
def top(self):
if (self.size() == 0):
return "Stack is Empty!!"
while self.q1.qsize() != 1:
self.q2.put(self.q1.get())
ans = self.q1.get()
while not self.q2.empty():
self.q1.put(self.q2.get())
self.q1.put(ans)
return ans
def size(self):
return self.q1.qsize()
def isEmpty(self):
return self.size() == 0
s = Stack()
s.push(10)
s.push(20)
s.push(30)
s.push(40)
s.push(50)
print(s.size())
print(s.isEmpty())
print(s.top())
while not s.isEmpty():
print(s.pop())
print(s.top())
print(s.isEmpty())
print(s.pop())