> 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;
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? I only used next. I don't want to try interation and reversing functions yet.
>> 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?
Thanks much for your help Pete. But maybe I should stay away from linked lists for now. I thought that they might be simpler than a binary tree but my C is not up to par enough to handle such things IMO. But I will save your code and I will study it in my own time. Do I have your permission to repost this code giving you credit of course if I have questions later with other about linked lists? This is kind of a slow list anyway so I might study and repost it here.
Bill Cunningham wrote:
> Do I have your permission to repost
> this code giving you credit of course
> if I have questions later with other
> about linked lists?
Yes.
All that that last program does is to declare a list node type
struct d_node {
struct d_node *next;
int data;
};
and then to make a linked list by using two functions.
> 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;