Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 45180 / input.js
diff options
context:
space:
mode:
1 files changed, 51 insertions, 0 deletions
-rw-r--r--homework/45180/input.js51
diff --git a/homework/45180/input.js b/homework/45180/input.js
new file mode 100644
index 0000000..6974019
--- /dev/null
+++ b/homework/45180/input.js
@@ -0,0 +1,51 @@
+export default class Queue {
+ constructor() {
+ this._firstNode = null;
+ this._lastNode = null;
+ }
+
+ dequeue() {
+ if (!this._firstNode) {
+ return this._firstNode;
+ }
+
+ const payload = this._firstNode.payload;
+
+ this._firstNode = this._firstNode.next;
+
+ return payload;
+ }
+
+ 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;
+ }
+ }
+
+ clear() {
+ this._firstNode = null;
+ this._lastNode = null;
+ }
+
+ top() {
+ if (!this._firstNode) {
+ return this._firstNode;
+ }
+
+ return this._firstNode.payload;
+ }
+
+ isEmpty() {
+ return !this._firstNode;
+ }
+}