hi i'm new java , have problem 2 methods operating on linkedlist.
the find function have wrote return false. find method take type e element argument, , returns true if item in linked list, or false otherwise.
the max method return maximum element(longest string in case) in list if list not empty, or null if empty list. comparison has done compareto(). max have wrote looking @ first letter of every element(string).
any appreciated!
public boolean find(e e){ linkedlisttest<e>.node node = null; node current =node; while (current != null){ if (current.equals(e)){ return true; } else{ current=current.next; } } return false; } public e max(){ iterator<e> iterator=iterator(); e max = iterator.next(); while (iterator.hasnext()) { e next = iterator.next(); if (max.compareto(next) > 0) max = next; } return max; }
your find
returns false because initialize node
, current
null, loop never entered. in addition, should compare e item, not node.
it should be:
public boolean find(e e){ node current = head; while (current != null){ if (current.item.equals(e)){