Need help with a for loop
Hi, I have to do program that uses a for loop to traverse a linked list. I have 99% of the program done and ready to go but our professor wanted us to use a specific loop which is the loop on line 31. I don't understand what b is suppose to be. like... how would i declare b and what would it be declared as... I don't think its an iterator. ill post my code below that so you can get an idea of what i'm trying to do and what i'm missing. I dont know the data type....
Here is the loop (its in my method):
Code :
for(b=list;b != null;b=b.rest)
System.out.print(b.first);
Heres my entire code:
Code :
import java.util.*;
public class homeWorkThree{
public static void main(String[] args)
{
LinkedList <Integer>list = new LinkedList<Integer>();
int one=1, two=2, three=3, four=4, five=5, n=0;
list.add(one);
list.add(two);
list.add(three);
list.add(four);
list.add(five);
occur(list,n=4);
boolean trueOrFalse = occur(list, n);
if(trueOrFalse==true)
System.out.println(trueOrFalse+": "+n+" does occur in the list");
else
System.out.println(trueOrFalse+": "+n+" does NOT occur in the list");
occur(list,n=9);
boolean trueOrFalseTwo = occur(list, n);
if(trueOrFalseTwo==true)
System.out.println(trueOrFalseTwo+": "+n+" does occur in the list");
else
System.out.println(trueOrFalseTwo+": "+n+" does NOT occur in the list");
}
public static boolean occur(LinkedList <Integer>list, int n){
boolean isItTrue = false;
for(b=list;b != null;b=b.rest)
System.out.print(b.first);
for(int i = 0; i < list.size(); ++i){
if(list.get(i)==n)
isItTrue=true;
else{}
}
return isItTrue;
}
}
Re: Need help with a for loop
That's how a theoretician would program iteration on a list - it's meant to be pseudo-code, isn't it? Perhaps the closest you would get with java.util.LinkedList is its pollFirst() method.
Re: Need help with a for loop
Yeah, that sure does look like you're missing something, or like it wasn't meant to be real code.
Also, doesn't it defeat the point of a LinkedList to use the get(i) function? Maybe that's what he was getting at.
Re: Need help with a for loop
Re: Need help with a for loop
The pseudo-code author refers to a recursive definition of a list: a list is an element and a list (rest). There's nothing quite like that in Java at all.
I think your professor intends for you to write a recursive linked list. It's not as hard as writing a list-of-nodes - you'd use lists-of-nodes for performance reasons, recursive lists for academic reasons!
I just typed up a recursive list out of interest, but posting it here would certainly be spoonfeeding! Here's my main method:
Code java:
public static void main(String[] args)
{
CrazinsList<Character> listJPF = new CrazinsList<Character>();
for (char c : "JavaProgrammingForums.Com".toCharArray())
listJPF.add(c);
System.out.println(listJPF + " is " + listJPF.size() + " long");
System.out.println(listJPF.get(0).toString() + listJPF.get(4).toString() + listJPF.get(15).toString());
System.out.println("Removed: " + listJPF.remove(20));
System.out.println(listJPF);
for (CrazinsList tmp = listJPF; tmp != null; tmp = tmp.rest)
System.out.println(tmp + " is " + tmp.size() + " long");
}
- you can see your 2-line 'for' statement at the bottom. And here's the output:
Quote:
JavaProgrammingForums.Com is 25 long
JPF
Removed: s
JavaProgrammingForum.Com
JavaProgrammingForum.Com is 24 long
avaProgrammingForum.Com is 23 long
vaProgrammingForum.Com is 22 long
aProgrammingForum.Com is 21 long
ProgrammingForum.Com is 20 long
rogrammingForum.Com is 19 long
ogrammingForum.Com is 18 long
grammingForum.Com is 17 long
rammingForum.Com is 16 long
ammingForum.Com is 15 long
mmingForum.Com is 14 long
mingForum.Com is 13 long
ingForum.Com is 12 long
ngForum.Com is 11 long
gForum.Com is 10 long
Forum.Com is 9 long
orum.Com is 8 long
rum.Com is 7 long
um.Com is 6 long
m.Com is 5 long
.Com is 4 long
Com is 3 long
om is 2 long
m is 1 long
Done
Every method inside the CrazinsList class is implemented with recursion - no loops anywhere.