Skip to content

Instantly share code, notes, and snippets.

@shreezan123
Created March 6, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shreezan123/6eb52af497f558c99fb0931d5f778f57 to your computer and use it in GitHub Desktop.
Save shreezan123/6eb52af497f558c99fb0931d5f778f57 to your computer and use it in GitHub Desktop.
Implement a queue using two stacks
class QueueUsingStacks:
def __init__(self):
self.stack1 = []
self.stack2 = []
def enqueue(self,value):
self.stack1.append(value)
#if empty, pop from stack1 and put each value in stack2.
def dequeue(self):
if len(self.stack2)==0:
while len(self.stack1)!=0:
self.stack2.append(self.stack1.pop())
print(self.stack2.pop())
#Print each of 2 stack's status
def returnstackstage(self):
print ("Stack 1", self.stack1)
print ("Stack 2", self.stack2)
q = QueueUsingStacks()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.dequeue()
q.dequeue()
q.dequeue()
q.returnstackstage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment