在散列函数分段错误(C) [英] Segmentation fault in hash function (C)

查看:118
本文介绍了在散列函数分段错误(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定,我想让在C程序,将采取用户输入和做事利用哈希表......好吧我遇到了阻碍,我完全失去了作为是怎么回事。当我运行我的code一切都很好,直到我给一个输入然后我得到一个分段错误。有没有人在那里,可以指出我的错误?当用户给出的输入程序将决定在何处以总以ASCII字母把字(S)。一旦我得到这个问题修复我会添加它,这样,如果有冲突,直到找到一个地方去,将通过类似7 incriment。

 的#include<&stdio.h中GT;
  #包括LT&;&string.h中GT;
  #包括LT&;&stdlib.h中GT;  结构散列* hashTable中= NULL;
  INT eleCount = 0;  结构节点{
        INT关键;
        字符名称[1024];
        结构节点*接下来的;
  };  结构哈希{
        结构节点*头;
        诠释计数;
  };  结构节点* createNode(INT键,字符*姓名){
        结构节点* newnode;
        newnode =(结构节点*)malloc的(的sizeof(结构节点));
        newnode->关键=键;
        的strcpy(newnode->名,姓名);
        newnode->接着= NULL;
        返回newnode;
  }
  无效insertToHash(INT键,字符*姓名){
        INT hashIndex =键%eleCount;
        结构节点* newnode = createNode(键,名);
        如果(!hashTable中[hashIndex]。头){
                hashTable中[hashIndex]。头= newnode;
                hashTable中[hashIndex] .Count之间= 1;
                返回;
        }
        newnode->接下来=(hashTable中[hashIndex]。头);
        hashTable中[hashIndex]。头= newnode;
        hashTable中[hashIndex] .Count之间++;
        返回;
  }
  无效deleteFromHash(INT键){
        INT hashIndex =项%eleCount,标志= 0;
        结构节点*温度,* MYNODE;
        MYNODE = hashTable中[hashIndex]。头;
        如果(!MYNODE){
                的printf(字不是在哈希表!\\ n);
                返回;
        }
        TEMP = MYNODE;
        而(MYNODE!= NULL){
                如果(myNode->键==键){
                        标志= 1;
                        如果(MYNODE == hashTable中[hashIndex]。头)
                                hashTable中[hashIndex]。头= myNode->接下来,
                        其他
                                TEMP->接下来= myNode->接下来,                        hashTable中[hashIndex] .count--;
                        免费(MYNODE);
                        打破;
                }
                TEMP = MYNODE;
                MYNODE = myNode->接下来,
        }
        如果(标志)
                的printf(字从哈希表中删除灰色骷髅\\ n次方);
        其他
                的printf(字是不是在哈希表present \\ n!);
        返回;
  }  无效searchInHash(INT键){
        INT hashIndex =项%eleCount,标志= 0;
        结构节点* MYNODE;
        MYNODE = hashTable中[hashIndex]。头;
        如果(!MYNODE){
                的printf(不在哈希表\\ n词搜索);
                返回;
        }
        而(MYNODE!= NULL){
                如果(myNode->键==键){
                        的printf(键数:%d \\ n,myNode->键);
                        的printf(名称:%s \\ n,myNode->名);
                        标志= 1;
                        打破;
                }
                MYNODE = myNode->接下来,
        }
        如果(!标志)
                的printf(不在哈希表\\ n词搜索);
        返回;
  }  无效显示(){
        结构节点* MYNODE;
        INT I;
        对于(i = 0; I< eleCount;我++){
                如果(hashTable中[I] .Count之间的== 0)
                        继续;
                MYNODE = hashTable中[I]。头;
                如果(!MYNODE)
                        继续;
                的printf(关键词\\ n);
                的printf(---------------- \\ n);
                而(MYNODE!= NULL){
                        的printf(% - 12D,myNode->键);
                        的printf(% - 15秒,myNode->名);
                        MYNODE = myNode->接下来,
                }
        }
        返回;
  }  诠释主(){
        INT N,CH,钥匙,我;
        字符名称[1024] CAS [5];
        eleCount = 23;
        hashTable中=(结构哈希*)释放calloc(N,的sizeof(结构散));
        而(1){
                的printf(\\ nword:插入Word \\ N#D:字删除字\\ n);
                的printf(#的诺言:搜索词\\ n#电话号码:显示哈希表\\ N#问:退出\\ n);
                的printf(请输入您的选择:);
                与fgets(名称,1023,标准输入);
                如果(sscanf的(名字,#D,&安培; CAS)== 1)
                    {//删除
                    我= 2;
                    而(名称[I]!='\\ 0')
                    {键=键+ I;
                    我++;}
                    deleteFromHash(键);
                    }
                否则如果(的sscanf(姓名,#的,&放大器; CAS)== 1)
                    {//搜索
                    我= 2;
                    而(名称[I]!='\\ 0')
                    {键=键+ I;
                    我++;}
                    sea​​rchInHash(键);
                    }
                否则,如果(sscanf的(名字,#P,&安培; CAS)== 1)
                    {//打印
                    显示();
                    }
                否则,如果(sscanf的(名字,#Q,&安培; CAS)== 1)
                    {//放弃
                    出口(0);
                    }
                其他
                    {//插
                    而(名称[I]!='\\ 0')
                    {键=键+ I;
                    我++;}
                    名称[strlen的(名字) - 1] ='\\ 0';
                    insertToHash(键,名);
                    }
                }        返回0;
  }


解决方案

下面

  hashTable中=(结构哈希*)释放calloc(N,的sizeof(结构散));

您在呼唤释放calloc 但你永远不初始化 N ;既不是变量初始化。

I decided that I wanted to make a program in C that would take a user input and do things with a hash table... well I have hit a snag and I am completely lost as to what is going on. When I run my code everything is fine until I give an input then I get a segmentation fault. Is there anyone out there that can point out my mistake? When the user gives an input the program will decide where to put the word(s) by the total of the letters in ascii. Once I get this issue fixed I will add it in so that if there is a collision it will incriment by something like 7 until it finds a place to go.

  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  struct hash *hashTable = NULL;
  int eleCount = 0;

  struct node {
        int key;
        char name[1024];
        struct node *next;
  };

  struct hash {
        struct node *head;
        int count;
  };

  struct node * createNode(int key, char *name) {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->key = key;
        strcpy(newnode->name, name);
        newnode->next = NULL;
        return newnode;
  }


  void insertToHash(int key, char *name) {
        int hashIndex = key % eleCount;
        struct node *newnode =  createNode(key, name);
        if (!hashTable[hashIndex].head) {
                hashTable[hashIndex].head = newnode;
                hashTable[hashIndex].count = 1;
                return;
        }
        newnode->next = (hashTable[hashIndex].head);
        hashTable[hashIndex].head = newnode;
        hashTable[hashIndex].count++;
        return;
  }
  void deleteFromHash(int key) {
        int hashIndex = key % eleCount, flag = 0;
        struct node *temp, *myNode;
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Word not in hash Table!!\n");
                return;
        }
        temp = myNode;
        while (myNode != NULL) {
                if (myNode->key == key) {
                        flag = 1;
                        if (myNode == hashTable[hashIndex].head)
                                hashTable[hashIndex].head = myNode->next;
                        else
                                temp->next = myNode->next;

                        hashTable[hashIndex].count--;
                        free(myNode);
                        break;
                }
                temp = myNode;
                myNode = myNode->next;
        }
        if (flag)
                printf("Word deleted from Hash Table by the power of Grey Skull\n");
        else
                printf("Word is not present in hash Table!\n");
        return;
  }

  void searchInHash(int key) {
        int hashIndex = key % eleCount, flag = 0;
        struct node *myNode;
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Searched word not in hash table\n");
                return;
        }
        while (myNode != NULL) {
                if (myNode->key == key) {
                        printf("Key      : %d\n", myNode->key);
                        printf("Name     : %s\n", myNode->name);
                        flag = 1;
                        break;
                }
                myNode = myNode->next;
        }
        if (!flag)
                printf("Searched word not in hash table\n");
        return;
  }

  void display() {
        struct node *myNode;
        int i;
        for (i = 0; i < eleCount; i++) {
                if (hashTable[i].count == 0)
                        continue;
                myNode = hashTable[i].head;
                if (!myNode)
                        continue;
                printf("Key         Word\n");
                printf("----------------\n");
                while (myNode != NULL) {
                        printf("%-12d", myNode->key);
                        printf("%-15s", myNode->name);
                        myNode = myNode->next;
                }
        }
        return;
  }

  int main() {
        int n, ch, key, i;
        char name[1024],cas[5];
        eleCount = 23;
        hashTable = (struct hash *)calloc(n, sizeof (struct hash));
        while (1) {
                printf("\nword: Insert word\n#d: word Delete word\n");
                printf("#s word: Search for word\n#p: Display hash table\n#Q: Exit\n");
                printf("Enter your choice:");
                fgets(name, 1023, stdin);
                if(sscanf(name,"#d",&cas)==1)
                    {//delete
                    i=2;
                    while(name[i]!='\0')
                    {key=key+i;
                    i++;}
                    deleteFromHash(key);
                    }
                else if(sscanf(name,"#s",&cas)==1)
                    {//search
                    i=2;
                    while(name[i]!='\0')
                    {key=key+i;
                    i++;}
                    searchInHash(key);
                    }
                else if(sscanf(name,"#p",&cas)==1)
                    {//print
                    display();
                    }
                else if(sscanf(name,"#Q",&cas)==1)
                    {//Quit
                    exit(0);
                    }
                else
                    {//insert
                    while(name[i]!='\0')
                    {key=key+i;
                    i++;}
                    name[strlen(name) - 1] = '\0';
                    insertToHash(key, name);
                    }
                }

        return 0;
  }

解决方案

Here

hashTable = (struct hash *)calloc(n, sizeof (struct hash));

You are calling calloc but you never initialized n; neither is the variable key initialized.

这篇关于在散列函数分段错误(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆