A stack is a linear data structure that follows the Last In First Out (LIFO)
principle. Elements are added and removed from the same end, called the top.
Common operations include push (add an element), pop (remove the top element),
and peek (view the top element without removing it). Stacks are used in various
applications like function call management, expression evaluation, and
backtracking.
Time complexity for access and search is Linear, i.e. O(n)
Time complexity for insertion and deletion is Constant, i.e. O(1)
Space complexity is Linear
/**
* @file Stack
* @author Dustin Boston
* @see Cormen, T, Leiserson, C, Rivest, R, and Stein, C. (2001).
* Introduction to Algorithms. (2nd ed). The MIT Press.
*//** Implements a basic stack data structure with push and pop operations. */exportclassStack<T>{privatestack: T[];privatetop: number;/**
* Constructs a stack instance.
*
* @param array - An array to initialize the stack with.
*/constructor(array: T[]=[]){this.stack=array;this.top=-1;}/**
* Checks if the stack is empty. O(1) time.
*
* @returns True if the stack is empty, false otherwise.
*/stackEmpty():boolean{returnthis.top===-1;}/**
* Adds an element to the top of the stack. O(1) time.
*
* @param element - The element to push onto the stack.
*/push(element: T):void{this.top+=1;this.stack[this.top]=element;}/**
* Removes and returns the element at the top of the stack in O(1) time.
*
* @returns The element removed from the top of the stack.
* @throws If the stack is empty (underflow).
*/pop():T{if(!this.stackEmpty()){this.top-=1;returnthis.stack[this.top+1];}thrownewError("Cannot pop from an empty stack.");}}(()=>{constarray=Array.from({length: 6},()=>0);conststack=newStack<number>(array);stack.push(4);if(array[0]!==4){thrownewError(`Expected 4 but got ${array[0]}`);}console.log(`Pushed 4 to the stack.`);constvalue=stack.pop();if(value!==4){thrownewError(`Expected value of 4 but got ${array[0]}`);}console.log(`Popped ${value} from the stack.`);})();