home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!spool.mu.edu!agate!doc.ic.ac.uk!uknet!strath-cs!ps
- From: ps@cs.strath.ac.uk (Paul Shaw)
- Newsgroups: comp.lang.c
- Subject: Re: Link Lists - Malloc - Help
- Message-ID: <10887@baird.cs.strath.ac.uk>
- Date: 6 Nov 92 08:30:59 GMT
- References: <1992Oct27.220720.3645@stortek.com> <2178@ontek.com> <Bx72EG.9JG@sci.kun.nl>
- Organization: Comp. Sci. Dept., Strathclyde Univ., Glasgow, Scotland.
- Lines: 64
-
- In article <Bx72EG.9JG@sci.kun.nl> hansm@cs.kun.nl (Hans Mulder) writes:
- >In <2178@ontek.com> mikey@ontek.com (euphausia superba) writes:
- >
- >You _can_ avoid making this distinction, if you're willing to throw
- >another level of pointers at it:
- >
- > zombie = search_list(head); /* identify item to delete */
- > if (zombie != NULL) {
- > struct item **ptr = &head;
- > while (*ptr != zombie)
- > ptr = &((*ptr)->next);
- > *ptr = (*ptr)->next;
- > free((void*)zombie);
- > }
- >
- >Is having only one case worth the confusion generated by using pointers
- >to pointers? I really don't know.
-
- I don't think so, its not very easy to read. Why not just do:
-
- typedef struct DEMO_LIST {
- /* Other stuff */
- struct DEMO_LIST *next;
- }DEMO_LIST;
-
- DEMO_LIST *delete_item(l,item) DEMO_LIST *l,*item;
- {
- if (l) {
- if (l == item) {
- DEMO_LIST *t;
- t = l->next; free((void *)l); return(t);
- }
- else {
- l->next = delete_item(l->next,item); return(l);
- }
- }
- else {
- /* Getting here means the item did not exist in the list. You
- * can stick what code you want in here, either return NULL (l) if this
- * is OK or do some error handling stuff.
- */
- return(l);
- }
- }
-
- Call this code by:
-
- list = delete_item(list,item);
-
- I also like my list functions returning the new list as this allows things
- like:
-
- list = delete_item(delete_item(l,item1),item2);
-
- I don't want to get into a flame war about recursion and efficiency here, but
- certainly to me, the recusive version is easier to read (no complex
- dereferences etc.), and more likely to be bug free.
-
- Just another view.....
-
- Paul Shaw,
- Dept of Computer Science,
- University of Strathclyde,
- Glasgow.
-