簡體   English   中英

LinkedList中的Add方法無效

[英]Add method in LinkedList is not working

我正在研究這個作為我的Java類的額外練習,但我似乎無法理解為什么我的add方法不起作用。 我已嘗試逐行調試代碼,但我無法看到它出錯的地方。 這顯然是一個邏輯錯誤。 當我測試add方法時,它似乎工作但它沒有正確地連接Node我猜,或者沒有存儲數據。

因此,分配是編寫鏈表(不添加重復項)。 我將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

此外,這是添加的測試方法(需要編寫單獨的測試應用程序並在單獨的主類中完成)

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方法工作,我不能對它們做太多,所以我很確定這個問題就在這里。 我在類似的問題上環顧網絡,但我仍然看不到它在哪里。 任何幫助表示贊賞,我現在已經搞砸了太久了。

if(firstNode == null)
    firstNode = aNewNode;

在這種情況下你應該返回true

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

這個測試沒有多大意義。 反過來會更有意義:

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

因為沒有條目調用contains()如果沒有條目,但contains()已經知道無論如何,如果numberOfEntries為零,則firstNode為null,所以它應該只是

if (!contains(newEntry))

注意:您沒有維護numberOfEntries

我沒有徹底閱讀你的所有代碼,但這里有一個錯誤:

if(firstNode == null)
        firstNode = aNewNode;

它應該增加numberOfEntries

因為numberOfEntries始終為零,並且add無法正常工作

同樣,您也不會在else維護numberOfEntries

包含方法也有問題,您正在更改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;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM