为什么我不能通过隐藏的哈希表测试用例?

问题描述

C 中的合并哈希

#include <stdio.h>
#include <stdlib.h>

#define TABLESIZE 37
#define PRIME     13

enum Marker {EMPTY,USED};

typedef struct _slot{
   int key;
   enum Marker indicator;
   int next;
} HashSlot;
int HashInsert(int key,HashSlot hashTable[]);
int HashFind(int key,HashSlot hashTable[]);
int hash(int key)
{
   return (key % TABLESIZE);
}
int main()
{
    int opt;
    int i;
    int key;
    int index;
    HashSlot hashTable[TABLESIZE];

    for(i=0;i<TABLESIZE;i++){
        hashTable[i].next = -1;
        hashTable[i].key = 0;
        hashTable[i].indicator = EMPTY;
    }

    printf("============= Hash Table ============\n");
    printf("|1. Insert a key to the hash table  |\n");
    printf("|2. Search a key in the hash table  |\n");
    printf("|3. Print the hash table            |\n");
    printf("|4. Quit                            |\n");
    printf("=====================================\n");

    printf("Enter selection: ");
    scanf("%d",&opt);
    while(opt>=1 && opt <=3){
        switch(opt){
        case 1:
            printf("Enter a key to be inserted:\n");
            scanf("%d",&key);
            index = HashInsert(key,hashTable);
            if(index <0)
                printf("Duplicate key\n");
            else if(index < TABLESIZE)
                printf("Insert %d at index %d\n",key,index);
            else
                printf("Table is full.\n");
            break;
        case 2:
            printf("Enter a key for searching in the HashTable:\n");
            scanf("%d",&key);
            index = HashFind(key,hashTable);

            if(index!=-1)
                printf("%d is found at index %d.\n",index);
            else
                printf("%d is not found.\n",key);
            break;

        case 3:
            printf("index:\t key \t next\n");
            for(i=0;i<TABLESIZE;i++) printf("%d\t%d\t%d\n",i,hashTable[i].key,hashTable[i].next);
            break;
        }
        printf("Enter selection: ");
        scanf("%d",&opt);
    }
    return 0;
}
int HashInsert(int key,HashSlot hashTable[])
{
    int index=hash(key),inIndex=0,count=0;

    if(hashTable[index].indicator == EMPTY) // first slot empty
    {
        hashTable[index].key = key;
        hashTable[index].next = -1;
        hashTable[index].indicator = USED;   
        return index;       
    }
    else // slot used
    {
        while(1)
        {   
            while(hashTable[count].indicator == USED) // all slots used
            {
                count++;
                if(count == TABLESIZE)
                {
                    return count; // return table is full
                }
            }

            if(index<TABLESIZE) // index slot < TABLESIZE
            {
                for(index=hash(key);index<TABLESIZE;index++) // iterate thru 
                {
                    if(hashTable[index].key == key)
                    {
                        return -1;
                    }
                    
                    if (hashTable[index].next == -1 && hashTable[index].indicator == USED && 
                    hash(key)==hash(hashTable[index].key) )//save the last point where it has the same mod value
                    {
                        inIndex = index;
                    }

                    else if(hashTable[index].indicator == EMPTY)
                    {
                        hashTable[inIndex].next = index; // lastpoint.next = currentindex
                        hashTable[index].key = key;
                        hashTable[index].next = -1;
                        hashTable[index].indicator = USED; 
                        return index; // return insertion value
                    }  
                }
            }
            else
            {
                for(index=hash(index);index<TABLESIZE;index++) // if index > TABLESIZE
                {
                    if(hashTable[index].key == key)
                    {
                        return -1;
                    }

                    if (hashTable[index].next == -1 && hashTable[index].indicator == USED && 
                    hash(key)==hash(hashTable[index].key) ) //save the last point where it has the same mod value
                    {
                        inIndex = index;
                    }
                    else if(hashTable[index].indicator == EMPTY)
                    {
                        hashTable[inIndex].next = index; // latestmodvalue.next = currentindex
                        hashTable[index].key = key;
                        hashTable[index].next = -1;
                        hashTable[index].indicator = USED; 
                        return index; // return insertion value
                    }  
                }
            }
        }

    }

    if(HashFind(key,hashTable) == index) // if key cant be found
        return -1;
}
int HashFind(int key,HashSlot hashTable[])
{
    int index;
    index = hash(key);

    if(hashTable[index].key == 0) return -1; // if empty slot,return -1
    else if(hashTable[index].key == key) // if found key,return index
    {
        return index;
    }
    else
    {
        while(hashTable[index].key!=key && index!=-1) // find the link,then find the key
        {
            index = hashTable[index].next;
        }
        return index;
    }
}

这个想法是通过封闭寻址和线性探测的组合来实现一个哈希表。我尝试了多个我想出的测试用例,例如处理否定或访问不在 HashTable 中的项目,似乎没有任何东西通过隐藏的测试用例。虽然通过了示例测试用例。有什么我忽略的东西吗?

给定的测试用例:

插入键:1 2 5 41 42 4 10(搜索然后插入)

搜索键:4 10(display DNE)

我的代码输出

============= Hash Table ============
|1. Insert a key to the hash table  |
|2. Search a key in the hash table  |
|3. Print the hash table            |
|4. Quit                            |
=====================================
Enter selection: 1 Enter a key to be inserted:
Insert 1 at index 1
Enter selection: 1 Enter a key to be inserted:
Insert 2 at index 2
Enter selection: 1 Enter a key to be inserted:
Insert 5 at index 5
Enter selection: 1 Enter a key to be inserted:
Insert 41 at index 4
Enter selection: 1 Enter a key to be inserted:
Insert 42 at index 6
Enter selection: 3
index:   key:    next:
0   0   -1
1   1   -1
2   2   -1
3   0   -1
4   41  -1
5   5   6
6   42  -1
7   0   -1
8   0   -1
9   0   -1
10  0   -1
11  0   -1
12  0   -1
13  0   -1
14  0   -1
15  0   -1
16  0   -1
17  0   -1
18  0   -1
19  0   -1
20  0   -1
21  0   -1
22  0   -1
23  0   -1
24  0   -1
25  0   -1
26  0   -1
27  0   -1
28  0   -1
29  0   -1
30  0   -1
31  0   -1
32  0   -1
33  0   -1
34  0   -1
35  0   -1
36  0   -1
Enter selection: 1 Enter a key to be inserted:
Insert 4 at index 7
Enter selection: 2 Enter a key for searching in the HashTable:
4 is found at index 7.
Enter selection: 2 Enter a key for searching in the HashTable:
10 is not found.
Enter selection: 1 Enter a key to be inserted:
Insert 10 at index 10
Enter selection: 3
index:   key:   next:
0   0   -1
1   1   -1
2   2   -1
3   0   -1
4   41  7
5   5   6
6   42  -1
7   4   -1
8   0   -1
9   0   -1
10  10  -1
11  0   -1
12  0   -1
13  0   -1
14  0   -1
15  0   -1
16  0   -1
17  0   -1
18  0   -1
19  0   -1
20  0   -1
21  0   -1
22  0   -1
23  0   -1
24  0   -1
25  0   -1
26  0   -1
27  0   -1
28  0   -1
29  0   -1
30  0   -1
31  0   -1
32  0   -1
33  0   -1
34  0   -1
35  0   -1
36  0   -1 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)