UNPKG

quarkle

Version:

quarkle is the JavaScript util library providing support of all data types and data structures.

88 lines (87 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stack = void 0; const utils_1 = require("../../utils"); class Stack { constructor(size) { if (size != null && size < 0) { throw new Error("Stack size cannot be negative!!"); } this.size = size !== null && size !== void 0 ? size : 2 ** 16 - 1; this.array = new Array(size).fill(undefined); this.top = -1; } /** * Checks if the stack is empty. * @returns `true` if the stack is empty, otherwise `false`. */ isEmpty() { return this.top === -1; } /** * Checks if the stack is full. * @returns `true` is the stack is full, otherwise `false`. */ isFull() { return this.top === this.size - 1; } /** * Pushes new item in a stack. It will throw an error if stack is full. * @param item * @returns void */ push(item) { if (this.isFull()) { throw new Error("Stack is full, cannot push new items!!"); } else { this.top++; this.array[this.top] = item; } } /** * Removes the top element from the stack. * @returns The popped item. */ pop() { if (this.isEmpty()) { throw new Error("Stack is empty, no items can be popped!!"); } else { const item = this.array[this.top]; this.array[this.top] = undefined; this.top--; return item; } } /** * Returns the top element in the stack. * @returns the top element. */ peek() { return this.array[this.top]; } /** * Returns the total elements count in the stack. * @returns The length of the stack. */ length() { return this.top + 1; } /** * Searches for an item in the stack and returns its index. If item is not present, it will return `-1`. * @param item * @returns The index of matched item, `-1` if item is not present in the stack. */ search(item) { let i = 0; while (i <= this.top) { if ((0, utils_1.isEqual)(this.array[i], item)) { return i; } i++; } return -1; } } exports.Stack = Stack;