add JavaScript code for Stacks (untested)

This commit is contained in:
buzz-lightsnack-2007 2024-08-28 14:42:41 +08:00
parent d72bdc4ccc
commit b4fdb3a797

55
src/javascript/stack.js Normal file
View file

@ -0,0 +1,55 @@
/*
IPseudoADS Stack
Pseudocode-like advanced stack management for JavaScript
by buzz-ligthsnack-2007
*/
class Stack {
#contents = [];
#max;
/*
Create a new stack.
@param {int} size stacks size
*/
constructor(size) {
if (size ? size > 0 : false) {
this.#max = size;
};
};
/*
Add an item to the top of the stack.
@param {Object} data the data to be added
*/
push(data) {
if ((this.#max != null) ? this.#max == this.#contents.length : false) {
throw new RangeError();
} else {
this.#contents.push(data);
}
};
/*
Remove and return the object at the top of the stack.
@return {Object} the data removed
*/
pop() {
return(this.#contents.pop());
};
/*
Test: stack contains no elements
@return {bool} test result
*/
isEmpty() {
return(this.#contents.length == 0);
}
}
export {Stack as default};