Finding Linked Lists size

I need to find the size of a linked list recursively and iteratively
Here is what I have for each
Do these look okay?
1
2
3
4
5
6
7
8
9
 int GetCurrentSize(node* p)
{
        if (p==NULL)
                return 0;
        else{
             return 1 + GetCurrentSize(p->next_node);
             }

}

1
2
3
4
5
6
7
8
int GetCurrentSize(node* cur){
    int count;
    while(cur != NULL){     
        cur->next_node;
        count++
        }
return count;
}
Last edited on
They look fine. Do they work? You may have to verify that they work.

If you write any sort of linked list class or struct yourself, you should probably manually keep track of size. That way when you are asked what size it is, you can give the answer immediately rather than trying to find out yourself.
Topic archived. No new replies allowed.