Okay so this is the deal. I want to make a call to a routine that replaces all of one certain type of integer with a number. I have it to where it is replaceing the first, however, it misses the tail node.
public static SimpleList<Integer> replaceAll(Integer old, Integer nEw, SimpleList<Integer> l)
// returns a copy of l with all occurrences of old replaced with nEw
{
if(l.isEmpty())
return new SimpleList<Integer>();
else if (l.head().equals(old))
return new SimpleList<Integer>(nEw, l.tail());
if (l.tail().equals(old))
return new SimpleList<Integer>(nEw, l.tail());
else
return new SimpleList<Integer>(l.head(), replaceAll(old, nEw,l.tail()));
to make the call to the method I use System.out.println(Proj7.replaceAll(new Integer(2),new Integer(88),l1));
the list contains 1,1,2,2,3,3. Its printing out 1,1,88,2,3,3.
is my logic wrong? any helpful hints?
btw .head() and .tail() are part of a class called SimpleList. It just points to the first, and next node.