UNPKG

dastal

Version:

Data Structures & Algorithms implementations

47 lines 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkedStack = void 0; const list_1 = require("../list"); /** * A linked list implementation of the {@link Stack} interface */ class LinkedStack { /** * Instantiate the stack. * * @param elements - A set of elements to initialize the stack with. */ constructor(elements) { this.list = new list_1.LinkedList(); for (const element of elements || []) { this.push(element); } } clear() { this.list.clear(); } peek() { return this.list.get(0); } pop() { return this.list.shift(); } push(element) { return this.list.unshift(element); } get size() { return this.list.size; } /** * 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]() { return this.list[Symbol.iterator](); } } exports.LinkedStack = LinkedStack; //# sourceMappingURL=linkedStack.js.map