我在C语言中完成我的任务,通过哈希表和链式方法将500个字符串存储到5个字符的字符串中,以修复冲突。
散列算法:将ASCII值相加,并对结果应用模运算符。
哈希表存储生成的哈希键和指向链接列表的指针。如果有多个5个字符的字符串提供相同的哈希键,则每个链表都有多个元素。
到目前为止这是我的代码。我编译了它(代码块),看起来没有错误。但是程序崩溃了。
请提供一些我做错了什么地方的信息。
#include <stdio.h>
#include <string.h>
#define SLEN 500
#define WLEN 5
#define MPRIME 73
struct Node {
char s[WLEN+1]; // array to hold the 5-letter word
int sindex; // starting index of the word
struct Node * next; // a pointer to the next word in the list
};
int searchword(char *);
int hashfunc(char *);
void build_hashtbl();
struct Node * hashtable[MPRIME] = {NULL};
char string[SLEN+1] = "thenamewasfamiliartomeonseverallevelslookingbackitwasfatethatifoundhimihadcometopeppervillebeachtocloseonasmallhousethathadbeeninourfamilyforyearsonmywaybacktotheairportistoppedforcoffeetherewasafieldacrossthestreetwherekidsinpurpletshirtswerepitchingandhittingihadtimeiwanderedoverasistoodatthebackstopmyfingercurledinthechainlinkfenceanoldmanmaneuveredalawnmoweroverthegrasshewastannedandwrinkledwithahalfcigarinhismouthheshutthemowerwhenhesawmeandaskedifihadakidoutthereisaidnoheaskedwhatiwasdoing";
int main(void) {
int index;
char query[WLEN+1];
build_hashtbl(); // prepare the hash table
printf("Enter a 5-letter word to search: ");
scanf("%s", query);
index = searchword(query);
if (index != -1)
printf("The word %s starts at index %d.\n", query, index);
else
printf("The word %s is not found.\n", query);
return 0;
}
int searchword(char * word) {
int hashval;
struct Node * lhead;
hashval = hashfunc(word);
lhead = hashtable[hashval];
while (lhead) {
if (strcmp(lhead->s,word) == 0)
return lhead->sindex;
lhead = lhead->next;
}
return -1;
}
int hashfunc(char *){
int hashval = 0;
int i = 0;
for (i = 0; i < WLEN; i++){
hashval += (int) string[i];
}
return (int) (hashval % MPRIME);
}
void build_hashtbl(){
struct Node *hashtable[MPRIME]; //already declared. put here for ease
struct Node * head = NULL;
struct Node * last = NULL;
int i = 0;
int k = 0;
int key = 0;
char sElement[WLEN+1] = {0};
for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
key = hashfunc(*string[i]);
for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
sElement[k] = string[i+k];
}
if (hashtable[key] != (NULL)){ //if the hashtable element at that index is empty, STORE it in a node
hashtable[key] = head;
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->sindex = i; //put the starting index of this word
new_node->next = NULL; //the next pointer is set to NULL
head->next = new_node; //finally set the head node to point to this new node
last = new_node; //set the new node as the last node
}
else { //if there is already a node in the array
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->sindex = i; //put the starting index of this word
new_node->next = NULL; //the next pointer is set to NULL
head->next = new_node; //finally set the head node to point to this new node
last->next = new_node; //set the last node to point to thew new created node
last = new_node; //set the new node as the last node
}
}
}
最佳答案
你使用的最后一个和头未初始化,所以头->下一个和朋友将segfault。事实上,您根本不需要它们,也不需要if分支-只要在hashtable[key]旁边设置new_node->后用new_node替换hashtable[key]
void build_hashtbl(){
int i = 0;
int k = 0;
int key = 0;
char sElement[WLEN+1] = {0};
for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
key = hashfunc(string+i);
for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
sElement[k] = string[i+k];
}
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->next=hashtable[key];
new_node->sindex=i;
hashtable[key]=new_node;
}
}
对我有用。
编辑:还需要
#include <stdlib.h>
(至少在这里)