help in link list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vandalous
    New Member
    • Sep 2006
    • 7

    help in link list

    hello . ive got a simple question.
    how do i create a method in my class which tells me the size of my list

    ..............
    int size(void){
    int s=0;
    for (pos = begin(); pos.not_equal(e nd()); pos.next())
    {s++;}
    return s;
    }
    ...............

    here is the methods for begin and end:

    Iterator List::begin()
    { Iterator iter;
    iter.position = first;
    iter.last = last;
    return iter;
    }

    Iterator List::end()

    { Iterator iter;
    iter.position = NULL;
    iter.last = last;
    return iter;
    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    STL already has a list class, and all of this is implemented already. I see you are using iterators, you could do that. However, for your code, you should be getting an infinite loop? You aren't updating the position cursor after each loop. It sits at the first element counting it infinitely many times.
    Code:
    int size(void)
    {
      int s=0;
      for (pos = begin(); pos.not_equal(end()); [b]pos = [/b]pos.next())
        s++;
      return s;
    }

    Comment

    • vandalous
      New Member
      • Sep 2006
      • 7

      #3
      thanks for your help . i got it right

      Comment

      Working...