哈希表 简单实现

来源:互联网 发布:兄弟连php强化就业 编辑:程序博客网 时间:2024/05/17 07:58
#include <stdio.h>#include <stdlib.h>typedef struct hash{int buff[100];}Hash,*pHash;Hash *createHash(){pHash p = (pHash)malloc(sizeof(Hash));if (NULL == p){return NULL;}for (int i = 0; i < 100; i++)//-1代表空{p->buff[i] = -1;}return p;}void destroyHash(Hash *p){if (NULL == p){return -1;}free(p);}int hashFunc(int key){if (key < 0)return -1;return key;//---------------------------------}int insertHash(Hash *p, int data){if (NULL == p){return -1;}//if (-1 == p->buff[data])int index = hashFunc(data);//内存  与   数据的关系  if (-1 == p->buff[index]){p->buff[index] = data;return 0;}else{/*int i = data;while (-1 != p->buff[i]&&i<100){i++;}if (i < 100){p->buff[i] = data;}return 0;*/while (-1 != p->buff[index] && index< 100){index++;}if (100 == index){return -3;}p->buff[index] = data;return 0;}}int findHash(Hash *p, int data){if (NULL == p){return -1;}if (data < 0){return -2;}int index = hashFunc(data);for (; index < 100; index++){if (p->buff[index] == data){return index;}}if (100 == index){return -1;}}void main(){}

0 0