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;
Last updated
Was this helpful?