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.
# Initializing two stacksstack_in,stack_out=[],[]# Dealing with each line of instructions, seperating it to 3 instructions as explainedforiinrange(int(input())):curr_inst=list(map(int,input().split(" ")))op=curr_inst[0]#1ifop==1:stack_in.append(curr_inst[1])#2ifop==2:# If stack_out is not empty then pop from itifstack_out:stack_out.pop()continueelse:# Move everything to the out stack:whilestack_in:stack_out.append(stack_in.pop())stack_out.pop()continue#3ifop==3:ifnotstack_out:print(stack_in[0])else:print(stack_out[-1])
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
My Python solution: