Constructor
new Stack()
In computer science, a stack is an abstract data type that serves as a
collection of elements, with two principal operations:
push, which adds an element to the collection, and
pop, which removes the most recently added element that was not yet removed.
The order in which elements come off a stack gives rise to its alternative name,
LIFO (last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.
Full wikipedia article at:
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Example
const Stack = require('dstructures').Stack;
const myStack = new Stack();
Methods
clear()
Deletes all elements in a stack.
Example
['Cat', 'Dog'] Stack.clear(); // []
length() → {Number}
Returns the length of a stack.
Returns:
- Type:
-
Number
Returns the length of a stack.
Example
[] Stack.length(); // 0
['Cat'] Stack.length(); // 1
peek() → {Boolean|Void}
Returns the topmost element of a stack.
Returns:
- Type:
-
Boolean
|Void
Returns the topmost element if the stack is not empty. Otherwise returns falsse.
Example
['Cat', 'Dog'] Stack.peek(); // 'Dog'
['Pig','Cat'] Stack.peek(); // 'Cat'
pop() → {Boolean|Any}
Removes and returns the topmost element.
Returns:
- Type:
-
Boolean
|Any
Returns the removed element if the stack is not empty. Otherwise returns false.
Example
['Cat', 'Dog', 'Deer'] Stack.pop(); // 'Deer'
['Cat', 'Dog'] Stack.pop(); // 'Dog'
push(element) → {Boolean|Void}
Adds element in a stack.
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Given element. |
Returns:
- Type:
-
Boolean
|Void
Returns fasle if the given element is undefined.
Example
[] Stack.push('Cat'); // ['Cat']
['Cat'] Stack.push('Dog'); // ['Cat', 'Dog']