Sobes.tech
Junior — Senior

Implementation of a queue using two stacks

livecode

Task condition

It is necessary to create a data structure — a queue implemented using two stacks, which will serve as internal storage. The operations of adding and removing elements should work according to the FIFO principle.


 class Stack {
  constructor() {
    this.storage = [];
  }

  push(data) {
    this.storage.push(data);
  }

  pop() {
    return this.storage.pop();
  }

  size() {
    return this.storage.length;
  }
}

class Queue {
  constructor() {
    this.stack1 = new Stack();
    this.stack2 = new Stack();
  }

  // Add to queue
  enqueue(data)