From 43212147936048a7c024506fc5d1cf4ce8c0efe6 Mon Sep 17 00:00:00 2001 From: Lisa Durdun Date: Wed, 25 Oct 2023 23:11:13 +0200 Subject: [PATCH] Iteration 1 and 2 without visualization --- src/queue/queue-data-structure.js | 25 ++++++++++++++++++++----- src/stack/stack-data-structure.js | 26 +++++++++++++++++++------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..cddebb3f 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,23 +5,38 @@ class Queue { } canEnqueue() { - // ... your code goes here + if (this.queueControl.length < this.MAX_SIZE) { + return true; + } else { + return false; + } } isEmpty() { - // ... your code goes here + if (this.queueControl.length === 0) { + return true; + } else { + return false; + } } enqueue(item) { - // ... your code goes here + if (this.canEnqueue() === true) { + this.queueControl.push(item); + return this.queueControl; + } else { throw new Error("QUEUE_OVERFLOW"); + } } dequeue() { - // ... your code goes here + if (this.isEmpty() === false) { + return this.queueControl.shift(); + } else { throw new Error("QUEUE_UNDERFLOW"); + } } display() { - // ... your code goes here + return this.queueControl; } } diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..ef0bf566 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,25 +5,37 @@ class Stack { } canPush() { - // ... your code goes here + if (this.stackControl.length < this.MAX_SIZE) { + return true; + } else return false; } isEmpty() { - // ... your code goes here + if (this.stackControl.length === 0) { + return true; + } else return false; } push(item) { - // ... your code goes here + if (this.canPush() === true) { + this.stackControl.push(item); + return this.stackControl; + } else { throw new Error("STACK_OVERFLOW"); + } } pop() { - // ... your code goes here + if (this.isEmpty() === false) { + return this.stackControl.pop(); + } else { + throw new Error("STACK_UNDERFLOW"); + } } display() { - // ... your code goes here - } + return this.stackControl; + } } // This is required to enable the automated tests, please ignore it. -if (typeof module !== 'undefined') module.exports = Stack; +if (typeof module !== "undefined") module.exports = Stack;