Bill said:
Is this a very simple example of a linked list
or is the concept I am
expressing in error again.
I don't know.
Here's a fairly simple example of a linked list:
/* BEGIN Lf_start.c */
/*
** Demonstration of use of long double list functions.
** Lf_start
** Lf_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 Lf_node {
struct Lf_node *next;
long double data;
};
typedef struct Lf_node Lf_type;
int Lf_fprintf(const Lf_type *node, FILE *stream);
Lf_type *Lf_start(long double data);
Lf_type *Lf_append_0(Lf_type *tail, long double data);
void Lf_free(Lf_type *node);
int main(void)
{
Lf_type *head;
Lf_type *tail;
long double numbers[] = NUMBERS;
long double *ptr = numbers;
long double *const after = numbers + NMEMB(numbers);
puts("/* BEGIN Lf_start.c output */");
puts("\nOriginal order of list of long doubles:");
head = tail = Lf_start(*ptr);
if (head != NULL) {
while (++ptr != after) {
tail = Lf_append_0(tail, *ptr);
if (tail == NULL) {
puts("malloc trouble!");
break;
}
}
} else {
puts("malloc trouble!");
}
Lf_fprintf(head, stdout);
Lf_free(head);
puts("\n/* END Lf_start.c output */");
return 0;
}
int Lf_fprintf(const Lf_type *node, FILE *stream)
{
int rc = 0;
while (node != NULL) {
if (0 > (rc = fprintf(stream, "%Lf\n", node -> data))) {
break;
}
node = node -> next;
}
return rc;
}
Lf_type *Lf_start(long double data)
{
Lf_type *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = data;
}
return node;
}
Lf_type *Lf_append_0(Lf_type *tail, long double data)
{
Lf_type *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = data;
tail -> next = node;
}
return node;
}
void Lf_free(Lf_type *node)
{
Lf_type *next_node;
while (node != NULL) {
next_node = node -> next;
free(node);
node = next_node;
}
}
/* END Lf_start.c */