C 指向链接列表的指针数组

C 指向链接列表的指针数组,c,C,我正在用C语言完成我的作业,通过哈希表和链式方法将500个字符串存储到5个字符的字符串中,以修复冲突 哈希算法:将ASCII值相加,并对结果应用模运算符 哈希表存储生成的哈希键和指向链接列表的指针。如果有多个5字符字符串提供相同的哈希键,则每个链表都有多个元素 到目前为止,这是我的代码。我编译了它(代码块),似乎没有错误。然而,程序崩溃了 请提供一些关于我哪里做错了的信息 #include <stdio.h> #include <string.h> #define SL

我正在用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
    }

    }
}
#包括
#包括
#定义SLEN 500
#定义WLEN 5
#定义MPRIME 73
结构节点{
char s[WLEN+1];//用于保存5个字母单词的数组
int sindex;//单词的起始索引
struct Node*next;//指向列表中下一个单词的指针
};
int searchword(char*);
int hashfunc(char*);
void build_hashtbl();
结构节点*哈希表[MPRIME]={NULL};
字符字符串[SLEN+1]="这名妇女在各个层面上都很熟悉,但很明显,她发现自己不得不将自己的女儿关在一间小房子里,这间房子在我回家的路上就已经被我们家的房子停在了咖啡馆里,在孩子们穿紫色衬衫的街道上有一个十字交叉口,我还想把手指头伸进围栏里当割草机在草丛上横扫、布满皱纹、抽着雪茄的时候,割草机就会被剪掉,而这意味着我不知道自己在干什么”;
内部主(空){
整数指数;
字符查询[WLEN+1];
build_hashtbl();//准备哈希表
printf(“输入一个5个字母的单词进行搜索:”);
scanf(“%s”,查询);
索引=搜索词(查询);
如果(索引!=-1)
printf(“单词%s从索引%d开始。\n”,查询,索引);
其他的
printf(“找不到单词%s。\n”,查询);
返回0;
}
int searchword(字符*word){
int hashval;
结构节点*lhead;
hashval=hashfunc(字);
lhead=哈希表[hashval];
while(lhead){
if(strcmp(lhead->s,word)==0)
返回lhead->sindex;
lhead=lhead->next;
}
返回-1;
}
int hashfunc(char*){
int hashval=0;
int i=0;
对于(i=0;inext=NULL;//下一个指针设置为NULL
head->next=new_node;//最后将head节点设置为指向此新节点
last=new_node;//将新节点设置为最后一个节点
}
else{//如果数组中已经有节点
结构节点*新节点;
新节点=(结构节点*)malloc(sizeof(结构节点));
strcpy(new_node->s,sElement);//将新的5个字母的单词字符串放入节点中
new_node->sindex=i;//放置该单词的起始索引
new_node->next=NULL;//下一个指针设置为NULL
head->next=new_node;//最后将head节点设置为指向此新节点
last->next=new_node;//将最后一个节点设置为指向新创建的节点
last=new_node;//将新节点设置为最后一个节点
}
}
}

您使用的last和head未初始化,因此head->next和friends会自动切换。事实上,您根本不需要它们,也不需要if分支-只需在hashtable[key]旁边设置new\u node->new\u node之后,用new\u 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;

    }
}
void build\u hashtbl(){
int i=0;
int k=0;
int键=0;
字符选择[WLEN+1]={0};
for(i=0;i next=hashtable[key];
新建_节点->sindex=i;
hashtable[key]=新的_节点;
}
}
对我有用


编辑:还需要
#包括
(至少在这里)

你上次使用的头和未初始化的头,这样头->下一步和朋友会自动切换。事实上你根本不需要它们,也不需要你的if分支-在设置新的\u节点->哈希表[key]旁边后,用新的\u节点替换哈希表[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;

    }
}
void build\u hashtbl(){
int i=0;
int k=0;
int键=0;
字符选择[WLEN+1]={0};
for(i=0;i next=hashtable[key];
新建_节点->sindex=i;
hashtable[key]=新的_节点;
}
}
对我有用


编辑:还需要
#包括
(至少在这里)

它是在哪里崩溃的?用GDB或PrimtFS来确定哪条线会导致崩溃,你会尝试在调试器中运行它吗?这会告诉你哪一行导致崩溃。谁把这样的事情作为C++赋值?有整齐的容器类来做各种各样的位。当然,你可以练习实现一个哈希容器BU。至少不要使用std::list(或std::slist)。从使用的角度来看,我怀疑这个问题被错误地标记为C++:它看起来更像C.
struct Node*hashtable[MPRIME]C++已经被称为阴影变量。在输入搜索词之前或之后,它是否崩溃?它在哪里崩溃?使用GDB或PrimtFS来确定哪一行导致在调试程序中运行这个崩溃?这会告诉你哪条线导致崩溃。Igment?有一些整洁的容器类可以实现各种不同的位。当然,你可以练习实现,例如哈希容器,但至少使用std::list(或std::slist)。从使用的角度来看,我怀疑这个问题被错误地标记为C++:它看起来更像C.
struct Node*hashtable[MPRIME];//已声明。为方便起见,将
放在此处称为阴影变量。它在输入搜索词之前还是之后崩溃