I don't quite know how to write a driver to test out some of the methods, i could only get the average, but for the void methods,moveToFront and countCommonValues, how would you write the driver to test it?
Driver:
publicclass IntListDriver
{
publicstaticvoid main(String[] args)
{
IntNode head=null;
for (int i = args.length - 1 ; i >= 0 ; i--) {
head = new IntNode(Integer.parseInt(args[i]), head);
}
IntList list = new IntList();
list.head=head;
double result;
result=list.getAverage();
System.out.println(result);
}
}
Class:
publicclass IntList
{
public IntNode head; // DO NOT MAKE THIS FIELD PRIVATE
public IntList()
{
head = null;
}
publicdouble getAverage()
{
double count=0;
double average=0;
IntNode pos=head;
while (pos!=null)
{
count++;
average=average+pos.data;
pos=pos.next;
}
average=average/count;
return average;
else
{
returnnull;
}
//ERROR CONDITIONS!
}
publicvoid addAfterEvery(int oldvalue, int newvalue)
{
IntNode pos=head;
IntNode prev=null;
while (pos!=null)
{
if (oldvalue==pos.data)
{
prev=pos.next;
IntNode newNode=new IntNode();
newNode.data=newvalue;
pos.next=newNode;
newNode.next=prev;
pos=newNode.next;
}
else
{
pos=pos.next;
}
}
}
publicvoid moveToFront(int value)
{
IntNode pos=head;
if (pos==null)
{
return;
}
IntNode p=null;
while (pos.next!=null)
{
if (value==pos.next.data)
{
p=pos;
pos=pos.next;
if (pos!=null)
{
p.next=pos.next;
pos.next=head;
pos=p;
}
}
else
{
pos=pos.next;
}
}
}
publicvoid reverse()
{
//IntNode
//pos.next=null;
//while (pos.next!=null)
//{
// pos.next=pos;
//
//}
//head=pos;
}
publicint countCommonValues(IntList other)
{
IntNode pos=head;
IntNode posother=other.head;
int count=0;
while (pos!=null)
{
if (pos==posother)
{
count++;
pos=pos.next;
posother=posother.next;
}
else
{
pos=pos.next;
posother=posother.next;
}
}
return count;
}
}