Complete the code to add an element to the end of a queue.
queue.push([1]);To add an element to the end of a queue, use push(element).
Complete the code to remove the first element from a queue.
const first = queue.[1]();To remove the first element from a queue, use shift().
Fix the error in this stack pop operation code.
const top = stack.[1]();Stacks remove the top element using pop() in JavaScript.
Fill both blanks to implement a function that uses a stack to reverse a string.
function reverseString(str) {
const stack = [];
for (let char of str) {
stack.[1](char);
}
let reversed = '';
while (stack.length > 0) {
reversed += stack.[2]();
}
return reversed;
}To reverse a string using a stack, push each character, then pop to build reversed string.
Fill all three blanks to implement a queue using an array with enqueue and dequeue functions.
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.[1](element); } dequeue() { if (this.items.length === 0) return null; return this.items.[2](); } peek() { return this.items[[3]]; } }
Enqueue adds to the end with push, dequeue removes from front with shift, peek returns first element at index 0.
