Hey all i am trying to figure out how to get this RefList working for me...
So far this is what i have:Quote:
Implement a method countValue() that counts the number of times an item occurs in a linked list.
int countValue(RefList f, int item);
Generate 20 random numbers in the range of 0 to 4, and insert each number at the front of a linked list. Output the list by using a method which you would call writeLinkedList which you would add to the RefList.java program. In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.
But there's an error for the "refl1.add(intRandomNum);" since the RefList class does not have a "add" property. How am i supposed to add to a list that has no command to add?Code:public class PREX6
{
public static void main(String[] args)
{
RefList refl1= new RefList();
int intRandomNum = 0;
int count = 0;
while (count != 21)
{
refl1.add(intRandomNum);
}
System.out.println(refl1);
}
}
Here is the RefList code:
Thanks!Code:public class RefList
{
protected int numElements; // number of elements in this list
protected LLObjectNode currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, else false
protected LLObjectNode location; // node containing element, if found
protected LLObjectNode previous; // node preceeding location
protected LLObjectNode list; // first node on the list
public RefList()
{
numElements = 0;
list = null;
currentPos = null;
}
protected void find(Object target)
// Searches list for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
boolean moreToSearch;
location = list;
found = false;
moreToSearch = (location != null);
while (moreToSearch && !found)
{
if (location.getInfo().equals(target)) // if they match
found = true;
else
{
previous = location;
location = location.getLink();
moreToSearch = (location != null);
}
}
}
public int size()
// Returns the number of elements on this list.
{
return numElements;
}
public boolean contains (Object element)
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
{
find(element);
return found;
}
public boolean remove (Object element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
if (list == location)
list = list.getLink(); // remove first node
else
previous.setLink(location.getLink()); // remove node at location
numElements--;
}
return found;
}
public Object get(Object element)
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
public String toString()
// Returns a nicely formatted string that represents this list.
{
LLObjectNode currNode = list;
String listString = "List:\n";
while (currNode != null)
{
listString = listString + " " + currNode.getInfo() + "\n";
currNode = currNode.getLink();
}
return listString;
}
public void reset()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentPos = list;
}
public Object getNext()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
{
Object next = currentPos.getInfo();
if (currentPos.getLink() == null)
currentPos = list;
else
currentPos = currentPos.getLink();
return next;
}
}
David

