Created
May 10, 2021 23:51
-
-
Save josecarneiro/c2155badd670ca61707d87bdb0511628 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Queue | |
class Queue { | |
constructor() { | |
this.queueControl = []; | |
this.MAX_SIZE = 10; | |
} | |
display() { | |
return this.queueControl; | |
} | |
canEnqueue() { | |
return this.queueControl.length < this.MAX_SIZE; | |
} | |
isEmpty() { | |
return !this.queueControl.length; | |
} | |
enqueue(item) { | |
if (!this.canEnqueue()) throw new Error('QUEUE_OVERFLOW'); | |
this.queueControl.push(item); | |
return this.queueControl; | |
} | |
dequeue() { | |
if (this.isEmpty()) throw new Error('QUEUE_UNDERFLOW'); | |
const [value] = this.queueControl.splice(0, 1); | |
return value; | |
} | |
} | |
if (typeof module !== 'undefined') { | |
module.exports = Queue; | |
} | |
// Stack | |
class Stack { | |
constructor() { | |
this.stackControl = []; | |
this.MAX_SIZE = 10; | |
} | |
canPush() { | |
return this.stackControl.length < this.MAX_SIZE; | |
} | |
display() { | |
return this.stackControl; | |
} | |
isEmpty() { | |
return !this.stackControl.length; | |
} | |
push(item) { | |
if (!this.canPush()) throw new Error('STACK_OVERFLOW'); | |
this.stackControl.push(item); | |
return this.stackControl; | |
} | |
pop() { | |
if (this.isEmpty()) throw new Error('STACK_UNDERFLOW'); | |
return this.stackControl.pop(); | |
} | |
} | |
if (typeof module !== 'undefined') { | |
module.exports = Stack; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment