Connecting Tech Pros Worldwide Help | Site Map

Need help with linked lists

Newbie
 
Join Date: Mar 2009
Posts: 3
#1: Mar 9 '09
Write two functions to find the the number of elements in a linked list: one using a loop, and one using recursion. To represent the linked list use the class lp that I showed you in the lecture.
Expand|Select|Wrap|Line Numbers
  1. public class lp {
  2.   public int first;
  3.   public lp rest;
  4.   public lp(int first1, lp rest1)
  5.   {
  6.     first = first1;
  7.     rest = rest1;
  8.   }
  9. }
  10.  
The version with the loop looks like the function I showed you in class, which prints all the elements. All you have to do is add a counter, and increment it in the body of the loop. When you reach the end, the counter's value is the length. The recursive version looks like the recursive function for displaying elements of a list. To count the elements, you must rewrite the function so it returns an integer -- the length. In the base case return a zero. In the recursive case, the recursive call returns an integer -- your answer is one more than that integer.

Your program should build a list of length 3 and then call the two length functions to find its length. Building the list is easy:
Expand|Select|Wrap|Line Numbers
  1. list1 = new lp(1, new lp(2, new lp(3, null)));
  2.  
Your program doesn't need any input. It should look like this to the user:
Expand|Select|Wrap|Line Numbers
  1. length computed with a loop: 3
  2. length computed with recursion: 3
  3.  
thats my assignment and what i have right now is:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. public class lp{
  5.   public int first;
  6.   public lp rest;
  7.   public lp curr;
  8.   public int count;
  9.   public lp(int first1, lp rest1){
  10.     first = first1;
  11.     rest = rest1;
  12.   }//lp construct
  13.     public static void show(lp first){
  14.     if(first==null){}//if
  15.     else{
  16.       System.out.println(first.rest);
  17.       show(first.rest);
  18.     }//else
  19.   }//show
  20.   public int loop(lp curr,int counter){
  21.     lp cur=curr;
  22.     count=counter;
  23.     System.out.println("test");
  24.     while(cur!=null){
  25.       cur = cur.rest;
  26.       counter++;
  27.     }//while
  28.     System.out.println(counter);
  29.     return counter;
  30.   }//lp method
  31.   public static int listrecurs(int head, lp next){
  32.     if(next==null){
  33.       return head;
  34.     }//if
  35.     else{
  36.       return listrecurs(head+1,next);
  37.     }//else
  38.   }//recursive method  
  39.   public static void main(String args[]){
  40.     int beg=1;
  41.     lp list1 = new lp(1, new lp(2, new lp(3, null)));
  42.     System.out.println("Length computer with a loop is:");
  43.     System.out.println(count);
  44.     System.out.println("Length computer with recursion is:");
  45.     System.out.println(listrecurs(beg,list1));
  46.   }//main
  47. }//class lp
  48.  
help would be greatly appreciated
Newbie
 
Join Date: Mar 2009
Posts: 3
#2: Mar 10 '09

re: Need help with linked lists


Really any insight would be greatly appreciated I've updated the program a bit but I don't think there is any actual progress
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Mar 10 '09

re: Need help with linked lists


You just copied and pasted your homework here. That is not allowed here. Better make an attempt at solving it and post only after you get problems with something specific in the code.
Newbie
 
Join Date: Mar 2009
Posts: 3
#4: Mar 11 '09

re: Need help with linked lists


well, yes it was[because it was already due] my homework. but the code was mostly written by me except for the small bit that appears at the top. I just posted the assignment so people could know what i was trying to accomplish. But my problem was getting the program to print out the work I have done in the main methods. So yes it was my homework, but i needed help with one specific part but i figured if some one copied it and tried to compile it they would see my error. But I am still looking for help beings my professor doesn't return a correct way to accomplish this task
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#5: Mar 11 '09

re: Need help with linked lists


You have copied your assignment here. Then you listed what you did so far. But you forgot to write where the problem is. If you don't mention it, we don't know where to help.
Quote:

Originally Posted by AndrewFancyname View Post

But my problem was getting the program to print out the work I have done in the main methods.

"my problem was" or it "my problem is"? Did you already solve it? It seems so, because you are already printing the results in the main method with "system.out.println(...)."
Then there is nothing to help for us.

But if you mean "is", then you should specify more: what is it currently printing? What did you expect? Can you please provide a trace of your program (that means inserting print-statements after every method call to print the parameter values). and tell us where you did expect something different than it is printed?
For example changing your code from
Expand|Select|Wrap|Line Numbers
  1. ...
  2. public lp(int first1, lp rest1){ 
  3.     first = first1; 
  4. ...
to

Expand|Select|Wrap|Line Numbers
  1. ...
  2. public lp(int first1, lp rest1){
  3.     System.out.println("Inside method lp(): called with first1=" + first1 + ",rest1=" + rest1);
  4.     first = first1; 
  5. ...
thanks for helping us to help you.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Mar 11 '09

re: Need help with linked lists


The following part is a spoiler:

Quote:

Originally Posted by Assignment Text

The version with the loop looks like the function I showed you in class, which prints all the elements. All you have to do is add a counter, and increment it in the body of the loop. When you reach the end, the counter's value is the length.

All you have to do is modify that method as described. I don't understand why you ignored that 'hint' (it's more than a hint actually) and started typing all that crappy code instead. Did you read your own assignment or did you simply dump it here for our amusement?

kind regards,

Jos
Newbie
 
Join Date: Oct 2009
Posts: 4
#7: 4 Weeks Ago

re: Need help with linked lists


good morning from greece!

let's say that we have one button in flash with actionscript 2.0 and we want to use tha same button in order to show us more than one image (frame), in a slide show, what's the actionscript that we need in that case?

i'm waiting for help!

REMIND: when we want to give one action for one frame or movieclip we put

Ex.1)
Expand|Select|Wrap|Line Numbers
  1. on(release){
  2.     with(_root.gallery){
  3.         gotoAndPlay("two");
  4.     }
  5. }
  6.  
or Ex2)
Expand|Select|Wrap|Line Numbers
  1. on(release){
  2.     gotoAndPlay("that", 1);
  3. }

....but ...what do we put for more than one 'link'?

thanx
Newbie
 
Join Date: Oct 2009
Posts: 4
#8: 4 Weeks Ago

re: Need help with linked lists


any help about the question??
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: 4 Weeks Ago

re: Need help with linked lists


Let's say that you start your own (new) thread in a relevant forum.
This is the Java forum you are asking in.
Newbie
 
Join Date: Oct 2009
Posts: 4
#10: 4 Weeks Ago

re: Need help with linked lists


ok forget it.
help me delete my account from bytes.com
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: 4 Weeks Ago

re: Need help with linked lists


Quote:

Originally Posted by Panagiotis View Post

ok forget it.
..

Done.

Quote:

Originally Posted by Panagiotis View Post

...
help me delete my account from bytes.com

Accounts don't get deleted.
Reply