Data Structures
Stack

Stack

Initialize the stack

You can create a stack with the given size. Size is optional argument with the default value 65535.

import { Stack } from "quark-dsa";
 
const stack = new Stack(200); // Creates stack with size 200.

Supported methods

MethodDescriptionReturns
push(item)Pushes the item onto the top of the stack.void
pop()Removes the item at the top this stack and returns it.popped item
isEmpty()Checks if the stack is emptytrue if stack is empty otherwise false
peek()Returns the top item from the stack without removing ittop element of the stack
length()Returns the length of the stack. Length is different than size. Size is the maximum limit of the stack, while length is the count of items in the stack.length of the stack
search(item)Returns the 0-based position of the item in the stack.returns the position of the item in the stack. If item is absent, it will return -1

Usage

import { Stack } from "quark-dsa";
 
const stack = new Stack(); // new stack is created.
stack.push(23); // pushes 23 to the top of the stack.
stack.push(36); // pushes 36 to the top of the stack.
stack.length(); // returns 2;
stack.search(36); // returns 1;
stack.search(98); // returns -1;
stack.pop(); // removes top element from the stack and returns it.
stack.peek(); // returns the top element of the stack, 23.
stack.length(); // returns 1;
stack.pop(); // removes top element from the stack and returns it.
stack.isEmpty(); // returns true as stack is empty now.