2014-02-25 59 views
0

我希望能够使用我的getNextWord函数返回指向文件中下一个单词的指针。我想我在插入时遇到了seg错误,但我无法弄清楚。任何帮助都是非常好的。此外,我应该找到一个更好的方法来获取我的hash_table_size,而不是增加文件中的单词总数然后重新计数。我如何使尺寸自动增长?插入散列表时出现分段错误

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

int hash_table_size; 

char* getNextWord(FILE* fd) { 
    char c; 
    char buffer[256]; 
    int putChar = 0; 
    while((c = fgetc(fd)) != EOF) { 
     if(isalnum(c)) break; 
    } 
    if(c == EOF) return NULL; 

    buffer[putChar++] = c; 

    while((c = fgetc(fd)) != EOF) { 
     if(isspace(c) || putChar >= 256 -1) break; 

     if(isalnum(c)) 
      buffer[putChar++] = c; 
    } 

    buffer[putChar] = '\0'; 
    return strdup(buffer); 
} 

struct node { 
    struct node *next; 
    int count; 
    char* key; 
}; 

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

struct list *hashTable = NULL; 

/* 
* djb2 hash function 
*/ 
unsigned int hash(unsigned char *str) { 
    unsigned int hash = 5381; 
    int c; 

    while(c == *str++) 
     hash = ((hash << 5) + hash) + c; 

    return (hash % hash_table_size); 
} 

struct node* createNode(char *key) { 

    struct node *new_node; 
    new_node = (struct node *)malloc(sizeof(struct node)); 
    strcpy(new_node->key, key); 
    new_node->next = NULL; 
    return new_node; 
} 

void hashInsert(char *str) { 
    int hash_dex = hash(str); 
    struct node *new_node = createNode(str); 

    if(!hashTable[hash_dex].head) { 
     hashTable[hash_dex].head = new_node; 
     hashTable[hash_dex].count = 1; 
     return; 
    } 

    new_node->next = (hashTable[hash_dex].head); 

    hashTable[hash_dex].head = new_node; 
    hashTable[hash_dex].count++; 
    return; 
} 

void display() { 
    struct node *current; 
    int i; 
    while(i < hash_table_size) { 
     if(hashTable[i].count == 0) 
      continue; 
     current = hashTable[i].head; 
     if(!current) 
      continue; 
     while(current != NULL) { 
      char tmp[256]; 
      strcpy(tmp, current->key); 
      printf("%s", tmp); 
      current = current->next; 
     } 
    } 
    return; 
} 

int main(int argc, char *argv[]) { 

    if (argc != 2) { 
     printf("Usage: ./hashFile textfile\n"); 
    } 
    else { 
     FILE *file = fopen(argv[1], "r"); 

     if(file == 0) { 
      printf("Could not open file\n"); 
     } 
     else { 

      char *new_word; 
      while((new_word = getNextWord(file)) != NULL) { 
       hash_table_size++; 
      } 
      rewind(file); 
      hashTable = (struct list *)calloc(hash_table_size, sizeof(struct list)); 
      while((new_word = getNextWord(file)) != NULL) { 
       hashInsert(new_word); 
      } 
      display(); 
      fclose(file); 
      } 
     } 
     return 0; 
} 

回答

0
int c; 

    while(c == *str++) 
     hash = ((hash << 5) + hash) + c; 

c这里没有初始化。正如idisplay的功能。请启用所有编译器警告并修复它们。

另外:

char c; 

char buffer[256]; 
int putChar = 0; 
while((c = fgetc(fd)) != EOF) { 
    if(isalnum(c)) break; 
} 

c必须是不intchar类型。

0

变化

while(c == *str++) 

while(c = *str++)