-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathQueueWith2StacksImple.py
More file actions
57 lines (51 loc) · 1.47 KB
/
Copy pathQueueWith2StacksImple.py
File metadata and controls
57 lines (51 loc) · 1.47 KB
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
# Queue with 2 stack implementation
# Author: Pradeep K. Pant, ppant@cpan.org
# Problem Statement: Given the Stack class below, implement a Queue class using two stacks!
# create a class
class QueueWith2Stack(object):
# Initialize class
def __init__(self):
# initialize two empty stack using python list which we have to use to implement queue
self.stack1 = []
self.stack2 = []
# Append a element
def enqueue(self,element):
# add a element in the queue
print ("Appending",+element)
self.stack1.append(element)
return True
# take the element out
def dequeue(self):
# remove a element from the queue
# Check for empty
if not self.stack2:
while self.stack1:
# Append in second stack the valu of pop from first list
# reverse order continuous popping of the element
self.stack2.append(self.stack1.pop())
print ("Poping element")
return self.stack2.pop()
# Test set 1
# Create a object of the class
qObj = QueueWith2Stack()
# Add an element
qObj.enqueue(1)
# Add another element
qObj.enqueue(2)
# Add more element
qObj.enqueue(4)
# Add more element
qObj.enqueue(8)
# Remove item
print (qObj.dequeue())
# Remove item
print (qObj.dequeue())
# Remove item
print (qObj.dequeue())
# Remove item
print (qObj.dequeue())
# Test set 2
for i in range(5):
qObj.enqueue(i)
for i in range(5):
print (qObj.dequeue())