散列表查找哈希表——数组和单向链表组成创建文件harsh.charsh.c定义链表的结构体哈希表的数组#includestdio.h #includestdlib.h #define SIZE 10 typedef int date_t; typedef struct harsh { struct harsh *next; date_t date; }node_t; node_t* harsh[SIZE];创建哈希表int harsh_create(date_t *a,int leng) { int i0; for(i0;ileng;i) { //找下标 int addr a[i] % 10; //创建链表节点 node_t *pnew malloc(sizeof(node_t)); pnew-date a[i]; pnew-next NULL; //把节点接在哈希数组后面 if(harsh[addr]NULL) { harsh[addr] pnew; }else { pnew-next harsh[addr]; harsh[addr] pnew; } } return 0; }打印void harsh_show() { int i 0; for(i0;iSIZE;i) { if(harsh[i]!NULL) { node_t *p harsh[i]; while(p) { if(p-next ! NULL) { printf(%d ,p-date); }else { printf(%d|NULL,p-date); } pp-next; } } printf(\n); } }查找node_t *harsh_find(date_t date) { int addr date%10; if(harsh[addr] NULL) { return NULL; }else { node_t *p harsh[addr]; while(p) { if(p-date date) { return p; } pp-next; } } return NULL; }更新node_t *harsh_update_key(date_t old, date_t new) { int addr old % 10; if(harsh[addr]NULL) { return NULL; }else { node_t *p harsh[addr]; while(p) { if(p-dateold) { p-date new; return p; } pp-next; } } }删除int harsh_delete_key(date_t date) { node_t *key harsh_find(date); if(keyNULL) { printf(no this date!\n); return -1; }else { node_t *p harsh[date%10]; if(p-nextNULL) { free(p); harsh[date%10]NULL; } else if(pkey) { harsh[date%10]key-next; free(key); } else { while(p (p-next-date ! date) p-next!NULL) { pp-next; } if(p-nextNULL) { return -1; } p-next key-next; free(key); } } }销毁int harsh_destrory() { int i0; for(i0;iSIZE;i) { if(harsh[i] ! NULL) { node_t * p harsh[i]; while(p) { node_t * p_tempp; pp-next; free(p_temp); harsh[i]NULL; } } } }valgrind——内存泄露问题memcheck:查内存错误越界未初始化重复free,泄露使用1.基本使用 valgrind /.a,out2.最常用—— --leak-checkfull