Created
January 19, 2016 23:01
-
-
Save mediaupstream/1f606099956a37ff8c9e to your computer and use it in GitHub Desktop.
Algorithms in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node(data, next){ | |
this.data = data; | |
this.next = next; | |
} | |
function List(){ | |
this.start = null; | |
this.end = null; | |
// add a node to the list | |
this.append = function(data){ | |
var node = new Node(data); | |
if (this.start === null){ | |
this.start = node; | |
this.end = this.start; | |
} else { | |
this.end.next = node; | |
this.end = this.end.next; | |
} | |
} | |
// iterate over the list, calling `cb` for each node | |
this.walk = function(cb, iter){ | |
var iter = iter || this.start; | |
cb(iter.data, iter.next); | |
if (iter.next){ | |
this.walk(cb, iter.next) | |
} | |
} | |
// find the nth item in the list | |
this.nth = function(n){ | |
function find(i, iter){ | |
if (i == n) return iter; | |
if (iter.next) { | |
i++; | |
return find(i, iter.next); | |
} | |
return; | |
} | |
return find(0, this.start); | |
} | |
} | |
// TEST IT | |
var list = new List(); | |
list.append("one") | |
list.append("two") | |
list.append("three") | |
list.append("four") | |
list.append("five") | |
list.append("six") | |
list.walk(function(data, next){ | |
console.log(data, '->', (next) ? next.data : 'undefined'); | |
}); | |
console.log("---") | |
console.log( (list.nth(0)).data ); // 'one' | |
console.log( (list.nth(4)).data ); // 'five' | |
console.log( list.nth(34) ); // undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FIFO queue (first in first out) | |
// | |
var q = []; | |
function enqueue(item, queue){ | |
queue[queue.length] = item | |
return queue; | |
} | |
function dequeue(queue){ | |
var ret = []; | |
for(var i=1; i<queue.length; i++){ | |
ret[i-1] = queue[i]; | |
} | |
return ret; | |
} | |
// using builtin methods | |
function enqueue(item, queue){ | |
queue.push(item) | |
return queue; | |
} | |
function dequeue(queue){ | |
queue.shift() | |
return queue; | |
} | |
q = enqueue(1, q); | |
q = enqueue(2, q); | |
q = enqueue(3, q); | |
q = enqueue(4, q); | |
if (String(q) == '1,2,3,4') console.log('true'); | |
q = dequeue(q) | |
q = dequeue(q) | |
q = dequeue(q) | |
if (String(q) == '4') console.log('true'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LIFO stack (last in first out) | |
// | |
var s = []; | |
function push(item, stack){ | |
stack[stack.length] = item; | |
return stack; | |
} | |
function pop(stack){ | |
var ret = [] | |
for(var i=0; i<stack.length-1; i++){ | |
ret[i] = stack[i] | |
} | |
return ret; | |
} | |
// using builtin methods | |
function push(item, stack){ | |
stack.push(item) | |
return stack | |
} | |
function pop(stack){ | |
stack.pop() | |
return stack; | |
} | |
s = push(1, s) | |
s = push(2, s) | |
s = push(3, s) | |
s = push(4, s) | |
if (String(s) == '1,2,3,4') console.log('true'); | |
s = pop(s) | |
s = pop(s) | |
s = pop(s) | |
if (String(s) == '1') console.log('true'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
queue.js and stack.js are both functional programming and don't mutate state. Also they both have manual methods and methods that use builtin javascript functions like pop, push, shift.