-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adressing issue Yet-Another-Series#103 - Added js-queue.js
- Loading branch information
Prasanna Gnanaraj
committed
Oct 27, 2019
1 parent
752a851
commit a798f01
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class Queue { | ||
constructor(...elements) { | ||
// State to hold the queue elements | ||
this._elements = []; | ||
// Init the queue using the initial arguments | ||
this.enqueue(...elements); | ||
} | ||
/** | ||
* @function enqueue | ||
* @param {any} elements: the items to enqueue | ||
*/ | ||
enqueue(...elements) { | ||
this._elements = [...this._elements, ...elements]; | ||
} | ||
/** | ||
* @function dequeue | ||
* @return {any}, Dequeues an item and returns it. If the queue is empty, returns null | ||
*/ | ||
dequeue() { | ||
return this.empty() ? null :this._elements.shift(); | ||
} | ||
/** | ||
* @function keyFront | ||
* @return {any} ,Returns the first element in the queue. If the queue is empty, returns null | ||
*/ | ||
keyFront() { | ||
return this.empty() ? null : this._elements[0]; | ||
} | ||
/** | ||
* @function empty | ||
* @return {Boolean}, true if the queue is empty, and false otherwise | ||
*/ | ||
empty() { | ||
return this._elements.length === 0; | ||
} | ||
/** | ||
* @function disp | ||
* @return {None} Displays the elements in the queue | ||
*/ | ||
disp() { | ||
console.log(this._elements); | ||
} | ||
/** | ||
* @function getLength | ||
* @return {Number} Returns the number of elements in the queue | ||
*/ | ||
getLength() { | ||
return this._elements.length; | ||
} | ||
} |