创建的链表未返回正确的索引

时间:2019-07-01 08:12:18

标签: java linked-list

我创建了一个行进为{50,10,60,30,40}的列表,并希望在链接列表中达到30时返回索引,但是我的程序始终返回-1(基本上它无法递增,返回我的索引)。除非能够将anEntry对象转换为整数并将其等于我的索引,否则我不确定如何在另一个庄园中找回索引。

class MyLinkedList {
    private Node firstNode; // index = 0
    private int length;
    public MyLinkedList() {
        firstNode = null;
        length = 0;
    } // end default constructor
    /** Task: Adds a new entry to the end of the list.
     * @param newEntry the object to be added as a new entry
     * @return true if the addition is successful, or false if not */
    public boolean add(Object newEntry) {
        Node newNode = new Node(newEntry);
        if (isEmpty())
            firstNode = newNode;
        else {
            Node lastNode = getNode(length-1);
            lastNode.next = newNode;
        }
        length++;
        return true;
    } // end add
    /** Task: Adds a new entry at a specified index
     * @param newEntry the object to be added at the specified index
     * @return true if successful, or false if not */
    public boolean add(int index, Object newEntry) {
        boolean isSuccessful = true;
        if ((index >= 0) && (index <= length)) {
            Node newNode = new Node(newEntry);
            if (isEmpty() || (index == 0)) {
                newNode.next = firstNode;
                firstNode = newNode;
            }
            else {
                Node nodeBefore = getNode(index - 1);
                Node nodeAfter = nodeBefore.next;
                newNode.next = nodeAfter;
                nodeBefore.next = newNode;
            }
            length++;
        }
        else
            isSuccessful = false;
        return isSuccessful;
    } // end add
    /** Task: Determines whether the list contains a given entry.
     * @param anEntry the object that is the desired entry
     * @return true if the list contains anEntry, or false if not */
    public boolean contains(Object anEntry) {
        boolean found = false;
        Node currentNode = firstNode;
        while (!found && (currentNode != null)) {
            if (anEntry.equals(currentNode.data))
                found = true;
            else
                currentNode = currentNode.next;
        } // end while
        return found;
    } // end contains
    /** Task: Gets an entry in the list.
     * @param the desired index
     * @return the desired entry, or
     * null if either the list is empty or index is invalid */
    public Object getEntry(int index) {
        Object result = null; // result to return
        if (!isEmpty() && (index >= 0) && (index < length))
            result = getNode(index).data;
        return result;
    } // end getEntry
    /** Task: Gets index of an entry in the list.
     * @param the desired entry
     * @return index of the first occurrence of the specified entry
     * in this list, or -1 if this list does not contain the entry */
    public int getIndex(Object anEntry) {
        Node currentNode = firstNode;
        int index = 0; // result to return
        while (anEntry != currentNode.data) {
           currentNode = currentNode.next;
           index++;
           if (anEntry.equals(currentNode.data)){
               break;
           }
           else {
               return -1;
           }
        }
        return index;
    } // end getIndex
    private Node getNode(int index) {
        Node currentNode = firstNode;
        // traverse the list to locate the desired node
        for (int counter = 0; counter < index; counter++)
            currentNode = currentNode.next;
        return currentNode;
    } // end getNode
        private Node getNode(int index) {
        Node currentNode = firstNode;
        // traverse the list to locate the desired node
        for (int counter = 0; counter < index; counter++)
            currentNode = currentNode.next;
        return currentNode;
    } // end getNode
    private class Node {
        private Object data; // data portion
        private Node next; // link to next node

        private Node(Object dataPortion) {
            data = dataPortion;
            next = null;
        } // end constructor
        private Node(Object dataPortion, Node nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor
        private void setData(Object dataPortion) {
            data = dataPortion;
        } // end setData
        private Object getData() {
            return data;
        } // end getData
        private void setNextNode(Node nextNode) {
            next = nextNode;
        } // end setNextNode
        private Node getNextNode() {
            return next;
        } // end getNextNode
    } // end Node
} // end MyLinkedList

1 个答案:

答案 0 :(得分:1)

问题是您的while循环最多只能进行一次迭代。

if (anEntry.equals(currentNode.data)){
    break;
}
else {
    return -1;
}

如果当前元素匹配,那么我已经找到了该项目,因此我们可以停止。如果不是,则列表不包含该项目。应该相对清楚地知道这种逻辑是错误的。仅仅因为当前元素不匹配并不意味着后续元素可能不匹配。

您可以通过观察getIndex(50)实际上确实返回正确的索引:零来进一步证明这一点。您的语句“ 我的程序总是返回-1 ”实际上是不正确的。

我们需要交换这种逻辑-仅在我们尝试了所有元素之后才返回-1

while (anEntry != currentNode.data) {
    currentNode = currentNode.next;
    index++;
    if (anEntry.equals(currentNode.data)){
        return index;
    }
}
return -1;

您仍然会有一个问题,如果元素不在列表中,您的代码将引发异常。只需对上面的代码稍作更改,就可以解决这个问题,但是我让您自己解决!