Name | Modified | Size | Downloads / Week |
---|---|---|---|
README.md | 2012-10-29 | 3.6 kB | |
core_data_structures-1.0.min.js | 2012-10-29 | 1.6 kB | |
LICENSE | 2012-10-29 | 1.1 kB | |
core_data_structures-1.0.js | 2012-10-29 | 5.0 kB | |
Totals: 4 Items | 11.3 kB | 0 |
Essential Data structures in Javascript
Collection of essential computer science data structures in Javascript. Plus some additional nifty datastructures coming in upcoming versions.
Contains basic data structures
-
Node (aliases UnaryNode) A simple node structure consisting of
value
, and link tonext
item.var a= new Node();
var b= new Node();
var c= new Node();
a.value(1);
b.value(2);
a.next(b);
b.next(null); // [Optional] This is by default
// Now traverse the nodes using their links
var t= a; // Start with first node
while(t!== null) {
alert(t.value());
t= t.next();
}
Calling
value()
without any parameters acts like a getter to read values
Callingnext()
behaves in the same way -
BinaryNode
Extension toNode
giving access toprevious
-
TernaryNode (alias
TreeNode
)
Similar toBinaryNode
exceptnext
is referred to asright
, andprevious
is referred to asleft
Plus some advanced data structures
-
Stack (aliases
LinkedList
,LIFOQueue
)
Stack is the most basic form of data structure that usesNode
and provides a list into which items arepush
ed on top, andpop
ed off from top.
push
,pop
happens on same end providing you with a simple LIFO (Last In First Out) list of items. At any time, you can useisEmpty
to check is list is empty.
Alternatively, you can also use read-onlysize
to determine how many items are currently in the list. -
Queue (aliases
FIFOQueue
)
Queue is another basic form of data structure representing a list into which items can bepush
ed on one end, andpop
ed off the other end. This gives you a nice list of items that can be queued for LIFO (Last In First Out) processing.
Similar toStack
, bothisEmpty
andsize
are available. -
Dequeue (aliases
Deque
,Deck
)
A double-ended queue is an advanced data structure which allowspush
,pop
from whichever end you want to. This advanced data structure provides you with a list into which you canpushFirst
, orpushLast
and you canpopFirst
, orpopLast
.
To handle complex operations, at anytime you can checkisEmpty
, orsize
to determine items in the list for processing.
No external dependencies on any other libraries. To use in your page,
<script type="text/javascript" language="javascript" src="core_data_structures-1.0.js"></script>
A minified version is available too in case you choose to
<script type="text/javascript" language="javascript" src="core_data_structures-1.0.min.js"></script>
Aliases allow you to use user-friendly names to represent these data structures. Few intuitive aliases are already provided. In Javascript, everything is an object, hence you can store & retrieve any type of Javascript object onto the Stack
, or Queue
.
For advanced ease of use, some synonyms are allowed within these data structures as below
shift
is synonymous to push
on a Stack
, Queue
and their aliases.
unshift
is synonymous to pop
on a Stack,
Queueand their aliases.
enqueueis synonymous to
pushon a
Queueand it's aliases.
dequeueis synonymous to
popon a
Queue` and it's aliases.
Please feel free to use, suggest, augment as long as you honor the LICENSE.