Skip to content

Instantly share code, notes, and snippets.

@davidejones
Created July 18, 2016 15:58
Show Gist options
  • Select an option

  • Save davidejones/a43d32dc867ee49d15224de064dbc3f0 to your computer and use it in GitHub Desktop.

Select an option

Save davidejones/a43d32dc867ee49d15224de064dbc3f0 to your computer and use it in GitHub Desktop.
Javascript linkedlist test
var LinkedList = function() {
this.head = null;
this.tail = null;
var Node = function(data) {
this.data = data;
this.next = null;
};
this.push = function(data) {
var tmpnode = new Node(data);
if(this.head == null){
// we have no head this must be first entry, make head
this.head = tmpnode;
} else if(this.tail == null) {
// we have no tail so lets make this node it
this.tail = tmpnode;
this.head.next = this.tail;
} else {
// we are adding to end
this.tail.next = tmpnode;
this.tail = tmpnode;
}
};
this.pop = function() {
var currentnode = this.head;
while(currentnode != null) {
if(currentnode.next == this.tail) {
this.tail = currentnode;
currentnode.next = null;
}
currentnode = currentnode.next;
}
};
this.debug = function() {
var currentnode = this.head;
while(currentnode != null) {
console.log(currentnode.data);
currentnode = currentnode.next;
}
};
};
var ll = new LinkedList();
ll.push("A");
ll.push("B");
ll.push("C");
ll.debug();
ll.pop();
ll.debug();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment