Basic abstract data types for JavaScript
Built by Audun Bjornerud Mo. @audun, audun.io
npm install abstract-data
var abs = require('abstract-data');
or clone the repo and put abstract.js
with your project and import with:
<script src="abstract.js"></script>
Current types:
Planned types:
- Binary trees
- Queues
- Dequeues
- Heaps
Stacks have five attributes: .length, .push(), .pop(), .pushAll(), .popAll(), and are instantiated as empty by:
Client-side
var abs = new AbstractJS();
myStack = new abs.Stack();
Node
var abs = require('abstract-data');
myStack = news abs.Stack();
Basics
Stack.push(item)
pushesitem
onto the stack.Stack.pop()
retrieves the last item to pushed, and removes it from the the stack.Stack.length
holds the length of the stack as a number
Advanced
Stack.pushAll(Object || Array input, string modifier, bool reverse)
Stack.pushAll()
takes a parameter input
that is either an array or a generic JS-object, and an optionalmodifier
given as a string. modifier
changes the manner in which input
is pushed onto the stack, if input
is an object. It is entirely optional.
Arrays
The default behavior for Stack.pushAll(array) is to push from left to right, that is array[0]
to array[array.length - 1]
. However, if the last parameter is true
, then this order is turned around.
Example:
var abs = new AbstractJS(),
myStack = new abs.Stack(),
myArray = [1,2, 'three', function(){return '4'}];
myStack.pushAll(myArray);
//results in a stack with order: 1,2, 'three', function(){return '4'}
myStack.popAll() //Empties the stack
myStack.pushAll(myArray, '', true)
//results in a stack with order: function(){return '4'}, 'three', 1,2
Objects
The default behavior for Stack.pushAll(object) is to push key-value pairs of the form {key : value}
. The modifier can here be used to only record keys, or only record values
Example:
var abs = new AbstractJS();
myStack = new abs.Stack(),
myObj = {
key_one : val_one,
key_two : vale_two,
key_three : val_three
};
myStack.pushAll(myObj);
//results in a stack like: {key_one : value_one}, {key_two : value_two}, {key_three : value_three}
myStack.popAll(); //Empties the stack
myStack.pushAll(myObj, 'keys');
//results in a stack like: key_one, key_two, key_three
myStack.popAll();
myStack.pushall(myObj, 'values');
//results in a stack like: value_one, value_two, value_three
Stack.popAll(callback(popped))
Stack.popAll()
takes an optional callback
. This callback is given the popped item as a parameter. The callback executes, and then repeats with the next object on the stack until the stack is empty. If no callback is specified, the stack will be emptied.
For example:
var abs = new AbstractJS();
executionStack = new abs.Stack(),
numbersToDouble = [1,2,3,4,5,6],
results = [];
executionStack.pushAll(numbersToDouble);
executionStack.popAll(function(popped){
results.append(popped * 2);
});
##Linked Lists
Linked lists consist of a set of nodes, each of which containing a reference
to the next node in the sequence. Instantiating an empty Linked List is done by:
```
var abs = new AbstractJS(),
LL = new abs.LinkedList();
```
####LLNodes
LLNodes are the constituent parts of a Linked List. They are instantiated, always with
a 'value' and 'next' property (undefined if this is the last node), like this:
```
var abs = new AbstractJS(),
LLNodeTwo = new abs.LLNode('this is my value', undefined),
LLNodeOne = new abs.LLNode('this is the first value', LLNodeTwo);
```
LLNodes can be added to a linked list manually by LinkedList.addNode(LLNode)
but there are also two other ways of adding LLNodes:
LinkedList.addAllNodes(startingNode)
This starts at the given node, then recursively adds startingNode.next, continuing
until it hits a node where node.next
is undefined
LinkedList.makeFromArray(array)
This loops over the given array, adding the elements as it goes. The elements
do not have to be LLNodes. Suppose array[i]
is not undefined.
If array[i+1]
is also not undefined, an LLNode is created with myLLNode.value = array[i]
and myLLNode.next = array[i+1]
, otherwise myLLNode.next = undefined
and the loop stops. This LLNode is then added to the Linked List.
Properties:
LinkedList.length
is a number, containing the length of the Linked List.
Built-in methods
LinkedList.iterateOver(callback(node))
iterates over all the nodes in the list, and executes a callback on them