有没有办法避免两次使用此功能?
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);
}
我正在使用哈希表来创建联系列表,并防止输入相同的电话号码,我认为我需要在索引中检查数据(使用checkduplicate()
)两次(在第一个之后一次哈希(Hash),重新命名之后的第二个),有办法,所以我只需要检查一次吗?
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是
checkduplate
线程hreachash
函数您想避免加倍加倍,对吗?首先,我认为
线程概论
可以被修改为linearhash
之类的东西,该将执行初始哈希和重新进行。然后,所有重复的检查都可以在Linearhash
内完成。您仍然需要一种方法
linearhash
才能返回三种不同类型的值:“内存完整”(当前-1
),“副词找到”(当前只是返回索引)和“找到可用空间”(目前还返回索引)。当然,您可以通过某种方式执行此操作(例如,通过指针参数传递数据),但这似乎并不必不可少。相反,我建议将您的“空空间”表示从
“ foo”
更改为null
指针(如果Phonenum
是指针)或空字符串(如果Phonenum
是固定的长度char数组)。然后,您可以检查您是否在不做strcmp
的情况下查看一个空插槽(phonenum == null
或 phonenum [0] =='\ 0 ')。然后只有linearhash
返回找到的插槽的索引(重复或空插槽或一个空插槽,或-1 -1
(如果已完整)),然后具有inputData < /code>检查返回的索引,以查看是否指向一个空插槽(在这种情况下,将详细信息插入插槽)或非空插槽(在这种情况下,使用
“使用
” )。当然,您仍在对返回的值进行额外的“空插槽”检查,但这并不重要。
I think it's the
checkDuplicate
inside thelinearRehash
function that you want to avoid doubling up on, right?First of all, I think
linearRehash
could be modified into something likelinearHash
, which would do the initial hashing and the rehashing. Then all the duplicate checking could be done insidelinearHash
.You'd still need a way for
linearHash
to return three different types of values: "memory full" (currently-1
), "duplicate found" (currently just returns the index) and "available space found" (also currently just returns the index). Of course there are ways that you can do this (e.g. passing data through pointer parameters), but that seems unnecessarily complicated.Instead I would suggest changing your "empty space" representation from
"foo"
to either aNULL
pointer (ifphoneNum
is a pointer) or an empty string (ifphoneNum
is a fixed length char array). Then you can check if you're looking at an empty slot without doing astrcmp
(eitherphoneNum == NULL
orphoneNum[0] == '\0'
). Then just havelinearHash
return the index of the slot found (either a duplicate or an empty slot, or-1
if memory full), and then haveinputData
check the returned index to see if it points to an empty slot (in which case insert the details into the slot) or a non-empty slot (in which case print"Number is used"
).Of course, you're still doing that extra "empty slot" check on the returned value, but it doesn't matter so much.