From c4a7941636bc73fb60a3f786f88ba91f5ae65575 Mon Sep 17 00:00:00 2001 From: guzegui Date: Sat, 3 Feb 2024 08:59:53 +0100 Subject: [PATCH] Done lab --- src/queue/queue-data-structure.js | 17 ++++++++++++----- src/stack/stack-data-structure.js | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..d09018ad 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,23 +5,30 @@ class Queue { } canEnqueue() { - // ... your code goes here + return this.queueControl.length < this.MAX_SIZE } isEmpty() { - // ... your code goes here + return this.queueControl.length === 0; } enqueue(item) { - // ... your code goes here + if (!this.canEnqueue()) { + throw new Error("QUEUE_OVERFLOW"); + } + this.queueControl.push(item); + return this.queueControl; } dequeue() { - // ... your code goes here + if (this.isEmpty()) { + throw new Error("QUEUE_UNDERFLOW"); + } + return this.queueControl.shift(); } 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..c5c2095d 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,25 +5,32 @@ class Stack { } canPush() { - // ... your code goes here + return this.stackControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return this.stackControl.length === 0; } push(item) { - // ... your code goes here + if (!this.canPush()) { + throw new Error("STACK_OVERFLOW"); + } + this.stackControl.push(item); + return this.stackControl; } pop() { - // ... your code goes here + if (this.isEmpty()) { + throw new Error("STACK_UNDERFLOW"); + } + return this.stackControl.pop(); } 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;