Complete the code to add an element to the queue using stack push operation.
def enqueue(self, x): self.stack_in.[1](x)
In Python, list's append method adds an element to the top of the stack.
Complete the code to check if the queue is empty by checking both stacks.
def is_empty(self): return len(self.stack_in) == 0 and len(self.stack_out) [1] 0
The queue is empty only if both stacks have zero length.
Fix the error in the dequeue method to correctly transfer elements from stack_in to stack_out.
def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.[1](self.stack_in.pop()) return self.stack_out.pop()
We use append to add elements to stack_out after popping from stack_in.
Fill both blanks to complete the peek method that returns the front element without removing it.
def peek(self): if not self.stack_out: while self.stack_in: self.stack_out.[1](self.stack_in.pop()) return self.stack_out[[2]]
We append elements from stack_in to stack_out, then return stack_out[-1] as the front of the queue.
Fill all three blanks to complete the full Queue class using two stacks.
class QueueUsingTwoStacks: def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, x): self.stack_in.[1](x) def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.[2](self.stack_in.pop()) return self.stack_out.pop() def is_empty(self): return len(self.stack_in) == 0 and len(self.stack_out) [3] 0
The enqueue method uses append to add elements to stack_in. The dequeue method transfers elements using append to stack_out. The is_empty method checks if both stacks have zero length using ==.