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.

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;
  }
}

Recommended Answers

All 33 Replies

If my question was unclear just post and say its unclear. Ill try and explain better

What is the occur method supposed to do?

Well the occur method is only suppose to check the linked list to see if n occurs and then return boolean true if it does or false if it doesnt. I have a for loop that does that but its not the for loop he wanted us t

o use. He wanted us to use the one with b in it and i dont know what b is suppose to be. Int? Array? Your guess is as good as mine. This part:

for(b=list;b != null;b=b.rest)
                 System.out.print(b.first);

What variable will point to the nodes/elements in the LinkedList as you go through it?

b will i think... according to him that loop should go through a linked list and print out each node.

our professor wanted us to use a specific loop which is the loop on line 31

Is the code on line 31 EXACTLY what the professor gave you?

You have seen a loop that prints every number in a linked list L:

for(b=L;b != null;b=b.rest) System.out.print(b.first);


Thats what he said verbatim.

I missed what he said so I can only go on what you are posting.
What is the datatype of the rest variable?
What is the first variable?

No idea. He didnt say anything. Just posted this.

Homework 3: Find an Element in a Linked List

You have seen a loop that prints every number in a linked list L:

for(b=L;b != null;b=b.rest) System.out.print(b.first);

Write a method called "occur" that takes two arguments: a linked list of integers L and an integer N. It should return the Boolean true if N occurs in the list L,otherwise false. Use a loop like the one above. Set your answer initially to the Boolean false,because you haven't found the number N yet. Inside the loop,compare N to (b.first). If they are equal,change your answer to true -- you have found N. At the end of the function,return the current answer. Your main method should create a list L with members 1,2,3,4,5 and call the "occur" method twice: once to check if 4 occurs in the list L,and once to check if 9 occurs. Print both results.

Sorry, you have much more experience with reading what your prof says. I have no idea what he means.

Look he wants us to use the loop he provided there to traverse a link list. I dont know what b is. I assume L would be list because thats the name of my linked list. im asking you to tell me what b would be. i think i have to initialize it as something. like int b=0; why not try compiling my program and see what it says.

I'd think b was a reference for what is in the list.

cool so how do i make it work? lol BTW thanks for being patient. my professor is not the greatest as you may have guessed lol. i think i need to initialize it or declare it or something...

You'd need to define b. The for loop does the initialization.

what would i define it as or rather how... i mean if i can just get that for statement running i can take it and run with it from there

You need to answer these questions:
What is the datatype of the rest variable?
What is the first variable?

is b a.... node?

i cant answer those questions... if i had those answers i wouldnt be here :P its a linked list of ints.

Your prof's code uses those variables. If you are going to use his code, you must know what they are and where they are defined.

thats just it. they were not used anywhere. he just posted that and said have fun! im asking you to help me adapt it to my code.

There is no way I see to use any of that code. The LinkedList class does NOT have any fields that you can access.

You'll have to look back on what the prof has said in the past and come up with what he means.

Good night. I'm done for the day.

for(b=list;b != null;b=b.rest)
    System.out.print(b.first);

Ignoring some Java syntax details, this pseudo code looks good to me.
Here's how this works:

b starts out referring to the whole original list
the first pass of the loop prints the first element of b, ie the first in the original list
b is then set to b.rest, ie the list minus the first element
the second pass of the loop prints the first element of b, ie the second in the original list
b is then set to b.rest, ie b minus its first element, which equals the original list minus its first two elements
the third pass of the loop prints the first element of b, ie the third in the original list
...
until b.rest returns null, ie the whole list has been consumed.

lol thank you for recognizing that it was pseudo code. thats a huge first step in the right direction. i was running around the java api look for this crap haha. but lets say i make b a linked list then what would i make .first and .rest? i assume .rest is pseudo code for saying 'the rest'

LinkedList <Integer>b = new LinkedList<Integer>();
    for(b=list;b != null;b=b.rest)
      System.out.print(b.first);

If b is a LinkedList object, what methods does it have that will:
get a reference to the first node in the list
get a reference to the next node in the list

Then how to recognize the end of the list, ie that there are no more nodes.

If you use an Iterator it has methods that use that terminology.

Yes something like that. Ideally the LinkedList implementation would have a first() method that returns the first element, and a rest() method that returns the remainder of the list as a new list. That's not exactly how the standard LinkedList in the Java API works, but it is pretty typical of lists in other list-based languages like Lisp.

k so i need to write a first method and a rest method and use .first and .rest to call them?

An Iterator has methods similar to those variable names.

great thats even better! can you whip me up a little code to show me how to make b my iterator? and also what the names of the methods are that are similar to those varibles

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.