Created
May 2, 2017 00:16
Star
You must be signed in to star a gist
Quiz crear nodo JS- E.C
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
function List() { | |
var Node = function(element){ | |
this.element = element; | |
this.next = null; | |
}; | |
var length = 0; | |
var headNode = null; | |
this.insert = insert; | |
function insert(element, position) { | |
// verificar que las posiciones existan | |
if (position > -1 && position < length) { | |
var node = new Node(element); | |
var actualNode = headNode; | |
var antNode; | |
var index = 0; | |
//Ver el primer elemento | |
if (position === 0) { | |
node.next = actualNode; | |
headNode = node; | |
} else { | |
while (index++ < position) { | |
antNode = actualNode; | |
actualNode = actualNode.next; | |
} | |
node.next = actualNode; | |
antNode.next = node; | |
} | |
length++; | |
return true; | |
} | |
return false; | |
} | |
} | |
var list = new List(); | |
console.log('Insertamos el 10 en la posición 0', list.insert(10, 0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment