new Stack()
Creates a STACK data-structure
Example
const { Stack } = require('data-structures-algorithms-js');
const stack = new Stack();
Methods
-
clear()
-
Resets the STACK data-structure
Example
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); stack.push(15); stack.clear(); // now stack is empty
-
isEmpty() → {Boolean}
-
Returns if the STACK data-structure is empty
Returns:
BooleanExample
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); stack.isEmpty(); // returns true; stack.push(23); stack.isEmpty(); //returns false;
-
pop()
-
Removes a element from the top of the STACK
Example
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); stack.push(3); stack.push(9); stack.pop(); //removes number 9
-
push(element)
-
Adds a element to the top of the STACK
Parameters:
Name Type Description element
* Element passed to insert
Example
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); stack.push(5); //inserts 5 to the top of the stack
-
size() → {Number}
-
Returns the size of the STACK data-structure
Returns:
Number -The number of elements in the STACK
Example
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); 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 on the top or undefined if stack is empty
Example
const { Stack } = require('data-structures-algorithms-js'); const stack = new Stack(); stack.top();// returns undefined stack.push(23); stack.push(89); stack.top(); //returns 89