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


Groups > comp.programming > #2174

Re: linked list

Message-ID <50492222.5F6D@mindspring.com> (permalink)
Date 2012-09-06 18:22 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.programming
Subject Re: linked list
References <k2b694$et0$1@speranza.aioe.org>

Show all headers | View raw


Bill Cunningham wrote:
> 
>     Can someone show me a very simple example
> of a linked list in C?  The simplest possible.




/* BEGIN d_append.c */
/*
** Demonstration of use of int list functions.
*/
#include <stdio.h>
#include <stdlib.h>

#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;

int d_fprintf(const d_type *node, FILE *stream);
d_type *d_append(d_type **head, d_type *tail, int data);
void d_free(d_type *node);

int 
main(void)
{
    d_type *head = NULL;
    d_type *tail = NULL;
    int numbers[] = {NUMBERS};
    int *ptr = numbers;
    int *const after = numbers + NMEMB(numbers);

    puts("/* BEGIN d_append.c output */");
    puts("\nOriginal order of list of ints:");
    do {
        tail = d_append(&head, tail, *ptr);
        if (tail == NULL) {
            puts("malloc trouble!");
            break;
        }
    } while (++ptr != after);
    d_fprintf(head, stdout);
    d_free(head);
    puts("\n/* END d_append.c output */");
    return 0;
}

int 
d_fprintf(const d_type *node, FILE *stream)
{
    int rc = 0;
    
    while (node != NULL) {
        if (0 > (rc = fprintf(stream, "%d\n", node -> data))) {
            break;
        }
        node = node -> next; 
    }
    return rc;
}

d_type *
d_append(d_type **head, d_type *tail, int data)
{
    d_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
        node -> next = NULL;
        node -> data = data;
        if (*head != NULL) {
            tail -> next = node;  
        } else {
            *head = node;
        }
    }
    return node;
}

void 
d_free(d_type *node)
{
    d_type *next_node;

    while (node != NULL) {
        next_node = node -> next;
        free(node);
        node = next_node;
    }
}

/* END d_append.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