We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my Python3 implementation using a class I defined.
Python3
# I created a class for a queue using two stacksclassqueueUsingTwoStacks:def__init__(self):self.orderedStack=[]self.reversedStack=[]defreverseAndPop(self):if(notself.reversedStack):while(self.orderedStack):self.reversedStack.append(self.orderedStack.pop())if(self.reversedStack):returnself.reversedStack.pop()returnNonedefenqueue(self,data):self.orderedStack.append(data)defdequeue(self):returnself.reverseAndPop()deffrontOfQueue(self):front=self.reverseAndPop()print(front)self.reversedStack.append(front)q=queueUsingTwoStacks()for_inrange(int(input())):cmd=input()ifcmd[0]is"1":q.enqueue(int(cmd[2:]))elifcmd[0]is"2":q.dequeue()elifcmd[0]is"3":q.frontOfQueue()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
Here's my Python3 implementation using a class I defined.
Python3