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


Groups > comp.programming > #2176

Re: linked list

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

Show all headers | View raw


Bill Cunningham wrote:
> 
> pete wrote:
> >
> > /* 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;
> 
>     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 <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;
};

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

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