0

Hola estoy intentando armar esta un método para borrar un dato en una linked list, ya implemente los métodos de agregar por índice y agregar al principio, pero con el de borrar vengo peleando hace un rato largo, creo que la base empezaria asi, pero no se como seguir, si alguien me podría ayudar se lo agradecería mucho.

class Node {
    // Accept arguments (the second one could be optional)
    constructor(data, next=null) {
        this.data = data; 
        this.next = next;
    }
    lastNode() { // new method that uses recursion
        return this.next?.lastNode() || this;
    }
}
class ListRecurse {
    constructor() {
        this.head = null;
        this.size = 0;
    }
    add(data) {
    let newNode = new Node(data); // No second argument. It has a default value
    if (this.head === null) {
        this.head = newNode;
    } else {
        // The lastNode implementation uses recursion:
        this.head.lastNode().next = newNode;
    }
    this.size ++;
    return this; // to allow chaining
    }
    DeletexData(data) {
        let current = this.head;
        let prev = null;
        if (current != null) {
            if (current.data === data && prev === null) {
                this.head = current.next;
            } else {
                this.head.next = current.next
            }
            this.head.lastNode(data).next 

            }
     return null;
     }
}

1 respuesta 1

0

Yo lo haría de esta manera

  • Primero inicias un ciclo para buscar nodo por nodo el dato
  • En cada paso preguntas si el dato es el buscado
  • Si se encuentra te preguntas en que posición se encuentra el nodo (Cabeza, cola o medio)
  • realizas el proceso correspondiente de cada posición
    • Cabeza: asiganas directamente a head su campos siguiente
    • Cola: asignas al previo en su campo siguiente null
    • Medio: asignas al previo el campo siguiente del nodo actual
    DeletexData(data) {
        let current = this.head;
        let prev = null;
    
        //while not finished the list
        while (current) {
            //found node
            if (current.data === data) {
                //is the head
                if (current === this.head) this.head = this.head.next;
    
                //is the tail
                else if (!current.next) prev.next = null;
            
                //are in the middle
                else prev.next = current.next;
                    
                this.size--;
                return true;
            }
            prev = current;
            current = current.next;
        }
        return false;
    }

Y recomendaría que agregaras un clasico método print, para realizar pruebas rapidas

print() {
    let current = this.head;

    while (current) {
        console.log(current.data);
        current = current.next;
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.