Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #2183

Re: linked list

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> (permalink)
Date Sat, 08 Sep 2012 14:49:00 -0400
From pete <pfiland@mindspring.com>
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 <k2b694$et0$1@speranza.aioe.org>
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

Show key headers only | View raw


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 <stdio.h>
#include <stdlib.h>

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

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 17:58 -0400
  Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 18:04 -0400
  Re: linked list pete <pfiland@mindspring.com> - 2012-09-06 18:22 -0400
    Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 18:28 -0400
      Re: linked list pete <pfiland@mindspring.com> - 2012-09-06 20:52 -0400
        Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-07 17:25 -0400
          Re: linked list pete <pfiland@mindspring.com> - 2012-09-08 09:35 -0400
  Re: linked list pete <pfiland@mindspring.com> - 2012-09-08 14:49 -0400
  Re: linked list Pascal J. Bourguignon <pjb@informatimago.com> - 2012-10-06 01:32 +0000

csiph-web