单链表自动生成函数
来源:互联网 发布:linux 查看硬盘使用率 编辑:程序博客网 时间:2024/05/23 20:44
#include <stdio.h>#include <stdlib.h>#include <memory.h>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 vpnid; 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 10show_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;}
0 0
- 单链表自动生成函数
- 单链表 查找函数自动生成宏
- sql 自动生成编号函数
- sql自动生成编码函数
- 编辑器自动生成的函数
- 主键字母自动生成函数
- C++ 类自动生成函数
- python:inspect函数自动生成函数名
- 文件名命名相关函数,自动生成HTML
- asp自动生成javascript检验函数
- asp自动生成javascript检验函数1
- EffictiveC++屏蔽编译器自动生成的函数
- QT自动生成槽函数命名规则
- Oracle自动生成编号的函数
- 使用函数自动生成n层目录
- 为何编译器无法自动生成拷贝函数
- 拒绝编译器自动生成的函数
- eclipse中如何自动生成构造函数
- SpringMVC-框架介绍
- 初探java虚拟机
- Color POJ
- 关于开机出现“安装程序正在为首次使用计算机做准备”的解决方案及微软OOBE与SYSPREP的实用技巧
- UART遇到两个与 stty 相关问题
- 单链表自动生成函数
- Geode多节点集群实验
- ios 使用自带的UIViewController实现qq加号下拉菜单的功能
- (leetcode) Keyboard Row
- Python安装Anaconda出现Error opening file for writing错误
- 程序员技术怀旧_晒晒那些过去的经典
- ReactiveCocoa学习篇——【下】
- poj 2355 DP 单调队列优化
- 一次涉及到物化视图的DB2 Insert性能问题的诊断