📄 6.3.c
字号:
#include<stdio.h>
#include<string.h>
#define NULL 0
#define MAXSIZE 50
#define DUPLICATE -1
#define SUCCESS 1
#define UNSUCCESS 0
struct Node
{char name[20];
char phone[15];
struct Node *next;
};
struct HashTable
{struct Node *data;
int count;
int sizeindex;
};
struct HashTable H;
int c;
int p;
int Hash(char name[20])
{int i=0;
while(name[i]!='\0')
i++;
return (name[0]+name[--i])%MAXSIZE;
}
void collision()
{++c;
p+=1;
}
int SearchHash(char name[20])
{p=Hash(name);
while(!strcmp(H.data[p].name,"NULLKEY")&&!strcmp(name,H.data[p].name))
collision();
if(!strcmp(name,H.data[p].name))
{printf("%s %s",H.data[p].name,H.data[p].phone);
return SUCCESS;
}
else
return UNSUCCESS;
}
void RecreatHashTable()
{printf("you must design another hashtable.\n");
}
int InsertHash(struct Node data)
{c=0;
if(SearchHash(data.name)==SUCCESS)
return DUPLICATE;
else
if(c<MAXSIZE)
{strcpy(H.data[p].name,data.name);
strcpy(H.data[p].phone,data.phone);
++H.count;
return SUCCESS;
}
else
{RecreatHashTable();
return UNSUCCESS;
}
}
void CreatHash()
{int i;
struct Node data;
H.data=(struct Node*)malloc(MAXSIZE*sizeof(struct Node));
H.count=0;
H.sizeindex=MAXSIZE;
for(i=0;i<MAXSIZE;i++)
strcpy(H.data[i].name,"NULLKEY");
printf("input data when input NULL to break!\n");
printf("only input name and phone!\n");
do
{scanf("%s",data.name);
scanf("%s",data.phone);
if(InsertHash(data)==DUPLICATE)
printf("\nhave exited that person!\n");
}while(strcmp(data.name,"NULL"));
}
void main(void)
{char name[20];
CreatHash();
printf("input name to search.\n");
scanf("%s",name);
if(SearchHash(name)==SUCCESS)
printf("\nsuccess!\n");
else
printf("search unsuccess!\n");
getchar();
getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -