StackArray()

This class is a Stack implementation based on JavaScript-Array

new StackArray()

Creates a STACK data-structure

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

Methods

clear()

Resets the STACK data-structure

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.push(15);

stack.clear(); // now stack is empty

isEmpty() → {Boolean}

Returns if the STACK data-structure is empty

Returns:
Boolean
Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.isEmpty(); // returns true;

stack.push(23);

stack.isEmpty(); //returns false;

pop()

Removes a element from the top of the STACK

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.push(3);
stack.push(9);

stack.pop(); //removes element 9

push(element)

Adds a element to the top of the STACK

Parameters:
Name Type Description
element * | Array

Element or Array passed to insert

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.push(5); //inserts 5 to the top of the stack
stack.push([8,63,2]); //inserts 8,63 and 2 to the top of the stack, in this order

size() → {Number}

Returns the size of the STACK data-structure

Returns:
Number -

The number of elements in the STACK

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.size(); //returns 0;

stack.push(8);

stack.size(); //returns 1;

top() → {*|undefined}

Returns the element on the top of the STACK

Returns:
* | undefined -

Returns the element or undefined if stack is empty

Example
const { StackArray } = require('data-structures-algorithms-js');
const stack = new StackArray();

stack.top();// returns undefined

stack.push(23);
stack.push(89);

stack.top(); //return 89