For the assignment we're suppose to add a findFirst, findNext, and addElement method to the given class and test them. I manage to get the findNext and addElement working but not the findFirst.
Here's what the findFirst suppose to do
"Method findFirst(T item) searches for and returns the index of the element closest to the head node or returns -1 (not found)."
Here's my code
public int findFirst(T elem)
{
int retval = -1;//return a -1 if not found
Node tmp = head;
if(hasNext())
{
for (int k = 0; k < size; k++)
{
if (tmp.data.equals(elem))
{
return retval = k;//return index closest to head
}
tmp = tmp.getNext();//going from head to tail
}
}
return retval;
}
No matter what I test, it would always return -1.
Thanks