Algorithms and data structures for JS/TS
  • Introduction
  • GETTING STARTED
    • TS
    • Node.js
    • ES Modules
    • Browsers
  • PUBLIC API
    • How to read
    • Linked lists
      • SinglyLinkedList
      • DoublyLinkedList
      • CircularlyLinkedList
    • Stacks and queues
      • LinkedStack
      • LinkedQueue
      • LinkedDeque
      • CircularQueue
      • CircularArrayBuffer
      • UnsortedPriorityQueue
      • SortedPriorityQueue
      • AdaptableHeapPriorityQueue
    • Maps
      • SortedMap
      • MaximaSet
      • AVLTreeMap
      • SplayTreeMap
      • RedBlackTreeMap
    • Trees
      • GeneralTree
      • LinkedBinaryTree
      • PreorderTreeTraversal
      • InorderTreeTraversal
      • PostorderTreeTraversal
      • EulerTourTreeTraversal
    • Searches
      • Binary search
      • Quick select
    • Text processing
      • Longest common subsequence
      • Boyer-Moore
      • Knuth-Morris-Pratt
    • Position
    • Locator
    • Comparators
  • CONTRIBUTION NOTES
    • How to contribute
    • Project structure
  • Changelog
Powered by GitBook
On this page
  • new LinkedStack()
  • length
  • clear()
  • isEmpty()
  • pop()
  • push()
  • top()

Was this helpful?

  1. PUBLIC API
  2. Stacks and queues

LinkedStack

Container of elements that are inserted and removed according to the LIFO principle. This structure is based on SinglyLinkedList.

new LinkedStack()

/**
 * Creates an instance of LinkedStack.
 *
 * @param elements List of elements to create the new stack with.
 */
constructor(elements: T[] = [])

length

length: number

Number of elements in the stack. This field is read only.

Examples:

import { LinkedStack } from 'ads-js/queues';

const stack = new LinkedStack();

stack.length === 0; // true
stack.push(1);
stack.length === 1; // true

clear()

/**
 * Clears the stack.
 */
clear(): void;

isEmpty()

isEmpty(): boolean

Running time O(1)

Checks whether the stack is empty or not.

Returns:

TRUE if the stack is empty, FALSE otherwise.

Examples:

import { LinkedStack } from 'ads-js/queues';

const stack = new LinkedStack();

stack.isEmpty(); // true
stack.push(1);
stack.isEmpty(); // false

pop()

/**
 * Removes the first element from the top of the stack and returns it.
 * Throws an error if the stack is empty.
 *
 * @returns Removed element.
 */
pop(): T;

push()

/**
 * Adds element at the top of the stack.
 *
 * @param element Element to add.
 */
push(element: T): void;

top()

/**
 * Gets element from the top of the stack without its removal.
 *
 * @returns Stack element.
 */
top(): T;

PreviousStacks and queuesNextLinkedQueue

Last updated 5 years ago

Was this helpful?