Java Linked List search and delete method

后端 未结 3 1855
谎友^
谎友^ 2020-12-07 00:05

I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need

3条回答
  •  借酒劲吻你
    2020-12-07 00:31

    public void delete (Magazine mag) {
        MagazineNode current = this.list;
        MagazineNode before;
    
        //if is the first element
        if (current.equals(mag)) {
            this.list = current.next;
            return;     //ending the method
        }
    
    
        before = current;
    
        //while there are elements in the list
        while ((current = current.next) != null) {
    
            //if is the current element
            if (current.equals(mag)) {
                before.next = current.next;
                return;     //endind the method 
            }
    
            before = current;
        }
    
        //it isnt in the list
    }
    

    the comments should explains whats happening

提交回复
热议问题