How to recursively find a value in a linked list

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster

How to recursively find a value in a linked list

 
0
  #1
Oct 10th, 2009
Hi
I have a method that has a reference to a linked list and an int that is the value. I want to recursively call that value to count and return how often that value is in the linked list.
So, here is what I got:
  1. public static int find(LinkedNode x, int value){
  2. if (x.value != value){
  3. return 0;
  4. }
  5. else{
  6. return 1+ find(x.next, value);
  7. }
  8. }
is that right?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,020
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #2
Oct 10th, 2009
Write a small test case. If the answer's right across a few different test cases, your code is good! (It looks OK to me.)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #3
Oct 10th, 2009
i tried testing, but it keeps giving java.lang.StackOverflowError. How do I fix this?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,020
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #4
Oct 10th, 2009
Maybe you have all non-zero values?
You also need to check that x.next() refers to a real node, so you can stop recursing when you reach the end of the list.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #5
Oct 10th, 2009
Yeah, I that is true, I put in all numbers, but even I put in some zeros it still shows that? what are you supposed to input in the linked list?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,652
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #6
Oct 10th, 2009
You just want to count how many times a value is in the linked list? If so, I don't see how your method could work.

if (x.value != value){
return 0;
}

The first time the value isn't at the node, it will stop, even if the value is found at some later node in the list. Right?
Last edited by BestJewSinceJC; Oct 10th, 2009 at 4:28 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #7
Oct 10th, 2009
Yes, so do i return nothing, or do I just call the method?
Reply With Quote Quick reply to this message  
Reply

Tags
count, linked, list, recursion

Message:




Views: 883 | Replies: 6
Thread Tools Search this Thread



Tag cloud for count, linked, list, recursion
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC