Stack Using Two Queues || Program 33 || Competitive Coding || Learning Monkey ||

Publicado em: 03 Março 2023
no canal de: Learning Monkey
7,723
126

Stack Using Two Queues
In this class, We discuss Stack Using Two Queues.
The reader can complete a competitive coding course to crack product development companies. Click Here.
The reader has to complete a placement training course for basic coding skills. Click Here.
Question:
Implement a stack using two queues.
We need to implement stack operations using two queues.
The stack works as last in, first out.
But queue works as first in, first out.
Using two queues, we must write push and pop functions that work as a stack.
Logic:
The step-by-step explanation is provided in the video.
Code:
queue_1=[]
queue_2=[]
def push(x):
global queue_1
global queue_2
queue_2.append(x)
while (len(queue_1)!=0):
queue_2.append(queue_1.pop(0))
queue_1,queue_2 = queue_2,queue_1

def pop():

global queue_1
global queue_2

code here
if len(queue_1)!=0:
return queue_1.pop(0)
else:
return -1

while(True):
x=int(input("enter your option 1. Push 2. Pop 3. quit"))
if(x==1):
y=int(input("enter the element to insert"))
push(y)
print("element inserted")
elif(x==2):
z=pop()
print(z)
elif(x==3):
break;
else:
print("wrong option")
Link for playlists:
   / @learningmonkey  


Link for our website: https://learningmonkey.in

Follow us on Facebook @   / learningmonkey  

Follow us on Instagram @   / learningmonkey1  

Follow us on Twitter @   / _learningmonkey  

Mail us @ learningmonkey01@gmail.com


Nesta página do site você pode assistir ao vídeo on-line Stack Using Two Queues || Program 33 || Competitive Coding || Learning Monkey || duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Learning Monkey 03 Março 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 7,723 vezes e gostou 126 espectadores. Boa visualização!