Created
January 18, 2023 20:47
-
-
Save 1travelintexan/2859f9cf54ee3ad76a34b0caba6d0a73 to your computer and use it in GitHub Desktop.
This file contains 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
class Queue { | |
constructor() { | |
this.queueControl = []; | |
this.MAX_SIZE = 10; | |
} | |
canEnqueue() { | |
if (this.queueControl.length === this.MAX_SIZE) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
isEmpty() { | |
if (this.queueControl.length === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
enqueue(item) { | |
if (this.canEnqueue()) { | |
this.queueControl.push(item); | |
return this.queueControl; | |
} else { | |
throw new Error("QUEUE_OVERFLOW"); | |
} | |
} | |
dequeue() { | |
if (!this.isEmpty()) { | |
return this.queueControl.shift(); | |
} else { | |
throw new Error("QUEUE_UNDERFLOW"); | |
} | |
} | |
display() { | |
return this.queueControl; | |
} | |
} | |
// This is required to enable the automated tests, please ignore it. | |
if (typeof module !== "undefined") module.exports = Queue; |
This file contains 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
class Stack { | |
constructor() { | |
this.stackControl = []; | |
this.MAX_SIZE = 10; | |
} | |
canPush() { | |
if (this.stackControl.length === this.MAX_SIZE) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
isEmpty() { | |
if (this.stackControl.length === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
push(item) { | |
if (this.canPush()) { | |
this.stackControl.push(item); | |
return this.stackControl; | |
} else { | |
throw new Error("STACK_OVERFLOW"); | |
} | |
} | |
pop() { | |
if (!this.isEmpty()) { | |
return this.stackControl.pop(); | |
} else { | |
throw new Error("STACK_UNDERFLOW"); | |
} | |
} | |
display() { | |
return this.stackControl; | |
} | |
} | |
// This is required to enable the automated tests, please ignore it. | |
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