akhayoon

Queue JavaScript

Sep 25th, 2021
1,086
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Queue {
  2.   constructor() {
  3.     this.items = {};
  4.     this.headIndex = 0;
  5.     this.tailIndex = 0;
  6.   }
  7.   enqueue(item) {
  8.     this.items[this.tailIndex] = item;
  9.     this.tailIndex++;
  10.   }
  11.   dequeue() {
  12.     const item = this.items[this.headIndex];
  13.     delete this.items[this.headIndex];
  14.     this.headIndex++;
  15.     return item;
  16.   }
  17.   peek() {
  18.     return this.items[this.headIndex];
  19.   }
  20.   get length() {
  21.     return this.tailIndex - this.headIndex;
  22.   }
  23. }
RAW Paste Data