C 为什么可以';我不能通过隐藏的哈希表测试用例吗?
在C中合并哈希C 为什么可以';我不能通过隐藏的哈希表测试用例吗?,c,debugging,hashtable,C,Debugging,Hashtable,在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; 其思想是结合闭合寻址和线性探测来实现哈希表。我已经尝试了多个我提出的
#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;
其思想是结合闭合寻址和线性探测来实现哈希表。我已经尝试了多个我提出的测试用例,比如处理否定项或者访问一个不在哈希表中的项目,没有任何东西能够通过隐藏的测试用例。但通过了示例测试用例。有什么是我忽略的吗
给定测试用例:
插入键:1 2 5 41 42 4 10(搜索然后插入)
搜索键:4 10(显示DNE)
我的代码的输出
请提供更多信息、详细信息和上下文。可能会显示程序中的非隐藏测试用例及其结果,以及您自己发明的任何测试用例,以便找到您的盲点。这是一个非常重要的程序,您不应该期望用户阅读代码并理解其中的所有内容(尽管可能有一些人可以…)。你在这个话题上帮助的用户越多,你对他们的帮助越深入,你得到答案的机会就越大。此外,制作一个整体而不是一组代码片段可能会有所帮助。您还可以尝试让程序发现任何不寻常的东西,从而帮助您。为此,例如,您不应忽略scanf的返回值并插入调试输出(或使用调试器)以观察是否一切都按照您的想象进行。这可能会发现偶然有效但实际略有偏差的案例。如果隐藏的测试用例失败了,那么反过来也可能会失败。什么是隐藏的测试用例?三个基本问题是1。你是做什么的?2.会发生什么?3.你以为会发生什么?
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",key,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;
}
}
============= 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