Created
March 19, 2014 07:13
-
-
Save ionox0/9636845 to your computer and use it in GitHub Desktop.
A Java Script Linked List Class!
This file contains 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 LinkedList() { | |
this._length = 0; | |
this._head = null; | |
} | |
LinkedList.prototype = { | |
add: function (data){ | |
var node = { | |
data: data, | |
next: null | |
}, | |
current; | |
if (this._head === null){ | |
this._head = node; | |
} else { | |
current = this._head; | |
while(current.next){ | |
current = current.next; | |
} | |
current.next = node; | |
} | |
this._length++; | |
}, | |
push: function (data){ | |
this._head = { | |
data: data, | |
next: this._head | |
}; | |
this._length++; | |
}, | |
pop: function(pos){ | |
var current = this._head; | |
for (var i = 0; i < pos; i++){ | |
current = current.next; | |
} | |
var gotchya = current.data; | |
if (current.next){ | |
current.data = current.next.data; | |
current.next = current.next.next; | |
} else { | |
this._head = null; | |
} | |
this._length--; | |
return gotchya; | |
}, | |
isElement: function(value2){ | |
var current = this._head; | |
while (current != null){ | |
if (current.data == value2) | |
return true; | |
current = current.next; | |
} | |
return false; | |
}, | |
isEmpty: function(){ | |
if (this._head == null) | |
return true; | |
else | |
return false; | |
} | |
}; | |
{ data:1, next{ data:2, next{ data:3, next:null }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment