Last active
August 29, 2015 14:27
-
-
Save pbse/626ce1b7f55764a07874 to your computer and use it in GitHub Desktop.
LinkedList in JavaScript
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 Node,LinkedList,obj,newobj; | |
| Node = function (item) { | |
| this.item = item; | |
| this.next = null; | |
| }; | |
| LinkedList = function () { | |
| this.head = new Node('head'); | |
| this.size = 0; | |
| }; | |
| LinkedList.prototype.find = function(data) { | |
| var cur_node = this.head; | |
| while (cur_node !== null && cur_node.item !== data) { | |
| cur_node = cur_node.next; | |
| } | |
| return cur_node; | |
| }; | |
| LinkedList.prototype.position = function(data) { | |
| var cur_node = this.head; | |
| var i = 0; | |
| while (cur_node !== null && cur_node.item !== data) { | |
| cur_node = cur_node.next; | |
| i+=1; | |
| } | |
| if(cur_node===null) { | |
| console.log('Node Not Found'); | |
| return false; | |
| } | |
| return i; | |
| }; | |
| LinkedList.prototype.insert = function(data) { | |
| if(this.find(data) === null) { | |
| var cur_node = this.head; | |
| while (cur_node.next !== null) { | |
| cur_node = cur_node.next; | |
| } | |
| var new_node = new Node(data); | |
| new_node.next = cur_node.next; | |
| cur_node.next = new_node; | |
| this.size += 1; | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| }; | |
| LinkedList.prototype.show = function() { | |
| var cur_node = this.head; | |
| while (cur_node.next !== null) { | |
| console.log(cur_node.next.item); | |
| cur_node = cur_node.next; | |
| } | |
| }; | |
| LinkedList.prototype.getPrevious = function(data) { | |
| var cur_node = this.head; | |
| while (cur_node !== null && cur_node.next.item !== data) { | |
| cur_node = cur_node.next; | |
| } | |
| return cur_node; | |
| }; | |
| LinkedList.prototype.remove = function(data) { | |
| var prev_node = this.getPrevious(data); | |
| if (prev_node.next !== null) { | |
| prev_node.next = prev_node.next.next; | |
| this.size -= 1; | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| }; | |
| LinkedList.prototype.union = function(NewList) { | |
| if(NewList instanceof LinkedList === false) { | |
| console.log("Wrong Parameter passed"); | |
| return false; | |
| } | |
| var cur_node = this.head; | |
| while (cur_node.next !== null) { | |
| cur_node = cur_node.next; | |
| } | |
| var NewListHead = NewList.head; | |
| cur_node.next = NewListHead.next; | |
| this.size += NewList.size; | |
| NewListHead.next = null; | |
| NewList.size = 0; | |
| }; | |
| obj = new LinkedList(); | |
| obj.insert('x'); | |
| obj.insert('y'); | |
| obj.insert('z'); | |
| newob = new LinkedList(); | |
| newobj.insert('Insert any object here'); | |
| obj.union(newobj); | |
| obj.show(); // Logs all elements | |
| newobj.show(); // Logs null element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment