Listas enlazadas function listaEnlazada() { var Node = function(element){ this.element = element; this.next = null; }; var listSize = 0; var headNode = null; this.add = add; this.insert = insert; this.remove = remove; this.removeFrom = removeFrom; this.indexOf = indexOf; this.hasElements = hasElements; this.size = size; this.toString = toString; function add(element) { var node = new Node(element); var currentNode; if (!headNode) { headNode = node; } else { currentNode = headNode; while (currentNode.next) { currentNode = currentNode.next; }
currentNode.next = node; } listSize++; } function insert(element, pos) { if (pos > -1 && pos < listSize) { var node = new Node(element); var currentNode = headNode; var previousNode; var index = 0; if (pos == 0) { node.next = currentNode; headNode = node; } else { while (index++ < pos) { previousNode = currentNode; currentNode = currentNode.next; } node.next = currentNode; previousNode.next = node; } listSize++; return true; } return false; }
function remove(element) { var index = this.indexOf(element); return this.removeFrom(index); } function removeFrom(pos) { if (pos > -1 && pos < listSize) { var currentNode = headNode; var previousNode; var index = 0; if (pos === 0) { headNode = currentNode.next; } else { while (index++ < pos) { previousNode = currentNode; currentNode = currentNode.next; } previousNode.next = currentNode.next; } listSize--; return currentNode.element; } return null; } function indexOf(element) { var currentNode = headNode; var index = 0;
while (currentNode) { if (currentNode.element === element) { return index; } index++; currentNode = currentNode.next; } return -1; } function hasElements() { return listSize > 0; } function size() { return listSize; } function toString() { var currentNode = headNode; var str = "|"; while (currentNode) { str += currentNode.element + "|"; currentNode = currentNode.next; } return str; } }
var listaEnlazada = new listaEnlazada();
var Sistema="no";
while (Sistema="no") { listaEnlazada.add(parseInt(prompt("Digite los elemtos a ingresar en la Lista")));
Sistema=prompt("¿Quieres Terminar de ingresar datos? (si) o (no) ","") if (Sistema=="no"){ } else { {break;} } } console.log('Tamaño de la lista', listaEnlazada.size()); console.log('La lista contiene los siguientes elementos:', listaEnlazada.toString());
var s=parseInt(prompt("Digite el elemento que desea Eliminar"));
for (var i =0; i< listaEnlazada.size(); i++) { listaEnlazada.remove(s); listaEnlazada.remove(s); } console.log('Tamaño de la lista', listaEnlazada.size()); console.log('La lista es:', listaEnlazada.toString());