232. Implement Queue using Stacks

class MyQueue:def __init__(self):
self.stk1=[]
self.stk2=[]

def push(self, x: int) -> None:
self.stk1.append(x)

def pop(self) -> int:
while(self.stk1):
self.stk2.append(self.stk1.pop(-1))
front = self.stk2.pop(-1)
while(self.stk2):
self.stk1.append(self.stk2.pop(-1))

return front
def peek(self) -> int:
while(self.stk1):
self.stk2.append(self.stk1.pop(-1))
front = self.stk2[-1]
while(self.stk2):
self.stk1.append(self.stk2.pop(-1))

return front
def empty(self) -> bool:
if not self.stk1:
return True
return False
class MyQueue:def __init__(self):
self.stk1=[]
self.stk2=[]

def push(self, x: int) -> None:
self.stk1.append(x)

def pop(self) -> int:
if not self.stk2:
while(self.stk1):
self.stk2.append(self.stk1.pop(-1))
front = self.stk2.pop(-1)
return front
def peek(self) -> int:
if not self.stk2:
while(self.stk1):
self.stk2.append(self.stk1.pop(-1))
front = self.stk2[-1]
return front
def empty(self) -> bool:
if not self.stk1 and not self.stk2:
return True
return False

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.