HASH相关例子
来源:互联网 发布:股票历史数据查询软件 编辑:程序博客网 时间:2024/05/29 15:22
#include <stdio.h>
#include <stdlib.h>
struct mac_ip{
char Mac[18];
char Ip[16];
struct mac_ip *next;
};
typedef struct mac_ip Mac_IP;
int hahash(const char mac[],int n)
{
int len=strlen(mac);
int i,sum=0;
for(i=0;i<len;i++)
sum+=mac[i];
return(sum%n);
}
void insert_new(Mac_IP *tank[],char mac[],char ip[])
{
Mac_IP *temp;
int i,n;
temp=(Mac_IP*)malloc(sizeof(Mac_IP));
if(temp == NULL){
printf("Malloc error!\n");
exit(1);
}
strcpy(temp->Mac,mac);
strcpy(temp->Ip,ip);
temp->next=NULL;
n=hahash(mac,131);
if(tank[n] == NULL){
tank[n]=temp;
}
else{
temp->next=tank[n];
tank[n]=temp;
}
}
char *hash_search(Mac_IP *tank[],char mac[])
{
int n;
Mac_IP *p;
n=hahash(mac,131);
if(tank[n] == NULL){
printf("No record of this MAC!\n");
return NULL;
}
else{
p=tank[n];
while(p != NULL){
if(strcmp(p->Mac,mac) == 0)
return(p->Ip);
else
p=p->next;
}
printf("No record of this MAC!\n");
return NULL;
}
}
int main(int argc,char **argv)
{
Mac_IP *tank[131];
int i;
char *ip=NULL;
for(i=0;i<131;i++)
tank[i]=NULL;
insert_new(tank,"10-2F-34-25-6D-DE","192.168.10.25");
insert_new(tank,"10-2F-34-6D-DE-25","192.168.1.126");
insert_new(tank,"4D-25-E4-2C-62-DE","10.0.0.234");
ip=hash_search(tank,"10-2F-34-25-6D-DE");
if(ip != NULL){
printf("ip:%s\n",ip);
}
return 0;
}
- HASH相关例子
- Hash相关
- HASH相关
- Murmur Hash 例子
- hash table相关
- hash table相关2
- hash table相关2
- Hash表相关
- Hash Table 哈希表 C++ 例子
- Hash Table 哈希表 C++ 例子
- Hash Table 哈希表 C++ 例子
- 堆-hash-AVL相关概念
- hash-map 相关参考博客
- hash表的相关知识
- 聚集索引相关例子
- DWR相关例子
- HttpClient的相关例子
- OGNL表达式相关例子
- org.apache.axis2.databinding.ADBException: Unexpected subelement arg0的一种解决办法
- jstl中foreach序号
- How to install and manage different versions of Python in Linux
- ganglia的疑难杂症
- 一次FragmentActivity 横竖屏切换Fragment残留问题
- HASH相关例子
- android访问WEB配置
- C# WINFROM中threeview中imagelist图片有时候不显示透明的
- 2013.11.22并行查询 分区表
- 15个实用的Linux find命令示例(一)
- 使用DecimalFormat实现四舍五入
- CentOS使用ntfs-3g挂载NTFS
- 15个极好的Linux find命令示例(二)
- Hibernate方言配置