简体   繁体   中英

Why don't LinkedList's removeAll() and removeFirstOccurrence() remove the node?

I have to write code of these method. It is a homework, mustn't change the methods' params, or write an other method.

The contains() and isEmpty() work correctly. The removeFirst() and removerLast() work well too.

The removeFirstOccurence() doesn't remove the first ouccurence of specified element. And the removeAll() doesn't revove the all of specified element.

/**
 * Removes the first occurrence of the specified element in this list (when
 * traversing the list from head to tail).   * 
 * @param value  element to be removed from this list, if present
 * @return {@code true} if the list contained the specified element
 */
public boolean removeFirstOccurrence(int value) {
    if(!contains(value))
        return false;
    else{
        boolean result = false;
        Node current = head;
        while ((current != null) && !result) {
            if (current.value == value){
                current=current.next;
                size--;
                return true;
                }
            current = current.next;
        }
        return result;
    }
}
/**
 * Removes all occurrences of the specified element from this list.
 * @param value the element to remove
 * @return {@code false} if nothing changed, otherwise {@code true}
 */
public boolean removeAll(int value) {
    if(isEmpty())
        return false;
    else{
        boolean result = false;
        Node current = head;
        while ((current.next != null) && !result) {
            if (current.value == value){
                current=current.next;
                size--;
                result=true;
            }                   
            current= current.next;
        }
        return result;
    }
}

Here the first part of MyLinkedList class:

public class MyLinkedList {

private class Node {

    private int value;
    private Node next;

    private Node(int value) {
        this.value = value;
        this.next = null;
    }

    @Override
    public String toString() {
        ...
    }
}

private Node head;
private int size;

// and the methods...

you never alter any of the nodes in any way

public boolean removeFirstOccurrence(int value) {
    //don't do contains(), you are looping over the entire thing anyway doing it again is useless
    if(head==null)return false;
    if(head.value==value){//does head contain the value 
        head=head.next;
        size--;
        return true;
    }
    Node prev = head;
    while ((prev.next != null)) {//actually checking prev.next if it contains the value
        if (prev.next.value == value){
            prev.next=prev.next.next; //remove link from prev to prev.next 
            size--;
            return true;
        }
        prev = prev.next;
    }
    return false;
}

public boolean removeAll(int value) {
    if(head==null)return false;
    while(head.value==value){//does head contain the value 
        head=head.next;
        size--;
        result=true;
    }
    Node prev=head;
    while ((prev.next != null)) {//actually checking prev.next if it contains the value
        (prev.next.value == value){
            prev.next=prev.next.next; //remove link from prev to prev.next 
            size--;
            result = true;
        }else {
            prev = prev.next;
        }
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM