繁体   English   中英

指向链表的指针数组

[英]Array of pointer to the linked list

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

    }
}

您使用的last和head未初始化,因此head-> next和朋友将出现段错误。 实际上,您根本不需要它们,也不需要if分支-在将new_node-> next设置为hashtable [key]之后,只需用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> (至少在这里)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM