hey guys. ive been asked to do this:
Write a recursive function which returns true if the linked list is sorted in
ascending order.
bool isSorted(nodeType *L)
why would we use a recursive function to check if it's in ascending order? can anyone help me with this.
If I were to write it, I doubt I would use recursion, though you certainly could use it. You could write it recursively very similarly to using a while loop:
while (L != NULL)
{
if (some condition)
{
// return true or false
}
L = L->next;
}
// return true or false
The same logic can be tweaked for recursion. Get rid of the while loop. Every trip through the current while loop is a new call to the function. Your base condition to get out of infinite recursion is the same as your while loop condition.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
>why would we use a recursive function to check if it's in ascending order?
It's just another poor example of teachers trying to get students to use recursion. In most cases the solution to the assignment they hand out would be much better suited to a loop, and the result is that kids know how to use recursion, but don't know when to use it. Theoretically, any recursion can be turned into a loop, but situations do exist out there where recursion actually can provide a simpler solution -- it's just somewhat difficult for teachers to create assignments which demonstrate that.
Go with what VernonDozier suggested about doing it with a loop first, then adding recursion once you've got the loop working. It'll be much simpler to think that way.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
It's just another poor example of teachers trying to get students to use recursion. In most cases the solution to the assignment they hand out would be much better suited to a loop, and the result is that kids know how to use recursion, but don't know when to use it. Theoretically, any recursion can be turned into a loop, but situations do exist out there where recursion actually can provide a simpler solution -- it's just somewhat difficult for teachers to create assignments which demonstrate that.
It is difficult to create assignments on recursion in a conventional course because generally recursive solutions are best suited to more advanced data structures (braun trees, directed graphs, etc) which are likely beyond the scope of a first programming course...so you end up learning recursion just for the sake of satisfying the curriculum.
The issue is of course, once you've learned looping, recursion is given to you as an afterthought, and you become so entrenched in looping iterations that thinking recursively is much more difficult, and seemingly pointless. The other thing is, I don't believe that any C++ compiler optimizes for structural or mutual recursion, only tail recursion for speed reasons (i.e. on a standard C++ compiler, structural/mutual recursion are slower and very memory inefficient...if you have ever played around with recursion then you've no doubt gotten a stack overflow error if you use C/C++).
But of course many patterns are recursive in nature...sequences and series and other mathematical patterns (think fibonacci). And, if you learn it properly (at least in my opinion), it is sometimes much easier to think about problems recursively, and then convert into loops...
In any case, the key to using recursion effectively is to start with the base case(s). Once you have a place for the definition to terminate, then set up the recursive call (making the necessary preparations)...Think about it logically, trace the program steps and ensure that each recursive call is moving a little closer to the base case...and then have a little faith ;)
You could also learn Scheme for practice (with recursion)...unbounded data ftw...lol
EDIT: And I don't believe that recursive functions need to have parameters to be classified as recursive...I just think you didn't use the proper function prototype in your solution
n1337
Junior Poster in Training
96 posts since May 2008
Reputation Points: 55
Solved Threads: 10