home *** CD-ROM | disk | FTP | other *** search
- From: Neal.Sanche@ttlg.UUCP (Neal Sanche)
- Sender: postmaster@ttlg.UUCP
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!cs.ubc.ca!alberta!nebulus!ttlg!postmaster
- Newsgroups: comp.lang.c
- Subject: Re: To Recurse or NOT to
- Message-ID: <721474484.0@ttlg.ttlg.UUCP>
- Date: 10 Nov 92 22:24:06 mst
- Lines: 27
-
- -U> > What is better to do when traversing a linked list..... SHould I use
- -U> > recursion to search down through the list for the last node, or should I
- -U> use a while loop? What or the pro's and con's of each?
-
- A while loop is probably the best and is really the easiest to program.
- It can be done quickly as follows:
-
- list *mylist;
- list *tail;
-
- tail = mylist;
- while (tail->next) {
- tail = tail->next;
- }
-
- This will leave tail pointing to the last element in the list.
- The only space require on the stack for this implementation in
- the space for one pointer (4 bytes in large model) whereas
- with recursion you are looking at n times as much, where n is
- the number of elements in the list. And as Rick pointed out,
- a lot more time wasted via function calls and so on.
-
- -Neal
- ___
- X OLX 2.1 TD X I give up, what is the meaning of life?
-
- * Origin: Paradigm BBS: A programmer's paradise. (42:100/11)
-