Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.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: Sat, 08 Sep 2012 13:49:01 -0500 Message-ID: <504B931C.541A@mindspring.com> Date: Sat, 08 Sep 2012 14:49:00 -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: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 63 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.57 X-Trace: sv3-p9UDhkZoTZoJtau1uGCPvQV2LeILtPkzteGMarYSXTi5iBGV6wo5HtS//ALYOVc0/IPvyydAdHoqOmh!kpqyBRA5hiRcDvqDZJQf3Ct79Oe5GSeDC57eayILmXN2MWY2N/0uvoqeebeREep3TPBDAM6RfZCL!PPSzTHzj3Q== 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: 2524 X-Received-Bytes: 2665 Xref: csiph.com comp.programming:2183 Bill Cunningham wrote: > > Can someone show me a very simple example > of a linked list in C? > The simplest possible. This is as simple as I could make it, without getting hung up on the meaning of the word "simple". /* BEGIN list_linked.c */ #include #include int main(void) { struct list { struct list *next; int data; }; struct list *head; struct list *tail; int number[] = {15,14,13,7,20,9,8,12,11,6}; unsigned index; puts("/* BEGIN list_linked.c output */\n"); puts("Original order of list of int:"); head = malloc(sizeof *head); tail = head; if (tail != NULL) { index = 0; tail -> data = number[index]; while (sizeof number / sizeof *number > ++index) { tail -> next = malloc(sizeof *(tail -> next)); if (tail -> next == NULL) { puts("malloc trouble!"); break; } tail = tail -> next; tail -> data = number[index]; } tail -> next = NULL; } else { puts("malloc trouble!"); } for (tail = head; tail != NULL; tail = tail -> next) { printf("%d\n", tail -> data); } while (head != NULL) { tail = head -> next; free(head); head = tail; } puts("\n/* END list_linked.c output */"); return 0; } /* END list_linked.c */ -- pete