Skip to Main Content

Implement Queue using Stacks

Problem URL:Implement Queue using Stacks

My Solution

JavaScript / TypeScript

class MyQueue {
  data: number[];

  constructor() {
    this.data = [];
  }

  push(x: number): void {
    this.data.push(x);
  }

  pop(): number {
    return this.data.shift();
  }

  peek(): number {
    return this.data[0];
  }

  empty(): boolean {
    return this.data.length === 0;
  }
}

Let's Connect

Twitter GitHub LinkedIn