简体   繁体   中英

Add method in LinkedList is not working

I'm working on this as extra practice for my Java class but I can't seem to see why my add method is not working. I've tried walking through the code with the debugger line by line but I can't see where it's going wrong. It obviously a logic error. When I test the add method it seem to work but it's not linking the Node's correctly I guess, or not storing the data.

So the assignment is to write a linked-list (that does not add duplicates). I did the Node class as an inner function to the LinkedSet class. Our textbook suggested that for this particular assignment. This is the add method I'm using.

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

In addition this is the test method for add (writing a separate testing app is required and done in a separate main class)

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

There are some other methods as well but until I can get the add method working I can't do much with them so I pretty sure the problem is around here somewhere. I've looked around the net at similar questions but I still can't see where it is off. Any help is appreciated, I've been messing with it for too long now.

if(firstNode == null)
    firstNode = aNewNode;

You should return true in this case.

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

This test doesn't make much sense. It would make more sense the other way round:

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

as there is no point in calling contains() if there are no entries, but contains() already knows that anyway, by virtue of firstNode being null if numberOfEntries is zero, so it should be just

if (!contains(newEntry))

NB You aren't maintaining numberOfEntries .

I haven't read all your code thoroughly but there is a mistake here:

if(firstNode == null)
        firstNode = aNewNode;

it should increment numberOfEntries .

Because of that numberOfEntries is always zero and add is not working properly

Similarly, you don't maintain numberOfEntries in else too.

Contains method also having problem, You are changing firstNode reference you should not change that

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;
            }

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