UNPKG

@toolbox-ts/dsa

Version:
48 lines 1.57 kB
import { Structure } from "../core/index.js"; /** * Factory for creating a stack structure instance. * The stack is a singly linked list with a single 'head' anchor. * Provides push, pop, reset, and iteration methods. * * @template D - Data type stored in the stack nodes * @returns Stack API instance */ export const create = () => { const struct = Structure.create({ type: "stack", nodeManagerCfg: { anchorKeys: ["head"], type: "singly", primaryAnchorKey: "head", }, }); const api = Structure.extractPublicAPI(struct); const stack = { ...api, [Symbol.iterator]: () => Structure.genericDetailIterator(struct.anchors.primary, "next"), get head() { return struct.anchors.primary?.detail; }, reset: () => { struct.reset(); return stack; }, pop: () => struct.remove(struct.anchors.primary, (head) => { struct.anchors.set("head", head.next); return head.detail; }), push: (detail) => struct.add(detail, (newNode) => { const currHead = struct.anchors.primary; if (currHead) { struct.anchors.set("head", newNode); struct.node.setPointer(newNode, { next: currHead }); } else struct.anchors.set("head", newNode); return stack; }), top: () => struct.anchors.primary?.detail, }; return stack; }; //# sourceMappingURL=stack.js.map