@kartikkhk/stl-node
Version:
Standard Data Structures for Node JS
49 lines (48 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class Stack {
constructor(stack) {
this._arr = [];
if (Array.isArray(stack))
this._arr = Array.from(stack);
}
push(value) {
this._arr.push(value);
}
// removes top element from stack or returns undefined
pop() {
return this._arr.pop();
}
// Returns the element on the top of the stack, but does not remove it.
peek() {
if (this.isEmpty())
return undefined;
return this._arr[this._arr.length - 1];
}
// checks if stack is empty
isEmpty() {
return this._arr.length === 0;
}
size() {
return this._arr.length;
}
array() {
return this._arr;
}
clear() {
this._arr = [];
}
// searches and checks if an object is in stack
search(obj) {
for (const val of this._arr) {
if (val === obj)
return true;
}
return false;
}
// creates a stack from an array
static fromArray(array) {
return new Stack(array);
}
}
exports.default = Stack;