Hello All,

I am having a problem with removing a specific item from a linked list. Any advice would be great:

Here are some snippets:

private Node head; //the head node at the front of the list
private Node previous; //element just before the current position
private Node current; //refers to the current element

public boolean removeSpecific(Item thing)
      {
         if(head != null)
         {
            if(previous == null)
               head = head.next;
            else if(current.data.equals(thing))
            {
              previous.next = current.next;
              current = current.next;
              count--;
              return true;
            }
        }
         return false;
      }

Something is very off here.
Any assistance will be appreciated.
Thank you

Do you understand what each individual line of your code does? Draw out your data structure on paper and simulate by hand what your code is doing, line by line. You'll see your problem then. Potential problems include... current could be null; behavior differs when the first node of the list is the current node; the code might not even be close to what you want to do.

I am having a problem with removing a specific item from a linked list. Any advice would be great:

Here are some snippets:

private Node head; //the head node at the front of the list
private Node previous; //element just before the current position
private Node current; //refers to the current element

public boolean removeSpecific(Item thing)
{
	 previous = null; current = head;
         while (current != null)
         {
            if(current.data.equals(thing))
            {
              current = current.next;
	      if (previous == null) previous = current
              else previous.next = current;
              count--;
              return true;
            } else {
              previous = current;
              current = current.next;
	    }
         } //end while
         return false;
}

Why did you write a solution for him? How is that supposed to help him?

Why did you write a solution for him? How is that supposed to help him?

I apologize sincerely. I thought he is in need of a solution quick else he cannot submit his solution (I presume he is doing some school related assignment?). I promise in future not to give answers but guide them towards the solution instead.

Giving fish to the poor will not be as good as teaching the poor how to fish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.