【问题标题】:Add method in LinkedList is not workingLinkedList 中的添加方法不起作用
【发布时间】:2017-08-04 07:44:27
【问题描述】:

我正在为我的 Java 类做额外的练习,但我似乎不明白为什么我的 add 方法不起作用。我试过用调试器逐行遍历代码,但我看不出哪里出错了。这显然是一个逻辑错误。当我测试 add 方法时,它似乎可以工作,但我猜它没有正确链接节点,或者没有存储数据。

所以任务是编写一个链表(不添加重复项)。我将 Node 类作为 LinkedSet 类的内部函数。我们的教科书建议对于这个特定的任务。 这是我正在使用的添加方法。

public class LinkedSet <T> implements SetInterface <T> {    
        private Node firstNode;
        private int numberOfEntries;


        public LinkedSet (){
            firstNode = null;
            numberOfEntries = 0;
        }

    public boolean add(T newEntry){
                boolean result = false;
                Node aNewNode = new Node (newEntry);

                //If Node is currently null then add data to front
                if(firstNode == null)
                    firstNode = aNewNode;
                else
                {
                    //Add newEntry if it's not already stored.
                    if(!contains(newEntry) && numberOfEntries != 0)
                    {
                        aNewNode.next = firstNode;
                        firstNode.next = aNewNode;
                        firstNode = aNewNode;

                        result = true;
                    }
                    else
                        result = false;
                }

                return result;      
            }//End add

        public boolean contains(T anEntry){
                boolean found = false;

                while (!found && (firstNode != null))
                {
                    if(anEntry.equals(firstNode.getData()))
                        found = true;
                    else 
                        firstNode = firstNode.getNextNode();
                }

                return found;
            }
    private class Node {

            private T data;
            private Node next;

            private Node (T theData){
                data = theData;
                next = null;
            }

            private Node(T theData, Node nextNode){
                data = theData;
                next = nextNode; 
            }
    } //End Node class
}End LinkedSet

另外这是 add 的测试方法(需要编写一个单独的测试应用程序并在单独的主类中完成)

private static boolean testAdd(SetInterface<String> aSet, String[] contentsToAdd){

        boolean result = false;

        for (int index = 0; index < contentsToAdd.length; index++)
        {
            aSet.add(contentsToAdd[index]);
            result = true;
        }

        return result;
    }//End testAdd

还有其他一些方法,但是在我可以使用 add 方法之前,我无法对它们做太多事情,所以我很确定问题出在某个地方。 我已经在网上查看了类似的问题,但我仍然看不到它在哪里关闭。任何帮助表示赞赏,我已经搞砸了太久了。

【问题讨论】:

    标签: java linked-list nodes singly-linked-list


    【解决方案1】:
    if(firstNode == null)
        firstNode = aNewNode;
    

    在这种情况下,您应该返回 true

    if(!contains(newEntry) && numberOfEntries != 0)
    

    这个测试没有多大意义。反过来说会更有意义:

    if(numberOfEntries != 0 && !contains(newEntry))
    

    因为如果没有条目,调用contains() 是没有意义的,但是contains() 已经知道无论如何,由于firstNode 如果numberOfEntries 为零,则为空,所以它应该只是

    if (!contains(newEntry))
    

    NB 你没有维护numberOfEntries

    【讨论】:

      【解决方案2】:

      我没有仔细阅读您的所有代码,但这里有一个错误:

      if(firstNode == null)
              firstNode = aNewNode;
      

      它应该增加numberOfEntries

      因为numberOfEntries 始终为零,并且 add 无法正常工作

      同样,您也不会在else 中维护numberOfEntries

      【讨论】:

        【解决方案3】:

        包含方法也有问题,您正在更改 firstNode 引用,您不应该更改它

        public boolean contains(T anEntry){
                        boolean found = false;
                        Node ptr = firstNode;
                        while (!found && (ptr != null))
                        {
                            if(anEntry.equals(ptr.getData()))
                                found = true;
                            else 
                                ptr = ptr.getNextNode();
                        }
        
                        return found;
                    }
        

        【讨论】:

        • firstNode = firstNode.getNextNode();
        猜你喜欢
        • 2018-06-24
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        相关资源
        最近更新 更多