Self-Evaluation for CS251 Students
25 pts
1. Write a non-recursive member function of
a linked list type that deletes every negative number from a linked list. If the linked list has no negative numbers it
should do nothing. If a list has 2,-3,
42,-8,-9, 11,-38, 4,-2,-13 the member would change it to: 2, 42,11,4 (do not use
other member functions).
25 pts
2. If the ListOfInts,
lisa, is –2, 3, 1,-5, 7, 4, 7 then what is
lisa.recurse()? lisa.rest.recurse()?
public class
ListOfInts
{
boolean emptyList = true;
// true if list is empty
int firstData; // first data item in list if not empty
ListOfInts rest; // rest
of the list if not empty
/** is list empty? */
public boolean empty() {
returnt emptyList; }
/** what is first element
of list */
public int first() {
if(empty()) throw new Exception(); return firstData; }
/** puts a new item at
beginning of list */
public void push(int
newFirst)
{ ListOfInts newRest = new ListOfInts();
newRest.emptyList
= emptyList;
newRest.firstData
= firstData;
newRest.rest
= rest;
rest = newRest;
emptyList
= false;
firstData
= newFirst;
} // end push
… // more methods go here
but not necessary for review
/** method to test
understanding of recursion in lists */
public int recurse()
{
if(rest.empty()) return first();
else if(first()<0)
return first()+2*rest.Recurse();
else return first() + rest.Recurse();
} // end recurse
} // end ListOfInts
25 pts
3. Present the items of this tree in
prefix, postfix and infix: 