Hi, so basically I made a singly-linked list and to navigate through it I have two methods that I've proposed, either iteratively or recursively. Both work but apparently my recursive function is not tail-recursive so it is eating up all my stack memory. Ideally I want to make use of the recursive search function rather than the iteration. So here's what I have:

__Recursive Search__

Code:

node rec_search (node n, int i)
{
if (i == 0)
{
return n;
}
else
{
return rec_search (n->next, i-1);
}
}

__Iterative Search__

Code:

node rec_search (node n, int i)
{
while (i > 0)
{
n = n->next;
i--;
}
return n;
}

I thought that my recursive function was tail-recursive to use constant space but apparently it's not. So if anyone could help me re-write this to not use anymore stack memory as I need that would much appreciated!

Thank you