Skip to content

Instantly share code, notes, and snippets.

@1travelintexan
Created January 18, 2023 20:47
Show Gist options
  • Save 1travelintexan/2859f9cf54ee3ad76a34b0caba6d0a73 to your computer and use it in GitHub Desktop.
Save 1travelintexan/2859f9cf54ee3ad76a34b0caba6d0a73 to your computer and use it in GitHub Desktop.
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;
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