Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #2183
| Message-ID | <504B931C.541A@mindspring.com> (permalink) |
|---|---|
| Date | 2012-09-08 14:49 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.programming |
| Subject | Re: linked list |
| References | <k2b694$et0$1@speranza.aioe.org> |
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
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