有没有办法避免两次使用此功能?

发布于 2025-02-05 07:42:23 字数 1812 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2025-02-12 07:42:23

我认为这是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 the linearRehash function that you want to avoid doubling up on, right?

First of all, I think linearRehash could be modified into something like linearHash, which would do the initial hashing and the rehashing. Then all the duplicate checking could be done inside linearHash.

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 a NULL pointer (if phoneNum is a pointer) or an empty string (if phoneNum is a fixed length char array). Then you can check if you're looking at an empty slot without doing a strcmp (either phoneNum == NULL or phoneNum[0] == '\0'). Then just have linearHash return the index of the slot found (either a duplicate or an empty slot, or -1 if memory full), and then have inputData 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文