Stack

Represents a Stack data structure

Constructor

new Stack(value)

Parameters:
NameTypeDescription
valueArray | *

The value to initialize the stack with (optional).

Properties
NameTypeDescription
topStackNode

The first node in the stack

sizeNumber

The number of nodes in the stack

Example
new Stack();
new Stack("Beep");
new Stack([10,20,30]);

Methods

clear() → {Stack}

Removes all values from the stack

Returns:

The current Stack instance.

Type: 
Stack
Example
const s = new Stack([10,20,30]);
s.clear(); // []

isEmpty() → {Boolean}

Checks if the stack is empty

Returns:

Whether or not the stack is empty.

Type: 
Boolean
Example
const s = new Stack();
s.isEmpty(); // true

peek(returnNode) → {*|StackNode}

Returns the first value in the stack

Parameters:
NameTypeDefaultDescription
returnNodeBooleanfalse

Whether to return the node or just the value.

Returns:

The value in the first node.

Type: 
* | StackNode
Example
const s = new Stack([10,20,30]);
s.peek(); // 10

pop(returnNode) → {Stack|*}

Removes the top value from the stack

Parameters:
NameTypeDefaultDescription
returnNodeBooleanfalse

Whether to return the node or just the value.

Returns:

The current Stack instance.

Type: 
Stack | *
Example
const s = new Stack([10,20,30]);
s.pop(); // [10,20]

print() → {Stack}

Prints the values in the stack

Returns:

The current Stack instance.

Type: 
Stack
Example
const s = new Stack([10,20,30]);
s.print(); // 10,20,30

push(value) → {Stack}

Adds a new value to the top of the stack

Parameters:
NameTypeDescription
valueNumber

The value to add.

Returns:

The current Stack instance.

Type: 
Stack
Example
const s = new Stack([10,20]);
s.push(30); // [10,20,30]

toArray() → {Number}

Returns the number of nodes in the stack

Returns:

The number of nodes in the stack.

Type: 
Number
Example
const s = new Stack([10,20,30]);
s.toArray(); // [10,20,30]