2015-02-06 41 views
0

我决定我想用C语言编写一个程序,该程序需要用户输入并使用散列表进行操作......以及我遇到了一个障碍,并且完全失去了发生了什么事情。当我运行我的代码时,一切都很好,直到我给出一个输入,然后出现分段错误。有没有人可以指出我的错误?当用户给出一个输入时,程序将决定将单词放在哪里,以ascii中的字母总数为准。一旦我解决了这个问题,我会加入它,这样如果发生碰撞,它会像7一样发怒,直到它找到一个地方。散列函数中的分段错误(C)

#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; 
    } 
+4

使用'gdb'来调试错误。 – Rohan 2015-02-06 10:19:47

+0

@rohan我已经这样做了,它在while(名字[i]!='\ 0')中是错误的。 是否有另一种方法来检查该字母是否不是行尾? – Gvalder 2015-02-06 10:23:47

回答

1

这里

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

要调用calloc但你永远不初始化n;初始化变量key也不是。

+0

n初始化字面上3线以上 – Gvalder 2015-02-06 10:38:24

+0

@Gvalder:我在说主内部初始化?它不是,它声明,你需要给它赋值 – 2015-02-06 10:38:59

+0

修正了这个n是暂时的东西,从来没有摆脱它。它应该是elecount – Gvalder 2015-02-06 10:42:42

0

您似乎没有正确初始化i

另外,您正在使用#而不是%中的sscanf()格式字符串。我不明白else if附近的逻辑与sccanf()调用相同,我不认为这是正确的。

您似乎使用sscanf()而不是strcmp()来比较字符串?

+0

#d是因为我实际上在字符串中寻找“#d”而不是值。 – Gvalder 2015-02-06 10:39:50

+0

@Gvalder是的,我意识到,这是关于'strcmp()'的部分所指的。你真的不应该这样使用'sscanf()',这是超级混乱。 – unwind 2015-02-06 10:41:24