home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: To Recurse or NOT to recurse?
- Message-ID: <BxBFrw.2p9@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov6.185136.8055@netcom.com>
- Date: Fri, 6 Nov 1992 22:28:43 GMT
- Lines: 32
-
- 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?
- :
- : Thanks in advance
- : ------------------------------------------------------------------
- Stephen,
- If you only want to get to the end of the linked list, definitely use
- a while loop. Recursion in a linked list would eat up alot of stack space
- and funtion calling overhead for such a simple process.
-
- Recursion would, however, be the choice if you need to do an operation that
- needs to keep a state constant. Menuing functions are good examples of this.
- If one menu calls another, you want the first one to remain in the same state
- when the second one gets aborted and the first one becomes active again.
- Without recursion, this would be a mess to try to keep all of the
- information active in one instance of a function. Instead, you let
- each instance keep track of only what it needs to.
-
- although there probably are exceptions, the only good time for recursion
- is when code will be reused and needs to keep each state of its instance
- completely independant of the others.
-
- back-and-forth wandering through the linked list would be a good case for
- this, but just locating the end of the list would be much easier and
- faster with a while loop.
-
- dave fuller
- dfuller@portal.hq.videocart.com
-
-
-