void inputData(){
printf("Enter contact name : "); gets(temp.name);
fflush(stdin);
printf("Enter contact email : "); gets(temp.email);
fflush(stdin);
printf("Enter contact phone number : "); gets(temp.phoneNum);
fflush(stdin);
int index = hash(temp.phoneNum, sizeof(table1.listContact)/sizeof(table1.listContact[0]));
if (checkDuplicate(index)){
puts("Number is used");
return;
}
if(strcmp(table1.listContact[index].phoneNum, "foo")){
index = linearRehash(index, sizeof(table1.listContact)/sizeof(table1.listContact[0]));
if (index == -1){
puts("Memory Full);
return;
}
if (checkDuplicate(index)){
puts("Number is used");
return;
}
}
strcpy(table1.listContact[index].name, temp.name);
strcpy(table1.listContact[index].email, temp.email);
strcpy(table1.listContact[index].phoneNum, temp.phoneNum);
}
I'm using hash tables to create a contact list, and to prevent entering the same phone number i think i need to check the data (using checkDuplicate()
) in the index twice (once after the first hash, second after the rehashing), is there a way so i only need to check it once?
int checkDuplicate(int index){
if (!strcmp(table1.listContact[index].phoneNum, temp.phoneNum)){
return 1;
}
return 0;
}
int linearRehash(int hash, int m){
int i = 0;
while (strcmp(table1.listContact[hash].phoneNum, "foo")){
hash = (hash+1)%m;
if (checkDuplicate(hash)){
return hash;
}//If duplicate found, return index with the same data
if (i == m){
return -1;
}//If no space in hashtable return -1
i++;
}
return hash;
}