diff --git a/DataStructures/Queue.js b/DataStructures/Queue.js new file mode 100644 index 0000000..9f62202 --- /dev/null +++ b/DataStructures/Queue.js @@ -0,0 +1,50 @@ +export default class Queue { + constructor() { + this.queue = []; + this.front = null; + this.rear = null; + } + + enqueue(element) { + this.queue.push(element); + this.rear = element; + this.front = this.queue[0]; + } + + dequeue() { + if (this.queue.length > 0) { + if (this.queue.length === 1) { + this.front = null; + } else { + this.front = this.queue[this.queue.length - 2]; + } + return this.queue.shift(); + } else { + throw new Error("Queue Underflow Exception: The queue is empty."); + } + } + + getFront() { + if (this.queue.length === 0) { + return null; + } + + return this.front; + } + + getRear() { + if (this.queue.length === 0) { + return null; + } + + return this.Rear; + } + + size() { + return this.queue.length; + } + + isEmpty() { + return this.queue.length === 0; + } +}