#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include "sglib.h" typedef struct _ip_mask_map { int first; int second; struct _ip_mask_map* next; }ip_mask_map,*pip_mask_map; int ipcomp(ip_mask_map* elem1,ip_mask_map* elem2) { if ((elem1->first == elem2->first) && (elem1->second == elem2->second)) { return 0; } else { return 1; } } int main(int argc, char* argv[]) { ip_mask_map *elem,*head=NULL,*_member; elem = (pip_mask_map)malloc(sizeof(ip_mask_map)); elem->first = 1; elem->second = 100; elem->next = NULL; //添加elem到鏈表,如果指向鏈表的指針head為空時,將新元素elem賦值給head SGLIB_LIST_CONCAT(ip_mask_map,head,elem,next); elem = (pip_mask_map)malloc(sizeof(ip_mask_map)); elem->first = 2; elem->second = 200; elem->next = NULL; SGLIB_LIST_CONCAT(ip_mask_map,head,elem,next); elem = (pip_mask_map)malloc(sizeof(ip_mask_map)); elem->first = 2; elem->second = 200; elem->next = NULL; //如果成員已經在鏈表存在的話,_member返回指向那個成員的指針,否者返回NULL SGLIB_LIST_ADD_IF_NOT_MEMBER(ip_mask_map,head,elem,ipcomp,next,_member); if (_member != NULL) free(elem); //遍歷並刪除動態分配的內存 for (elem = head; elem != NULL;) { pip_mask_map tmp = elem; printf("%d,%d\n",elem->first,elem->second); elem = elem->next; free(tmp); } return 0; }