Constructor
new Stack(value)
Parameters:
PropertiesName | Type | Description |
---|---|---|
value | Array | | The value to initialize the stack with (optional). |
Name | Type | Description |
---|---|---|
top | StackNode | The first node in the stack |
size | Number | The number of nodes in the stack |
- Source
Example
new Stack();
new Stack("Beep");
new Stack([10,20,30]);
Methods
clear() → {Stack}
Removes all values from the stack
- Source
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
- Source
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:
Name | Type | Default | Description |
---|---|---|---|
returnNode | Boolean | false | Whether to return the node or just the value. |
- Source
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:
Name | Type | Default | Description |
---|---|---|---|
returnNode | Boolean | false | Whether to return the node or just the value. |
- Source
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
- Source
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:
Name | Type | Description |
---|---|---|
value | Number | The value to add. |
- Source
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
- Source
Returns:
The number of nodes in the stack.
- Type:
- Number
Example
const s = new Stack([10,20,30]);
s.toArray(); // [10,20,30]