Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 06 Sep 2012 19:52:16 -0500 Message-ID: <50494540.4AA5@mindspring.com> Date: Thu, 06 Sep 2012 20:52:16 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.programming Subject: Re: linked list References: <50492222.5F6D@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 154 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.217.228 X-Trace: sv3-KphdIt8Z0NHGrDAU2dOMkYP1ZF1Js9dQ05oDKpkkhyX9csGx/fOE6vkWfKeAz/dEH40KL3uNzMleDLn!AGYAKzzjewsDE2wH8+0l2XSuyy5t/DPingUi/oTaMH8LXGjkeMxG4mvLbux320vzvphfdr1Gbh2F!xAzjsC7JJ0E= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4294 Xref: csiph.com comp.programming:2176 Bill Cunningham wrote: > > pete wrote: > > > > /* BEGIN d_append.c */ > > /* > > ** Demonstration of use of int list functions. > > */ > > #include > > #include > > > > #define NUMBERS 15,14,13,7,20,9,8,12,11,6 > > > > #define NMEMB(A) (sizeof (A) / sizeof *(A)) > > > > struct d_node { > > struct d_node *next; > > int data; > > }; > > > > typedef struct d_node d_type; > > Wow if this is simple I'd hate to see complex. > Whew. One step at a time > I guess. > > > int d_fprintf(const d_type *node, FILE *stream); > > d_type *d_append(d_type **head, d_type *tail, int data); > > I've never worked with pointers to pointers. > Except in the command line. > What's head and tail for? head points to the first node of the linked list. tail points to the last node of the linked list. >I only used next. What can you do with a linked list, by only using next? I simplified it: /* BEGIN d_start.c */ /* ** Demonstration of use of int list functions. ** d_start ** d_append_0 */ #include #include #define NUMBERS 15,14,13,7,20,9,8,12,11,6 #define NMEMB(A) (sizeof (A) / sizeof *(A)) struct d_node { struct d_node *next; int data; }; int d_fprintf(const struct d_node *node, FILE *stream); struct d_node *d_start(int data); struct d_node *d_append_0(struct d_node *tail, int data); void d_free(struct d_node *node); int main(void) { struct d_node *head; struct d_node *tail; int numbers[] = {NUMBERS}; int *ptr = numbers; int *const after = numbers + NMEMB(numbers); puts("/* BEGIN d_start.c output */"); puts("\nOriginal order of list of ints:"); head = tail = d_start(*ptr); if (head != NULL) { while (++ptr != after) { tail = d_append_0(tail, *ptr); if (tail == NULL) { puts("malloc trouble!"); break; } } } else { puts("malloc trouble!"); } d_fprintf(head, stdout); d_free(head); puts("\n/* END d_start.c output */"); return 0; } int d_fprintf(const struct d_node *node, FILE *stream) { int rc = 0; while (node != NULL) { if (0 > (rc = fprintf(stream, "%d\n", node -> data))) { break; } node = node -> next; } return rc; } struct d_node * d_start(int data) { struct d_node *node; node = malloc(sizeof *node); if (node != NULL) { node -> next = NULL; node -> data = data; } return node; } struct d_node * d_append_0(struct d_node *tail, int data) { struct d_node *node; node = malloc(sizeof *node); if (node != NULL) { node -> next = NULL; node -> data = data; tail -> next = node; } return node; } void d_free(struct d_node *node) { struct d_node *next_node; while (node != NULL) { next_node = node -> next; free(node); node = next_node; } } /* END d_start.c */ -- pete