我正在尝试实现链接(哈希表中的冲突解决技术)。我的程序适用于大多数测试用例。
这是我的程序(它非常大,但我知道失败的地方。所以如果你愿意,可以跳过这部分。):
#include<stdio.h>
#include<stdlib.h>
struct LinkedListNode
{
int data;
struct LinkedListNode *next;
};
struct LinkedListNode* getNewNode()
{
// I don't need to cast the malloc in c
return malloc(sizeof(struct LinkedListNode));
}
struct LinkedListNode* insertAtBeginning(struct LinkedListNode** hashTable, int index, int data)
{
struct LinkedListNode* newNode = getNewNode(); // the new node
newNode->data = data;
newNode->next = NULL; // for now I can put the next to be null
// check if the block is containing null or not
if (hashTable[index] == NULL)
{
// now just insert the new element at beginning
hashTable[index] = newNode;
return hashTable[index]; // returning the new address of the block
}
// if collisions occur
struct LinkedListNode* blockListAddress = hashTable[index]; // the address pointing to first node of linked list
newNode->next = blockListAddress; // storing the address of block in the next of the new linkedlist
hashTable[index] = newNode; // changing the block address to the address of new node (as we have to insert in beginning)
return hashTable[index];
}
struct LinkedListNode* searchMe(struct LinkedListNode** hashTable, int index, int key)
{
struct LinkedListNode* res = NULL;
struct LinkedListNode* temp = hashTable[index];
if (temp == NULL)
return NULL;
// if we just have one element in the block then the while loop below won't get executed
// because here condition is temp->next which will be null, thus here I have written if condition
if (hashTable[index] != NULL && hashTable[index]->data == key)
{
return hashTable[index];
}
// if not null then traverse through linked list
while (temp != NULL)
{
printf("\nTEMP = %d", temp);
if (key == temp->data)
res = temp;
printf("\ntemp->data=%d\n", temp->data);
temp = temp->next;
}
return res;
}
int hashFunction(int num)
{
return num%10;
}
int main()
{
int n;
printf("\nEnter elements to be stored\n");
scanf("%d", &n);
// declaring the hashTable of size n (i.e. size of input elements), its gonna have pointers to LinkedListNode
struct LinkedListNode** hashTable = malloc(n*sizeof(struct LinkedListNode*)); // I have given memory to the table, now I even need to give memory to the elements in the table
int i;
for (i = 0; i < n; ++i)
{
hashTable[i] = NULL;
}
int d;
printf("\nEnter the elements in array\n");
for (i = 0; i < n; ++i)
{
scanf("%d", &d);
int hashedValue = hashFunction(d);
hashTable[hashedValue] = insertAtBeginning(hashTable, hashedValue, d);
}
int key;
printf("\nEnter the element you want to search for\n");
scanf("%d", &key);
int ind = hashFunction(key);
struct LinkedListNode* res = searchMe(hashTable, ind, key);
if (res == NULL)
{
printf("\nNot found\n");
}
else
{
printf("\n%d is found\n", res->data);
}
}
// time complexity in worst case for searching = O(n),
// average case t.c = O(1+alpha), where alpha = n/m
// n <- number of elements in hashtable
// m <- size of hashtable
// so alpha is 1 in this case
// thus average t.c = theta(1)
该程序为此测试用例提供了SIGSEGV
:
输入要存储的元素
5
输入数组中的元素
21 32 565 784 445
输入您要搜索的元素
565
TEMP = 35383520
TEMP-&GT;数据= 445
TEMP = 35383456
TEMP-&GT;数据= 565
分段错误(核心转储)
经过调试后,我发现它在第56行引发了段错误,即:
if (key == temp->data)
这一行写在以下代码片段中:
while (temp != NULL)
{
printf("\nTEMP = %d", temp);
if (key == temp->data)
res = temp;
printf("\ntemp->data=%d\n", temp->data);
temp = temp->next;
}
正如您在上面的失败测试中所看到的那样,while循环正在执行3次(它应该只执行两次,因为temp应该第三次指向null)。当它第三次执行时,它会在行if (key == temp->data)
处抛出段错误。
这意味着temp不是NULL
,它甚至没有data
字段。因此插入时可能存在问题(即插入newNode的下一个字段时可能不会NULL
,但我正在妥善处理那个事情。)如果插入时会出现问题那么我的代码也应该失败的其他测试用例。但是代码只是失败了上面的指定测试用例。
例如,代码传递了这个测试用例:
[aupadhyay @ localhost c] $ ./a.out
输入要存储的元素
7
输入数组中的元素
21 32 565 784 445 655 84
输入您要搜索的元素
565
TEMP = 8063248
TEMP-&GT;数据= 655
TEMP = 8063216
TEMP-&GT;数据= 445
TEMP = 8063152
TEMP-&GT;数据= 565
发现565
我只是无法弄清楚,为什么它没有通过上述测试用例。
答案 0 :(得分:1)
您的代码在许多地方存在的问题是,在索引到hashTable
时,您无法确保保留在最初分配的范围内。
在您的示例中,数字565将生成hashedValue
为5.您只在hashTable
中分配了5个元素,但这些元素覆盖了0到4的范围。插入hashTable[5]
进入未定义行为领域,代码崩溃。当你输入7个数字时,你不会遇到同样的问题,因为所有7个数字都会产生{0}到0到6范围内的hashedValue
。
由于您的hashFunction
只返回0到9之间的数字,您可以声明一个跟踪hashTable
大小的新变量,并在输入数字时将其展开,或者预先为其分配最大尺寸。
struct LinkedListNode** hashTable = malloc(10*sizeof(struct LinkedListNode*)); // I
int i;
for (i = 0; i < 10; ++i)
{
hashTable[i] = NULL;
}
从长远来看,另一种选择会更有意义,因为如果你改变hashFunction
它会适应。
答案 1 :(得分:1)
您遇到的问题是:您没有正确初始化哈希表。你写道:
int i;
for (i = 0; i < n; ++i)
{
hashTable[i] = NULL;
}
由于您的散列表有10个桶,因此它应该是i&lt; 10。
当n == 5时,hashTable [5]没有被初始化并且有垃圾。当545被哈希处理时,它会得到那个垃圾,因为它是下一个指针,而不是NULL。