设为首页 加入收藏

TOP

建立简单的Hash table(哈希表)by C language
2018-10-21 20:08:45 】 浏览:117
Tags:建立 简单 Hash table 哈希 language

 

 1 #define SIZE 1000    //定义Hash table的初始大小
 2 struct HashArray
 3 {
 4     int key;
 5     int count;
 6     struct HashArray* next;
 7 }Hash[SIZE];       //主函数中需要初始化
 8 void addHash(int num)     //在Hash table中添加数据
 9 {
10     int temp=abs(num%SIZE);     //添加的数据可包括负数
11     if(Hash[temp].key==0)
12     {
13         Hash[temp].key=num;
14         Hash[temp].count++;
15     }else if(Hash[temp].key==num)
16     {
17         Hash[temp].count++;     
18     }else
19     {
20         struct HashArray *p=&Hash[temp]; 
21         while(p->key!=num&&p->next!=NULL)    
22         {p=p->next;}
23         if(p->key==num)
24         {p->count++;}
25         else
26         {
27             p->next=(struct HashArray*)malloc(sizeof(struct HashArray));
28             p=p->next;
29             p->key=num;
30             p->count=1;
31             p->next=NULL;
32         }
33     }   
34 } 

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇linux c编程:make编译一 下一篇socket 开发 - 那些年用过的基础 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目