• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/slist.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008-2010 by Matthias Ihrke   *
00003  *   mihrke@uni-goettingen.de   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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 }

Generated on Fri Jun 25 2010 14:10:20 for libeegtools by  doxygen 1.7.0