From 1384d7ea4e80eaa6b195195af81de974575b2bb0 Mon Sep 17 00:00:00 2001 From: omalinov Date: Sun, 21 Oct 2018 17:36:32 +0300 Subject: Lexer/homework (#15) * Initial commit * code is formatted * Homework finished * Genie project generation. * Updated code with new constructors --- homework/45183/CodeFormatting/input2.js | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 homework/45183/CodeFormatting/input2.js (limited to 'homework/45183/CodeFormatting/input2.js') 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 -- cgit v1.3.1-23-gd82d