Java – The Add method in LinkedList does not work

The Add method in LinkedList does not work… here is a solution to the problem.

The Add method in LinkedList does not work

I’m using this as an extra exercise for my Java class, but I can’t seem to understand why my add method isn’t working. I tried examining the code line by line with the debugger, but I couldn’t see what was wrong. This is clearly a logical error. When I test the add method, it seems to work, but I’m guessing it’s not linking nodes correctly, or not storing data.

So the job is to write a linked list (without adding duplicates). I use the Node class as an intrinsic function of the LinkedSet class. Our textbook recommendations are for this specific 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 added test method (which requires writing a separate test application and completing it 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 a few other methods, but I can’t do much with them until I can use the add method, so I’m pretty sure the problem is somewhere nearby.
I looked at similar issues online but I still can’t see where it is. Thanks for any help, I’ve been messing up for too long.

Solution

if(firstNode == null)
    firstNode = aNewNode;

In this case, you should return true.

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

This test doesn’t make much sense. It would make more sense the other way around:

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

It doesn’t make sense to call contains() without entries, but contains() already knows because firstNode is null if numberOfEntries is zero, so it should just be

if (!contains(newEntry))

Note that you are not maintaining numberOfEntries.

Related Problems and Solutions