Skip to content

Instantly share code, notes, and snippets.

@brecabal
Created May 2, 2017 00:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save brecabal/ee689bf6d545676f474cf46c12047117 to your computer and use it in GitHub Desktop.
Quiz crear nodo JS- E.C
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