diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..9546f315 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,23 +5,36 @@ class Queue { } canEnqueue() { - // ... your code goes here + if (this.queueControl.length === this.MAX_SIZE) { + return false + } + return true } isEmpty() { - // ... your code goes here + if (this.queueControl.length > 0) { + return false + } + return true } 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..d33c7e17 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,23 +5,38 @@ class Stack { } canPush() { - // ... your code goes here + if (this.stackControl.length === this.MAX_SIZE) { + return false + } + return true } isEmpty() { - // ... your code goes here + if (this.stackControl.length > 0) { + return false + } + return true } 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 } }