简体   繁体   中英

Can someone tell me what i'm doing wrong? Counting and looping through LinkedList

My code is as follows:

import net.datastructures.Node;

public class SLinkedListExtended<E> extends SLinkedList<E> {

public int count(E elem) {
    Node <E> currentNode = new Node <E>();
    currentNode = head;
    int counter = 0;

    for (int i = 0; i<size; i++){

    if (currentNode == null) {
        return 0; //current is null
    }
    else if (elem.equals(currentNode.getElement())){
                counter++;
                currentNode = currentNode.getNext();
            }
    }
    return counter;
    }



public static void main(String[] args) {

    SLinkedListExtended<String> x = new SLinkedListExtended<String>();

    x.insertAtTail("abc");
    x.insertAtTail("def");
    x.insertAtTail("def");
    x.insertAtTail("xyz");
    System.out.println(x.count("def")); // should print "2"
    //x.insertAtTail(null);
    x.insertAtTail("def");
    //x.insertAtTail(null);
    System.out.println(x.count("def")); // should print "3"
    //System.out.println(x.count(null)); // should print "2"
}

}

The method count is supposed to return the number of the amount of times a given element, elem is found in a list. I have written this loop but only get a return of 0 every time. A nullpointerexception is also thrown.

Edit: SLinkedList SuperClass

import net.datastructures.Node;

public class SLinkedList<E> {
protected Node<E> head; // head node of the list
protected Node<E> tail; // tail node of the list (if needed)
protected long size; // number of nodes in the list (if needed)

// default constructor that creates an empty list
public SLinkedList() {
    head = null;
    tail = null;
    size = 0;
}

// update and search methods
public void insertAtHead(E element) {
    head = new Node<E>(element, head);
    size++;
    if (size == 1) {
        tail = head;
    }
}

public void insertAtTail(E element) {
    Node<E> newNode = new Node<E>(element, null);
    if (head != null) {
        tail.setNext(newNode);
    } else {
        head = newNode;
    }
    tail = newNode;
    size++;
}



public static void main(String[] args) { // test


}
}

It seems you missed to go to the next node if non of both condition match.

public int count(E elem) {
    Node <E> currentNode = new Node <E>();
    currentNode = head;
    int counter = 0;

    for (int i = 0; i<size; i++){
        if (currentNode == null) {
            return 0; //current is null
        }
        else if (elem.equals(currentNode.getElement())){
            counter++;
        }
        currentNode = currentNode.getNext();          
    }
    return counter;
}

MrSmith's answer nails it, I think. I would not use size for the loop but take the fact that there is no next as bottom. Of course your count method then has to return the counter in all cases and not 0.

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