JavaScript presentation of queue data structure
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
/** | |
* A queue can remove only the most recently added value, what if we need to remove old value. here comes another | |
* data structure QUEUE. | |
* queue is work on FIRST IN FIRST OUT | |
* | |
* queue is work on LAST IN FIRST OUT principle. | |
* queue is just like array with only capabilities of push and pop (method). | |
* [6] | |
* [5] | |
* [4] | |
* [3] | |
* [2] | |
* [1] ----> which is first element of queue | |
* @method | |
* enqueue: adds value to a queue return size of queue | |
* dequeue: removes value from queue and return removed value. | |
* | |
* own added methods. | |
* size: return size of queue. | |
* contains: return boolean value wether value exist in queue or not. | |
*/ | |
class Queue { | |
constructor() { | |
this._start = 0; | |
this._end = 0; // hold | |
this._storage = {}; | |
} | |
} | |
/** | |
* add element to stack. | |
* 1. increment the queue _end count. | |
* 2. to retain the order in which it was added, we use _end as key. | |
* @param value | |
* @return updated stack | |
*/ | |
Queue.prototype.enqueue = function(value) { | |
this._storage[this._end] = value; | |
this._end += 1; | |
return this.size(); | |
} | |
/** | |
* @return removed element | |
*/ | |
Queue.prototype.dequeue = function() { | |
var element = this._storage[this._start]; | |
delete this._storage[this._start]; | |
if (this._start < this._end) { | |
// When _start is equal to _end it means there are no elements in the queue. | |
// There is no need to increase _start if there are no elements. | |
// _start can't be greater than _end, that is why we only increment _start if _start is lower than _end. | |
// We make sure that _start is always pointing to a number equal or lower than _end. | |
this.start += 1; | |
} | |
return element; | |
} | |
/** | |
* @return last element of stack. | |
*/ | |
Queue.prototype.peek = function() { | |
return this._storage[this._start] | |
} | |
/** | |
* get stack | |
*/ | |
Queue.prototype.getQueue = function () { | |
return this._storage; | |
} | |
/** | |
* get count | |
*/ | |
Queue.prototype.size = function () { | |
return this._end; | |
} | |
/** | |
* loop until _end is smaller than to _start | |
* @param {*} value | |
* @returns boolean | |
*/ | |
Queue.prototype.contains = function(value) { | |
for (var i = this._start; i < this._end; i++) { | |
if (this._storage[i] === value) return true; | |
} | |
return false; | |
}; | |
let stack = new Queue(); | |
console.log(stack.getQueue()); | |
console.log(stack.enqueue(1)); | |
console.log(stack.enqueue(2)); | |
console.log(stack.dequeue()); | |
console.log(stack.enqueue(3)); | |
console.log(stack.enqueue([1, 2, 3, 4])); | |
console.log(stack.dequeue()) | |
console.log(stack.getQueue()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment