Generic list manipulation function in C?

后端 未结 7 784
独厮守ぢ
独厮守ぢ 2020-12-16 05:53

What is a generic list manipulation function in C? (I saw this when I was going through some materials.)

What is the difference between this function and a function

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 06:39

    For my teachings I came to develop this "generic" list module, probably a simplify version of the linux kernel one, with additional though undiscovered bugs included, and that uses gcc extensions... Any comments are welcomed !

    #ifndef _LISTE
    #define _LISTE
    #include 
    typedef struct liste_s {
      struct liste_s * suivant ;
    } * liste ;
    
    
    #define newl(t) ( (liste) malloc ( sizeof ( struct liste_s ) + sizeof ( t ) ) )
    #define elt(l,t) ( * ( ( t * ) ( l + 1 ) ) )
    
    #define liste_vide NULL
    #define videp(l) ( l == liste_vide )
    #define lvide() liste_vide
    #define cons(e,l) \
      ({ liste res = newl(typeof(e)) ;      \
         res->suivant = l ; \
         elt(res,typeof(e)) = e ;           \
        res ; }) 
    
    #define hd(l,t) ({ liste res = l ; if ( videp(res) ) exit ( EXIT_FAILURE ) ; elt(res,t) ; })
    #define tl(l)   ({ liste res = l ; if ( videp(res) ) exit ( EXIT_FAILURE ) ; res->suivant ;})
    
    
    #endif
    

提交回复
热议问题