单链表自动生成函数

#include 
#include 
#include 
typedef struct SLHead_struct { //single list head
    char * sl_mbr_ptr;//
    unsigned int total_elements;
} SLHeadType;
//入参自动生成宏
#define __STD_PARAM1(t1)      (tmp->t1==the_ele->t1)
#define __STD_PARAM2(t2, ...) (tmp->t2==the_ele->t2)&&__STD_PARAM1(__VA_ARGS__)
#define __STD_PARAM3(t3, ...) (tmp->t3==the_ele->t3)&&__STD_PARAM2(__VA_ARGS__)
#define __STD_PARAM4(t4, ...) (tmp->t4==the_ele->t4)&&__STD_PARAM3(__VA_ARGS__)
#define __STD_PARAM5(t5, ...) (tmp->t5==the_ele->t5)&&__STD_PARAM4(__VA_ARGS__)
#define __STD_PARAM6(t6, ...) (tmp->t6==the_ele->t6)&&__STD_PARAM5(__VA_ARGS__)
//单链表插入函数自动生成宏
#define DEFINE_SINGLE_LIST_INSERT_FUN(the_func_name, node_type, x,...)    \
int the_func_name(SLHeadType * _list_head, node_type * the_ele)           \
{                                                                         \
    node_type * new_ele_ptr  = (node_type *)malloc(sizeof(node_type));    \
    if(new_ele_ptr) {                                                     \
        *new_ele_ptr = *the_ele;                                          \
        new_ele_ptr->next = (node_type *)_list_head->sl_mbr_ptr;          \
        _list_head->sl_mbr_ptr = (char *)new_ele_ptr;                     \
        _list_head->total_elements++;                                     \
        return 0;                                                         \
    }                                                                     \
    return -4;                                                            \
}
//单链表删除函数自动生成宏
#define DEFINE_SINGLE_LIST_DELETE_FUN(the_func_name, node_type, x,...)        \
int the_func_name(SLHeadType * _list_head, node_type * the_ele)               \
{                                                                             \
    node_type * tmp  = (node_type *)_list_head->sl_mbr_ptr;                    \
    node_type ** prv = (node_type **)&_list_head->sl_mbr_ptr;                 \
    while(tmp) {                                                              \
        if(__STD_PARAM##x(__VA_ARGS__)) {                                     \
            *prv = tmp->next;                                                  \
            free((char *)tmp);                                       \
            _list_head->total_elements--;                                     \
            return 0;                                                         \
        }                                                                     \
        prv = &tmp->next;                                                      \
        tmp = tmp->next;                                                       \
    }                                                                         \
    return -7;                                                                \
}
//单链表查找函数自动生成宏
#define DEFINE_SINGLE_LIST_LOOKUP_FUN(the_func_name, node_type, x,...)        \
node_type * the_func_name(SLHeadType * _list_head, node_type * the_ele)       \
{                                                                             \
    node_type * tmp = (node_type *)_list_head->sl_mbr_ptr;                    \
    while(tmp) {                                                              \
        if(__STD_PARAM##x(__VA_ARGS__)) {                                     \
            return tmp;                                                       \
        }                                                                     \
        tmp = tmp->next;                                                      \
    }                                                                         \
    return NULL;                                                              \
}

typedef struct SingleListMbrStruct {
    struct SingleListMbrStruct * next;
    int ip;
    int mask;
    int id;
    int data;
} SingleListMbrType;
//定义查找函数
DEFINE_SINGLE_LIST_INSERT_FUN(single_list_insert_fun, SingleListMbrType, 2,ip,mask);
DEFINE_SINGLE_LIST_DELETE_FUN(single_list_delete_fun, SingleListMbrType, 2,ip,mask);
DEFINE_SINGLE_LIST_LOOKUP_FUN(single_list_lookup_fun, SingleListMbrType, 2,ip,mask);

typedef struct SingleListTestStruct {
    SLHeadType user;
} SingleListTestType;

#define LIST_TOL_NUM  10
show_list_all(SingleListTestType * slt){
    //打印当前链表里的总数,并遍历每个成员
    printf("\n ********print all member start***** \n");
    SingleListMbrType  * tmp_ptr = NULL;
    printf("total num =%d \n",slt->user.total_elements);
    tmp_ptr = (SingleListMbrType  *)slt->user.sl_mbr_ptr;
    while(tmp_ptr){
        printf("ip=%d,mask=%d data=%d \n",tmp_ptr->ip,tmp_ptr->mask,tmp_ptr->data);
        tmp_ptr = tmp_ptr->next;
    }
    printf(" ********print all member end***** \n");
}
int main(){
    int i = 0;
    int rv = 0;
    SingleListTestType  slt = {{0}};
    SingleListMbrType   slt_mbr ={0};
    SingleListMbrType  * tmp_ptr = NULL;
    //插入n个元素,
    for(i = 0; i < LIST_TOL_NUM ;i++){
        slt_mbr.ip = i;
        slt_mbr.mask = i+10;
        slt_mbr.data = 2*i;
        rv = single_list_insert_fun(&slt.user,&slt_mbr);
    }
    printf("遍历一下刚刚插入的数据 \n");
    show_list_all(&slt);

    printf("\n测试查找函数,对应数据链表中存在,执行结果: \n");
    slt_mbr.ip = 3;
    slt_mbr.mask = 13;
    tmp_ptr = single_list_lookup_fun(&slt.user, &slt_mbr);
    if(tmp_ptr){
        printf("查找到对应数据:ip=%d,mask=%d data=%d \n",tmp_ptr->ip,tmp_ptr->mask,tmp_ptr->data);
    }else{
        printf("not found \n");
    }

    printf("\n测试查找函数,对应数据链表中不存在 执行结果:\n");
    slt_mbr.ip = 3;
    slt_mbr.mask = 13;
    tmp_ptr = single_list_lookup_fun(&slt.user, &slt_mbr);
    if(tmp_ptr){
        printf("查找到对应数据:ip=%d,mask=%d data=%d \n",tmp_ptr->ip,tmp_ptr->mask,tmp_ptr->data);
    }else{
        printf("not found \n");
    }

    printf("\n测试要删除的数据是头节点 执行结果:\n");
    slt_mbr.ip = 9;
    slt_mbr.mask = 19;
    rv = single_list_delete_fun(&slt.user, &slt_mbr);
    printf("删除结果 rv = %d \n",rv);
    printf("删除后当前链表的情况");
    show_list_all(&slt);

    printf("\n测试要删除的数据是中间节点 执行结果:\n");
    slt_mbr.ip = 5;
    slt_mbr.mask = 15;
    rv = single_list_delete_fun(&slt.user, &slt_mbr);
    printf("删除结果 rv = %d \n",rv);
    printf("删除后当前链表的情况");
    show_list_all(&slt);

    printf("\n测试要删除的数据不存在 执行结果:\n");
    slt_mbr.ip = 4;
    slt_mbr.mask = 15;
    rv = single_list_delete_fun(&slt.user, &slt_mbr);
    printf("删除结果 rv = %d \n",rv);
    printf("删除后当前链表的情况");
    show_list_all(&slt);
    return 0;
}




你可能感兴趣的:(单链表自动生成函数)