home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!darwin.sura.net!spool.mu.edu!olivea!news.bbn.com!papaya.bbn.com!cbarber
- From: cbarber@bbn.com (Chris Barber)
- Newsgroups: comp.lang.c
- Subject: Re: To Recurse or NOT to recurse?
- Message-ID: <4223@litchi.bbn.com>
- Date: 9 Nov 92 17:25:00 GMT
- References: <1992Nov6.185136.8055@netcom.com>
- Organization: BBN Systems and Technology, Inc.
- Lines: 20
-
- In article <1992Nov6.185136.8055@netcom.com> sjs@netcom.com
- (Stephen Schow) writes:
- >What is better to do when traversing a linked list..... SHould I use recursion
- >to search down through the list for the last node, or should I use a while
- >loop? What or the pro's and con's of each?
-
- Recursion should usually be avoided in C for reasons of efficiency. A
- recursive linked-list traversal will require much greater overhead both for
- CPU cycles (because of the instructions needed to make function calls) and
- storage space (because of the use of the stack) than a while-loop traversal.
- However, recursion can be appropriate if it greatly simplifies the code and
- will not need to recurse very many levels, thus minimizing the cost of the
- overhead.
-
- This advice does not apply to code written in Lisp and other languages that
- optimize tail-recursion.
-
- --
- Christopher Barber
- (cbarber@bbn.com)
-