Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created September 28, 2012 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyattdanger/3801085 to your computer and use it in GitHub Desktop.
Save wyattdanger/3801085 to your computer and use it in GitHub Desktop.
simple implementation, does not handle errors
/*
* 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