dastal
Version:
Data Structures & Algorithms implementations
44 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayStack = void 0;
/**
* An implementation of the {@link Stack} interface using an array
*/
class ArrayStack {
/**
* Instantiate the stack.
*
* @param elements - A set of elements to initialize the stack with.
*/
constructor(elements) {
this.array = elements ? Array.from(elements) : [];
}
clear() {
this.array.length = 0;
}
peek() {
return this.array[this.array.length - 1];
}
pop() {
return this.array.pop();
}
push(element) {
return this.array.push(element);
}
get size() {
return this.array.length;
}
/**
* Receive an iterator through the stack.
*
* **Note:** Unexpected behavior can occur if the collection is modified during iteration.
*
* @returns An iterator through the stack
*/
*[Symbol.iterator]() {
const array = this.array;
for (let i = array.length; i > 0; yield array[--i]) { }
}
}
exports.ArrayStack = ArrayStack;
//# sourceMappingURL=arrayStack.js.map