0

I have this function, and I want to change it to an iterative one. Does anyone know how to do it?

#include "list.h"

int count(LINK head)
{

if(head == NULL)
   return 0;
else
   return (1 + count(head -> next));
}
Dan D.
  • 73,243
  • 15
  • 104
  • 123
Jacob
  • 39
  • 2
  • 2
    This smells like homework to me. Have you even tried? – Andrew Shepherd Apr 27 '11 at 01:51
  • 1
    Yes, I know how to do it, and you could too if you tried. – Jim Balter Apr 27 '11 at 02:17
  • possible duplicate of [Can every recursion be converted into iteration?](http://stackoverflow.com/q/931762/), [Design patterns for converting recursive algorithms to iterative ones](http://stackoverflow.com/q/1549943/) – outis Jun 20 '12 at 22:43

1 Answers1

5
int count(LINK head)
{
    int count = 0;
    while(head != NULL)
    {
        head = head->next;
        count = count + 1;
    }
    return count;
}
Dan D.
  • 73,243
  • 15
  • 104
  • 123