diff options
1 files changed, 71 insertions, 0 deletions
| -rw-r--r-- | homework/45183/CodeFormatting/input2.js | 71 |
diff --git a/homework/45183/CodeFormatting/input2.js b/homework/45183/CodeFormatting/input2.js new file mode 100644 index 0000000..c1c377c --- /dev/null +++ b/homework/45183/CodeFormatting/input2.js @@ -0,0 +1,71 @@ +export default class Queue { + constructor() { + this._firstNode = null; + this._lastNode = null; + } + + /** + * Retrieves the first element in the queue. + * It also removes it from the queue. + * @returns {*} + */ + dequeue() { + if (!this._firstNode) { + return this._firstNode; + } + + const payload = this._firstNode.payload; + + this._firstNode = this._firstNode.next; + + return payload; + } + + /** + * Adds an item to the queue + * @param {*} item + */ + enqueue(item) { + const newNode = { + payload: item, + next: null + }; + + if (this._firstNode === null) { + this._firstNode = newNode; + this._lastNode = newNode; + + } else { + this._lastNode.next = newNode; + this._lastNode = newNode; + } + } + + /** + * Empties the queue + */ + clear() { + this._firstNode = null; + this._lastNode = null; + } + + /** + * Gets the first element of the queue + * @returns {*} + */ + top() { + if (!this._firstNode) { + return this._firstNode; + } + + return this._firstNode.payload; + } + + /** + * Checks whether the queue is empty or not + * @returns {boolean} + */ + isEmpty() { + return !this._firstNode; + } +}
\ No newline at end of file |
