java - 创建的链接列表未返回正确的索引

标签 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

最佳答案

问题是您的 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;

您仍然会遇到一个问题,如果该元素不在列表中,您的代码将引发异常。通过对上面的代码稍加修改就可以轻松解决这个问题,但我将把它留给你来解决!

关于java - 创建的链接列表未返回正确的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832121/

相关文章:

java - android:background不适用于Theme.MaterialComponents

c - 从链表数组中删除一个节点?

java - Eclipse 错误无法解析为类型

Java多线程访问同一个变量

java - 如果序列化是在层次结构中继承的,则将调用未实现序列化的类的构造函数。为什么会这样呢?

java - ASP .net C# Decimal 与 Java Double 之间的舍入不匹配

C 将链表写入文件

java - 如何在我的 Java 应用程序中添加文件浏览器?

c++ - 使用 `delete` 从链表中删除节点

C - 我想反转链表