Implement Queue using JS
Steps: Queue has following methods
- Enqueue - Add element to the end of queue
- Dequeue - Remove element from the front of queue
- isEmpty - Check if queue is empty
- isFull - Check if queue is full
- Peek - Get the value from front of queue without removing it
class Queue {
constructor() {
this.items = {};
this.headIndex = 0;
this.tailIndex = 0;
}
enqueue(element) {
this.items[this.tailIndex] = element;
this.tailIndex++;
}
dequeue() {
let el = this.items[this.headIndex];
delete this.items[this.headIndex];
if (this.isEmpty()) {
this.headIndex = 0;
this.tailIndex = 0;
} else {
this.headIndex++;
}
return el;
}
peek() {
return this.items[this.headIndex];
}
isEmpty() {
return (this.tailIndex - this.headIndex === 0);
}
clear() {
this.items = {};
this.headIndex = 0;
this.tailIndex = 0;
}
}
let q = new Queue();
q.enqueue(20);
q.enqueue(4);
q.items;
q.dequeue();
q.items;
q.clear();
q.items;