2012-05-05 14 views
0

私はこれをデバッグするのに非常に苦労しています。構造をプリントアウトしようとすると、私は最後の言葉しか得ません。私は記憶の上に何かを書いていますか?誰か助けてくれますか? insert_hashこれは最後の単語だけを印刷するのはなぜですか?

typedef struct hash_table_ { 
    void **order; 
    int *number_next_calls; 
    int *number_buckets; 
    int *buckets_size; 
    int *worst; 
    int *total; 
    float *average; 
    int (*hash_func)(char *); 
    data_el **buckets_array; 
} hash_table, *Phash_table; 

typedef struct data_{ 
    char *key; 
    void *data; 
    struct data_ *next; 
}data_el; 

main(){ 

while ((c = getchar()) != EOF) { 
    if ((c == ' ') || (c == ',') || (c == '.') || (c == '!') || (c == '"') || 
     (c == ':') || (c == '\n')) { 

     /* End of a word */ 
     if (char_index) { 
     /* Word is not empty */ 
     word[char_index] = '\0'; 
     lower_case_word(word); 
     if(!find_hash(dictionary,word)){ 
      insert_hash(dictionary,word,frequency[hash_function(word)]); 
     } 
     printf("%s\n", dictionary -> buckets_array[hash_function(word)] -> key); 
     printf("%d \n",hash_function(word)); 
     frequency[hash_function(word)]++; 
     char_index = 0; 
     num_words++; 
     } 
    }else{ 
     word[char_index++] = c; 
    } 
    } 

/*This is when it prints*/ 
    printf("%s\n", dictionary -> buckets_array[337] -> key); 
    printf("%s\n", dictionary -> buckets_array[532] -> key); 
    printf("%s\n", dictionary -> buckets_array[93] -> key); 

} 

int hash_function(char *word){ 

    int sum,i; 
    i = 0; 
    sum = 0; 
    while(word[i] != '\0'){ 
    sum = sum + word[i]; 
    i++; 
    } 
    return sum%1000; 
} 

void insert_hash(Phash_table table, char *key, void *data){ 
    int index; 
    data_el *p, *cur; 

    index = table -> hash_func(key); 

    /*Head insertion*/ 
    if(table -> buckets_array[index] == NULL){ 
    table -> buckets_array[index] = (data_el *)malloc(sizeof(data_el)); 
    table -> buckets_array[index] -> data = data; 
    table -> buckets_array[index] -> next = NULL; 
    table -> buckets_array[index] -> key = key; 
    }else{ 
    printf("%s",table -> buckets_array[index] -> key); 
    cur = table -> buckets_array[index]; 
    p = (data_el *)malloc(sizeof(data_el)); 
    p -> key = key; 
    p -> data = data; 
    p -> next = cur; 
    cur = p; 
    /* 
    table -> buckets_array[index] = cur; 
    */ 
    } 
} 
+1

これはコードの大部分です。問題をプログラムの特定の部分に分けてください。デバッガを使用するか、またはステートメントを出力する必要があります。また、サンプルの入力と出力(期待と実際)を用意してください私たちは問題をより良く理解しています。 – amit

+0

私がwhile関数に入っているとき、私はprintfを使ってそれが格納しているデータを見て、ちょうどスキャンされたものを印刷しています。これは正しいです。 whileループで処理した後、printf( "%s \ n"、辞書 - > buckets_array [337] - > key)を印刷しようとしました。 printf( "%s \ n"、辞書 - > buckets_array [532] - >キー); –

+1

文法上の注意:演算子の優先順位は、あなたの利点の中で働くことがあります: '' * table-> total = *(table-> total)+ 1;は '* table-> total = * table-> total + 1;'になります。 > total + = 1; '' - > 'はとても緊密に結びついています! – wildplasser

答えて

1

あなたはmainから渡されました同じメモリにバケットエントリポイントを聞かせて、ある

table -> buckets_array[index] -> key = key; 

RESP

p -> key = key; 

を持っています。コードは不完全なので、わかりませんがmainword配列を再利用し、挿入するたびに新しい配列を割り当てないでください。したがって、table->buckets_array[index]->keyが指す文字列の内容は上書きされます。

文字列を新しいメモリのチャンクにコピーし、バケットエントリがそれを指すようにする必要があります。

+0

私はあなたの権利を信じています。お手伝いありがとう。 –

+0

はい、そうです。どうもありがとうございます! –

関連する問題