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 (in page 414-421) program. In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.
So far this is what i have:
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?
Here is the RefList code:
Probably easier to use a java.util.Random object, and its nextInt(5) method, than Math.random().
You will have to write an add method, which inserts the new Object at the very beginning of the list, and maybe overload it to add at a particular location: public boolean add(Object obj) and public boolean add(int index, Object obj). You could use void return type if you prefer.
And I'm really not getting the "int countValue(RefList f, int item);"... Nowhere in that does it have the "size()" command found in the RefList that could be used to get how many items are in the list without having to do all that???
You need a root class, the way I saw it done.This is a singly-linked list, so your remove method actually removes the next node. See how it does it, passing the command onto the next node until it reaches the end of the list or finds the element to remove. If you reach the end of the list before removing the element, it will simply return null. If you have two elements which return true from equals, it returns the parameter, so you could use an == operator on the returned value.
The removeNextNode() method reassigns "next" to the next from the next, unless there is already a null. You need the null tests because there is guaranteed to be a null value somewhere at the end of the list.
Change removed = topNode.getElement(); to removed = e; in LLRoot to match LLNode[/edit]
[ October 23, 2008: Message edited by: Campbell Ritchie ]
This may help you and me more.. its some "hints" for getting this flowing:
* Declare an unsortedlinkedlist unlist.
* use a for loop in which you will do the following 20 times:
1. create a random number between 0 and 4
2. insert the number into the unlist
* print the elements in unlist.
* call the countValue method 5 times where each time you will send 2 parameters:
1. the unlist
2. the value of an int that is 0,1,2,3,4
* write the countValue method which accomplishes the followings:
1. it has 2 parameters:
o the unlist
o an int i
2. declare an int counter a.
3. make a list element out of the element i, say f2.
4. declare a for loop that will run 20 times in which you will do the following:
o get the first element from the unlist, say f1.
o make sure you compare f1 to f2.
o if they are equal increment the counter a.
5. return a.
I dont see a "LLNode<E>" in any class'es this program uses.... Is that the section i need to add to RefList.java? I really dont see anything in the chapter that has anything close to explaining what "LLNode<E>" does...
you should be able to get at the topNode, and from that gain access to the other nodes in order (you can't simply get at node 3 in a linked list; you can only get it from node 2 or maybe node 4). You can then go through the list and look for the values you want and count them.
But sorry if I have posted something which doesn't help.
You can get away without the temp local variable
. . . current.setNext(new LLNode(obj));
Consider whether to put the counting in the LLNode class.
Consider whether to keep a reference to the end node as a field. That will make it much quicker to reach the end of the list when you are adding, but you have to update it if you remove the end node.
But just on a 3-second look, it will all work as you expect.
So now im on the:
write the countValue method which accomplishes the followings:
1. it has 2 parameters:
* the unlist
* an int v
2. declare an int counter a.
3. declare a for loop that will run 20 times in which you will do the following:
* get the first element from the unlist, say f1.
* make sure you compare f1 to v.
* if they are equal increment the counter a.
4. return a.
However, this is the errors i get
I need some help getting the RefUnsortedList value from "f"...
Some of those errors are because you wrote LLObjectNode and I wrote LLNode. The compiler objects to the different spelling.
You need to compare your two interfaces, which have methods with the same signature but different return types; they both have to return the same thing.
Your count method has several unnecessary local variables and doesn't actually count. I shall be surprised if you can even compile it in its present form.
What you need to do is to start at the head node, see whether its value is the same as "v" then go to the next node. Count as you go, then return the count.
And you can't simply use the == operator to compare nodes and values.
Probably worth having a currentNode local variable to help find the next node.
And if I had been teaching I would have told you to count the occurrences of "v" in the whole list, rather than counting 20 nodes.
In which case what the List contains is Object instances, and the JVM has no way of knowing what they are when they come out.
There are two ways of sorting it out, one good and complicated, the other cheap and cheerful and simple and unreliable.
See following posts.
This will work in Java5 and later, not in Java1.4.2 or earlier.
Parameterise the list.
Every method in the LLObjectNode class which takes an Object as its Parameter takes an E. That's right, an E. Short for element.
Every method in the LLObjectNode class which returns an Object as its return type returns an E.
The type of class is changed; it is no longer LLObjectNode, but LLOBjectNode<E>.
Every time you quote the LLObjectNode class you have to quote it as LLObjectNode<Something>, and you add the same <Something> to any constructor calls.
What that means is the compiler will only allow you to put one particular kind of Object in, and only get one kind of Object (the same kind) out. If you declare the formal type parameter as <Integer>, then you can only put Integers in, and only get Integers out.
This is a brief introduction to generics, which ought to have been in the language from day 1 and was introduced 8 years too late in September 2004 (Java 5).
So how do you get an Integer into an int? Another feature introduced in Java5 was boxing (also called auto-boxing because it is done automatically). What it means is you can present an int to something which is an Integer and the compiler will fiddle it so it works. It works the other way round, too, so you can have an Integer and put it into an int variable, only that is called unboxing.
So, parameterising the entire class and then using it with <Integer> will sort that problem out.
When you try putting an int into something which is supposed to be an Object, the compiler will try. The nearest it can find is Integer, so it will try that; an Integer is in fact an Object, so it puts an Integer into your list. You already know all about that; it is called boxing.
Now, when you try to get it out, it is still an Integer, but the compiler doesn't know that; your method returns Object, so the compiler only knows it is an Object.
If you are sure you are only putting ints/Integers into your list, you try casting them back when they are retrieved. Put a cast to (Integer) where you are retrieving the values from the list, and the compiler will un-box the value back to the int.
You will get all sorts of complaints from the compiler, particularly if you are using -Xlint, but it will actually compile and run, as long as you haven't let anything which isn't an Integer into your list.
If you have got something else . . . [Try it and see what happens.]
The other errors mean you need to have an add method which is in the interface. Also the two add methods in the two interfaces need to have the same return type.
I set them all to void and it compiled... it seems to be working now..