JavaScript - linked list y node

 
Vista:

linked list y node

Publicado por Hugo Santiago Gil Giraldo (1 intervención) el 23/07/2021 19:29:27
Tengo el siguiente problema y no se como hacerlo estaria agradecido con la ayuda

// Implementa la clase LinkedList
// tiene metodos `add`, `remove`, y `search`
// add: Agrega un nuevo nodo en el final de la lista
// Ej: Head --> null
// add(1): Head --> 1 --> null
// add(2): Head --> 1 --> 2 --> null
// remove: Elimina el último nodo de la lista y devuelve su valor. (Tener en cuenta el caso particular de una lista de un solo nodo y de una lista vacía)
// Ej: Head --> 1
// remove(): Head --> null y devuelve 1
// search: Busca un valor dentro de la lista. Puede recibir un valor o una función. Si no hubiera resultados, devuelve null.

function LinkedList() {

}

function Node(value){

}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

linked list y node

Publicado por Marcos (2 intervenciones) el 30/09/2021 22:45:09
/*Hola ahí te dejo varias funciones para manejo de listas simples*/

function LinkedList() {
this.head = null;
this._length = 0;
}
function Node(value){
this.dato = value;
this.next = null;
}
//////////////////////////////////////////////////////////////////////////////////////
LinkedList.prototype.add = function(data){
let node = new Node(data);
//si lista vacia
if(this.head === null){
this.head = node;
}
else{//si ya tiene nodos
let actual = this.head;
while(actual.next){
actual = actual.next;
}
actual.next = node;
}
this._length++;
return node;
}
-------------------------------------------------------------------------------------
LinkedList.prototype.insertaHead=function(dato){
let nn=new Nodo(dato);
if(this.head===null){ this.head=nn;}
else{
nn.next=this.head;
this.head=nn;
}
this._length++;
}
-------------------------------------------------------------------------------------
LinkedList.prototype.insert = function(data,pos){
if(pos > this._length){ this.push(data); console.log("Se agregó al final!");}
else if( pos === 1){
insertaHead(data);
console.log("Se actualizó el head");
}
else{
var nn = new Node(data);
let actual=this.head;
let prev =null;
while(pos > 1){
pos--;
actual=actual.next;
}
prev = actual;
actual = actual.next;
nn.next = actual;
prev.next = nn;

}
this._length++;
}
--------------------------------------------------------------
LinkedList.prototype.muestrLista=function(){
if(this.head===null){console.log("Lista Vacia");}
else{
let actual=this.head;
while(actual.next){
console.log(actual);
actual=actual.next;
}
console.log(actual);
}
}
--------------------------------------------------------------
LinkedList.prototype.muestraXPos=function(pos){
if(pos > this._length){console.log("Fuera de rango");}
else{
let actual=this.head;
while(pos>1){
pos--;
actual=actual.next;
}
console.log("El elemt de la pos "+pos+ " es:",actual.dato);
}
}
----------------------------------------------------------------
LinkedList.prototype.ordenaInt = function(){
if(this.head === null){
console.log("Lista vacia");
}
let p = this.head;
let q = p.next;
let aux = null;
while(p.next !== null){
q = p.next;
while(q !== null){
if(q.dato < p.dato){
aux = p.dato;
p.dato = q.dato;
q.dato = aux;
}
q = q.next;
}
p = p.next;
}

}
----------------------------------------------------------------
LinkedList.prototype.elimUlti = function(){
let actual = this.head;
let axu = null;
//si está vacia
if(this.head === null){
console.log("Lista vacia");
}//si solo tengo head
else if(actual.next === null){
this.elimPri();
}//recorro hasta el ultimo, de a dos
else{
while(actual.next.next !== null){
actual=actual.next;
}
aux = actual.next.dato;
actual.next = null;
this._length--;
console.log("Elemnt elim:" +aux);
}

}
--------------------------------------------------------------------
LinkedList.prototype.deleteFirst = function(){
if (this._length === 0) return console.log('Es una lista vacia');
else{
let aux = this.head.dato;
this.head = this.head.next;
this._length--;
console.log("Elemnt elim: " +aux);
}
}
---------------------------------------------------------------------
LinkedList.prototype.elimDato = function(dato){
var actual = this.head;
var anterior = null;

//recorrer lista
while(actual != null){
//compara si existe el dato
if(actual.dato === dato){
//verifico si tengo q elim la head
if(!anterior){
let aux = actual.dato;
this.head = actual.next;
console.log("Elemnt Elim: " +aux)
}
else{//elim nodos sgts al
let aux = actual.dato;
anterior.next = actual.next;
console.log("Elemnt Elim: " +aux)
};
//achico lista
this.size --;

}

//si no encontré el dato, paso al sigt nodo
anterior = actual;
actual = actual.next;
};

}
-----------------------------------------------------------------------
LinkedList.prototype.eliminaPorIndice = function(indice){
var actual = this.head;
var anterior = null;

//verifico indice
if(indice < 0 || indice > this.size){
console.log("Fuera de la Lista");
}
//si tngo q elim cabeza
if(indice === 0 ){
this.head = actual.next;
}
else{
for(var i=0; i<indice;i++){
anterior = actual;
actual = actual.next;
};
anterior.next = actual.next;
};
this._length--;
return actual.data;
}
----------------------------------------------------------------------------------------
// search: Busca un valor dentro de la lista. Puede recibir un valor o una función.
// Si no hubiera resultados, devuelve null.---------------------------------------------
LinkedList.prototype.search = function (value){
if(this.head === null) return null;
let current = this.head;
while(current){
if(current.dato === value) return current.value;
else if(typeof (value) === 'function'){
if(value(current.dato)){
return current.dato;
}
}
current = current.next;
}
return null;
}
----------------------------------------------------------------
LinkedList.prototype.changeNotNumber = function(){
//metodo .isNsn
let actual = this.head;
let cont = 0;
while(actual !== null){
if(isNaN(Number(actual.dato))){//quiere decir q el value/dato no es trnsformable, se debe trans a kricocho
cont ++;
actual.dato = "kricocho";
}
actual = actual.next;
}
return cont;
}
///////////////////////////////////////
var lista=new LinkedList();
lista.add(14);
lista.add(13);
lista.add(15);
console.log("----------------------------------------");
lista.muestrLista();
lista.insert(11,2);
lista.muestraXPos(1);
console.log("----------------------------------------");
lista.ordenaInt();
lista.muestrLista();
console.log("----------------------------------------")
lista.elimUlti();
lista.muestrLista();
console.log("----------------------------------------")
lista.deleteFirst();
lista.muestrLista();
console.log("----------------------------------------")
lista.elimDato(13);
lista.muestrLista();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar