Created
July 18, 2016 15:58
-
-
Save davidejones/a43d32dc867ee49d15224de064dbc3f0 to your computer and use it in GitHub Desktop.
Javascript linkedlist test
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
| 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