Created
September 28, 2012 17:18
-
-
Save wyattdanger/3801085 to your computer and use it in GitHub Desktop.
simple implementation, does not handle errors
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
/* | |
* LinkedList | |
* Very simple LinkedList implementation | |
*/ | |
function Node(item, next) { | |
this.item = item; | |
this.next = next; | |
} | |
function LinkedList() { | |
this.first = null; | |
} | |
LinkedList.prototype = { | |
push: function(item) { | |
this.first = new Node(item, this.first); | |
}, | |
pop: function() { | |
var previouslyFirst = this.first; | |
this.first = previouslyFirst.next; | |
return previouslyFirst.item; | |
} | |
}; | |
module.exports = LinkedList; | |
function main() { | |
var assert = require('assert'); | |
var list = new LinkedList(); | |
list.push(1); | |
list.push(2); | |
assert(list.pop() === 2); | |
list.push(3); | |
assert(list.pop() === 3); | |
assert(list.pop() === 1); | |
console.log("Tests pass."); | |
} | |
if (require.main === module) main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment