Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion js/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function List(head, tail) {

// Returns a new List containing the array.
List.list = function (array) {
var sentinel = new List(),
var sentinel = new List(), //
len = array.length,
p, i;

Expand All @@ -19,9 +19,27 @@ List.list = function (array) {
p.tail = new List(array[i]);
p = p.tail;
}
// console.log(sentinel.tail);
return sentinel.tail;
}

List.prototype.forEach = function(cb){
var L,i;
for (L = this,i=0; L !== null; L = L.tail,i++) {
cb(L.head,i)
}
}

List.prototype.push = function(item){
var L = this;
while(L.tail)
{
L = L.tail
}
L.tail = new List(item);
return this
}

// Returns a readable String for THIS.
List.prototype.toString = function () {
var res = '', L;
Expand All @@ -32,3 +50,4 @@ List.prototype.toString = function () {
res += ' ]';
return res;
};

Loading