Search code examples
javalinked-listcircular-list

Unable to delete first node in circular linked list


Alright, I'm trying to do this for an assignment. Been having an issue with my sorted circular linked list. I'm able to add and remove things just fine, with one exception. That exception being the first node in the list. It crashes on "if (location.getInfo().equals(target))", in the find method, everytime. I have no idea why and need help. It spits a null pointer error message and narrows it down to the above message. If I input, for example, Adam, it adds it to the list and tallies it appropriately. But, when I go to remove the item, it runs the find method and NPEs on the find method. I've tried both .equals(target) and ==, both give the NPE.

protected void find(T target)
{
    location = list;
    found = false;

    if(list != null)
    {
        System.out.println("\nFinding: " + target);
        do
        {
            previous = location; // move search to the next node
            location = location.getLink();

            if (location.getInfo().equals(target))*// checks for a match
            {
                System.out.println(target + " was found.");
                found = true;
            }
        } 
        while ((location != list) && !found);
    }
}

And here's my LLNode.java:

public class LLNode<T> 
    {
    private LLNode<T> link;
    private T info;

    public LLNode(T info)
    {
        this.info = info;
        link = null;
    }

    public void setInfo(T info)
    {
        this.info = info;
    }

    public T getInfo()
    {
        return info;
    }

    public void setLink(LLNode<T> link)
    {
        this.link = link;
    }

    public LLNode<T> getLink()
    {
        return link;
    } 
}

Any help would be greatly appreciated.

Also, here is my add method:

public void add(T element)
{
    LLNode<T> prevLoc;
    LLNode<T> location;
    T listElement;

    if (!element.equals(""))
    {   
        LLNode<T> newNode = new LLNode<T>(element);
        if(list == null)
        {
            list = newNode;
            newNode.setLink(list);
        }
        else if (list.getInfo().compareTo(element) > 0)
        {
            newNode.setLink(list.getLink());
            list.setLink(newNode);
            list = newNode;
        }
        else if (list.getInfo().compareTo(element) < 0)
        {
            newNode.setLink(list.getLink());
            list.setLink(newNode);

        }
        else
        {
            location = list.getLink();
            prevLoc = null;
            while (location != list)
            {

                listElement = location.getInfo();
                if (listElement.compareTo(element) < 0)
                {
                    prevLoc = location;
                    location = location.getLink();
                }
                else
                {
                    break;
                }
            }// Insert node into list
            if (prevLoc == null)
            {
                // Insert as first node
                newNode.setLink(list);
                list = newNode;
            }
            else
            {
                // Insert elsewhere
                newNode.setLink(location);
                prevLoc.setLink(newNode);
            }
        }
        numElements++;  
    }
    else
    {
        System.out.print("\nReturning to the main menu.");
    }
}

Hopefully, this helps narrow down where the problem with my code is.


Solution

  • In the add method:

    while (location != null)
    

    So the method expects location to sometimes be null. That means the list is not circular but has an end.

    In the find method:

    do {
         if (location.getInfo().equals(target))
         ...
    } while ((location != list) && !found);
    

    So the method expects location to never be null, like in a circular list.

    In a circular list all elements in the list point to another element in the list (in a circle).
    When the list has one element, it will point to itself.
    When the list has 2 elements, each will point to the other, and so on.

    In a normal linked-list, the last element will always point to null and the code needs to expect that.