Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "helper.h"
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include "slist.h"
00025
00028 SingleList* slist_init( void *content ){
00029 SingleList *el;
00030 MALLOC( el, 1, SingleList );
00031 el->content=content;
00032 el->next=NULL;
00033 return el;
00034 }
00035
00038 SingleList* slist_append( SingleList *l ){
00039 if( l==NULL ){
00040 l = (SingleList*)malloc( sizeof(SingleList) );
00041 return l;
00042 } else {
00043 while( l->next!=NULL ) l=l->next;
00044 l->next = slist_init(NULL);
00045 return l->next;
00046 }
00047 }
00055 SingleList* slist_index( const SingleList *l, int idx ){
00056 SingleList *idxn=l;
00057 int i;
00058 for( i=0; i<idx; i++ ){
00059 if( idxn==NULL ){
00060 return idxn;
00061 }
00062 idxn=idxn->next;
00063 }
00064 return idxn;
00065 }
00066
00069 void slist_print( FILE *out, const SingleList *l, void(*print_content)(FILE*,void*) ){
00070 if( l==NULL ){
00071 fprintf( out, "*\n" );
00072 return;
00073 }
00074 (*print_content)(out,l->content);
00075 fprintf( out, "->" );
00076 slist_print( out, l->next, print_content );
00077 }
00078
00081 int slist_length( const SingleList *l ){
00082 if( l==NULL )
00083 return 0;
00084 else
00085 return 1+slist_length(l->next);
00086 }
00087
00091 void slist_free( SingleList *l ){
00092 if( l==NULL ) return;
00093 slist_free( l->next );
00094 free( l );
00095 }