EXERCISE · AlgoCasts

Queue from stacks

Implement a Queue datastructure using two stacks. *Do not* create an array inside of the 'Queue' class. Queue should implement the methods 'add', 'remove', and 'peek'.

5 testsAlgoCastsGPL-3.0exercise 17 of 26

Directions

Directions

Implement a Queue datastructure using two stacks. *Do not* create an array inside of the 'Queue' class. Queue should implement the methods 'add', 'remove', and 'peek'. For a reminder on what each method does, look back at the Queue exercise.

Examples#

JS
  const q = new Queue();  q.add(1);  q.add(2);  q.peek();  // returns 1  q.remove(); // returns 1  q.remove(); // returns 2

Try it first#

Copy the stub, make the 5 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/qfroms/index.js
const Stack = require('./stack'); class Queue {} module.exports = Queue;

See also