You are viewing a single comment's thread. Return to all comments →
import os
class MyQueue:
def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, num): # print(f"enqueue {num=}") self.stack_in.append(num) def dequeue(self): # print("dequeue") if self.stack_out: return self.stack_out.pop() else: while self.stack_in: self.stack_out.append(self.stack_in.pop()) return self.stack_out.pop() def front(self): # print("front") if self.stack_out: return self.stack_out[-1] elif self.stack_in: return self.stack_in[0] def exec(self, cmd, num=None): # print(f"{cmd=} {num=}") if cmd == '1': self.enqueue(num) elif cmd == '2': self.dequeue() elif cmd == '3': print(self.front())
if name == 'main': queue = MyQueue() n = input() queries = [] for _ in range(int(n)): inp = input().split() # print(f"{inp=}") queue.exec(*inp)
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 →
import os
class MyQueue:
if name == 'main': queue = MyQueue() n = input() queries = [] for _ in range(int(n)): inp = input().split() # print(f"{inp=}") queue.exec(*inp)