diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/EXAMPLES/linklst2.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/EXAMPLES/linklst2.c')
-rw-r--r-- | reference/C/EXAMPLES/linklst2.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/linklst2.c b/reference/C/EXAMPLES/linklst2.c new file mode 100644 index 0000000..d019454 --- /dev/null +++ b/reference/C/EXAMPLES/linklst2.c @@ -0,0 +1,112 @@ + +/************************************************************************ + * + * Purpose: To demonstrate 'linked lists' This program will build a + * linked list and place data into it. When the data is exausted + * the contents of the list are O/P. + * + * This is a "First in First out" (FIFO) list. + * + * Author: M. J. Leslie + * + * Date: 11-May-95 + * + ************************************************************************/ + +#include <stdlib.h> /* malloc */ + +/************************************************************************/ + +struct x { /* Declare a structure */ + char name[20]; + int age; + struct x *next_rec; +}; + +/************************************************************************/ + +main() +{ + struct x *start_pointer; /* Define pointers to the structure */ + struct x *next_pointer; + + /* Create some data to be placed in the + * Linked list. */ + char *names[]= + { + "Martin", + "John ", + "Alex ", + "" + }; + + int ages[]={32, 43, 29, 0}; + + int count=0; /* General purpose counter. */ + + /*===================================================================* + = = + = Build a LINKED LIST and place data into it. = + = = + *===================================================================*/ + + /* Initalise 'start_pointer' by reserving + * memory and pointing to it */ + + start_pointer=(struct x *) malloc (sizeof (struct x)); + + /* Initalise 'next_pointer' to point + * to the same location. */ + next_pointer=start_pointer; + + /* Put some data into the reserved + * memory. */ + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + + + /* Loop until all data has been read */ + + while ( ages[++count] != 0 ) + { + /* Reserve more memory and point to it */ + + next_pointer->next_rec=(struct x *) malloc (sizeof (struct x)); + + next_pointer=next_pointer->next_rec; + + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + } + + next_pointer->next_rec=NULL; + + /*===================================================================* + = = + = Traverse the linked list and O/P all the data within it. = + = = + *===================================================================*/ + + + next_pointer=start_pointer; + + while (next_pointer != NULL) + { + printf("%s ", next_pointer->name); + printf("%d \n", next_pointer->age); + next_pointer=next_pointer->next_rec; + } +} + +/************************************************************************ + * + * Program results. + * + * Martin 32 + * John 43 + * Alex 29 + * + ************************************************************************/ + |