首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >哈希表-链表-分段故障

哈希表-链表-分段故障
EN

Stack Overflow用户
提问于 2012-04-11 23:55:25
回答 2查看 597关注 0票数 0

我正在尝试实现一个带有链表链接的哈希表。下面的代码是有效的-

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TABSIZ 200

struct record {
    struct record *next;
    char name[BUFSIZ];
    int data;
};

static struct record *htable[TABSIZ];

unsigned hash(char *s)
{
    unsigned h;

    for (h = 0; *s; s++)
        h = *s;
//printf("%d", h%TABSIZ);
//I know its not a good hash function but i wanted to check chaining
    return h % TABSIZ;
}

struct record *find(char *name)
{
    struct record *item;

    for (item = htable[hash(name)]; item; item = item->next)
    {
        if (strcmp(name, item->name) == 0)
            return item;
    }

    return NULL;
}

struct record *insert(char *name,int value)
{
    struct record *item;
    unsigned h;

    if ((item = find(name)) == NULL)
    {
        if ((item = malloc(sizeof (*item))) == NULL)
            return NULL;

        strcpy(item->name, name);
        item->data=value;
        h = hash(name);
        item->next = htable[h];
        htable[h] = item;
    }

    return item;
}
void printTable()
{
    int i=0;
    struct record *temp;
    for(i=0;i<=TABSIZ;i++)
    {
        temp=htable[i];
        while(temp!=NULL)
        {
            printf("\n%d - %s - %d\n", i,temp->name, temp->data);
            temp=temp->next;
            }
    }
}
int main(void)
{
    char buf[BUFSIZ];int value;
    struct record *item;
    do{
    printf("Enter the name of the student:\n");
    scanf("%s", buf);
    if(strcmp(buf,"stop")==0) break;
    printf("Enter the marks of the student:\n");
    scanf("%d", &value);
    if(insert(buf, value)==NULL)
    {
        break;
    }
}while((strcmp(buf,"stop"))!=0);

    printf("Enter a name to find: ");
    scanf("%s", buf);
    if((item=find(buf))!=NULL)
        printf("The marks of the student is %d\n", item->data);
    else printf("\n Not Found\n");
    printTable();
    return 0;
}

现在,我尝试删除全局变量,并将局部变量用于结构数组。我删除了htable的全局声明,并在main中将其声明为

代码语言:javascript
运行
复制
struct record *htable[TABSIZ];

并将函数更改为

代码语言:javascript
运行
复制
struct record *find(struct record *htable, char *name);
struct record *insert(struct record *htable, char *name,int value);

我调用这些函数的方式是

代码语言:javascript
运行
复制
find(htable, name);
insert(htable,name,value);

但是现在我的程序出现了分段错误。我传递的结构数组是对的吗?我的声明是正确的吗。任何帮助都将不胜感激。

EN

Stack Overflow用户

发布于 2012-04-12 05:22:14

在printTable()中:

代码语言:javascript
运行
复制
for(i=0;i<=TABSIZ;i++)

看起来很可疑。你可能想要:

代码语言:javascript
运行
复制
void printTable()
{
    unsigned int i;
    struct record *temp;

    for(i=0; i < TABSIZ;i++)
    {
        for (temp=htable[i]; temp!=NULL; temp=temp->next )
        {
            printf("\n%d - %s - %d\n", i,temp->name, temp->data);

        }
    }
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10109544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档