Last active
December 4, 2018 16:14
-
-
Save kvedantmahajan/e558855d5a6ee7c88ee44e3385c9bc8c to your computer and use it in GitHub Desktop.
Implementation of Linked list in Javascript using OLOO ( Object linked with another object ) style of coding i.e. no "new" keyword and sugar coated classes
This file contains 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
/** | |
* Initialize your data structure here. | |
*/ | |
var MyLinkedList = { | |
head: null, | |
length: 0, | |
Node: function (val){ | |
return { | |
val, | |
next: null | |
} | |
} | |
}; | |
/** | |
* Get the value of the index-th node in the linked list. If the index is invalid, return -1. | |
* @param {number} index | |
* @return {number} | |
*/ | |
MyLinkedList.get = function(index) { | |
if(!this.length || index < 0 || index >= this.length) return -1; | |
var currentNode = this.head; | |
var count = 0; | |
while(count < index){ | |
count++; | |
currentNode = currentNode.next; | |
} | |
return currentNode.val; | |
}; | |
/** | |
* Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. | |
* @param {number} val | |
* @return {void} | |
*/ | |
MyLinkedList.addAtHead = function(val) { | |
var head = this.head; | |
this.head = { | |
val, | |
next: head | |
} | |
this.length++; | |
}; | |
/** | |
* Append a node of value val to the last element of the linked list. | |
* @param {number} val | |
* @return {void} | |
*/ | |
MyLinkedList.addAtTail = function(val) { | |
var node = this.Node(val); | |
if (this.head === null){ | |
this.head = node; | |
} else { | |
var currentNode = this.head; | |
while( currentNode.next){ | |
currentNode = currentNode.next; | |
} | |
currentNode.next = node; | |
} | |
this.length++; | |
}; | |
/** | |
* Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. | |
* @param {number} index | |
* @param {number} val | |
* @return {void} | |
*/ | |
MyLinkedList.addAtIndex = function(index, val) { | |
var node = this.Node(val); | |
var currentNode = this.head; | |
var previousNode; | |
var count = 0; | |
if(index > this.length) return false; | |
if(index === 0){ | |
this.head = node; | |
node.next = null; | |
} else { | |
while ( count < index){ | |
count++; | |
previousNode = currentNode; | |
currentNode = currentNode.next; | |
} | |
node.next = currentNode; | |
previousNode.next = node; | |
} | |
this.length++; | |
}; | |
/** | |
* Delete the index-th node in the linked list, if the index is valid. | |
* @param {number} index | |
* @return {void} | |
*/ | |
MyLinkedList.deleteAtIndex = function(index) { | |
var count = 0; | |
var currentNode = this.head; | |
var previousNode; | |
while ( count < index){ | |
count++; | |
previousNode = currentNode; | |
currentNode = currentNode.next; | |
} | |
previousNode.next = currentNode.next; | |
this.length--; | |
}; | |
var obj = Object.create(MyLinkedList); | |
var param_1 = obj.addAtTail(45); | |
console.log(param_1); | |
/** | |
* var param_1 = obj.get(index) | |
* obj.addAtHead(val) | |
* obj.addAtTail(val) | |
* obj.addAtIndex(index,val) | |
* obj.deleteAtIndex(index) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment