Results 1 to 3 of 3
  1. #1
    hussien is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default count elements in linked list recursively!

    Good morning,
    I was asked to implement a method which I can count elements in linked list recursively.I tried and it compiles but whenever I run I found stackoverflow error.
    here is my trial
    public int countRec(){
    return countRec1(head);
    }
    public int countRec1(Link current){
    current = head ;
    if ( current == null ){
    return 0 ;

    }
    else{

    return 1 +countRec1(current.next);
    }
    any help please? :(

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: count elements in linked list recursively!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: count elements in linked list recursively!

    That means you have some infinite recursion happening. Verify your base case, and also that your recursive call is in fact traversing the list and not just looping over the same item forever.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Outputting Elements of a Linked List
    By Grimey in forum New To Java
    Replies: 6
    Last Post: 06-10-2012, 08:03 PM
  3. How would I count number of nodes in linked list?
    By colerelm in forum New To Java
    Replies: 2
    Last Post: 10-17-2011, 06:26 AM
  4. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  5. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •