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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Confessions of a Kindle Convert

How to Choose the Best DevOps Tools

Serverless CRUD API using AWS Lambda, DynamoDB, API Gateway and Node.JS

Windows application Test Automation (Part 1)

Clone of Pepperfry.com

Register Page

Breaking layouts in Rows and Columns in Flutter

INTRODUCTION TO BIOPYTHON

Customised Radio Buttons in Blazor WASM .Net 5

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.

More from Medium

How to secure your GraphQL API (Part 2, the Typetta way)

Generating a GraphQL API from a PostgreSQL schema using PostGraphile

Taager’s Foray in Messaging Part 2; Installing the Pulsar Node.js client on macOS

How To Store And Retrieve RedisJSON on Redis Cloud using Node.js