2016-12-01 13 views
0

私は自分の課題の1つを終えようとしていますが、いくつか問題があります。私は単語を追加する必要があるリンクリストを作成するために構造体を使用するプログラムを作成する必要があります。単語がすでにリンクリストに入っている場合は、頻度を更新するだけです。リンクリストに単語を追加してcの単語の頻度をチェックして更新する方法

私はすでにこれを持っている:

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

typedef struct words Words; 
struct words{ 
    char *word; 
    int freq; 
    Words *next; 
}; 

/* 
Inserts a copy of newWord into the list, in lexicographical order. If newWord is already 
in the list, increment the freq member of the node. The function returns a pointer to 
the list. 
*/ 
Words *addWord(Words *headPtr, char* newWord){ 
Words *current = headPtr; 
if(headPtr == NULL) 
{ 
    current->word = newWord; 
    current->freq = 1; 
} 
else 
{ 
    while(current != NULL) 
     if(strcmp(headPtr->word, newWord)) 
     { 
      current->freq++; 
      return headPtr; 
     } 
     else 
     { 
      current->word = newWord; 
      current->freq = 1; 
     } 
} 
return headPtr; 
} 

//prints the words in the list, along with the frequency of each word 
void printWords(Words *headPtr){ 

    while(headPtr != NULL) 
    { 
     printf("%s: %d", headPtr->word, headPtr->freq); 
     headPtr = headPtr->next; 
    } 
} 

//frees the entire list. Note: Words **headPtr since the headPtr NULL upon return 
void deleteList(Words **headPtr){ 
    Words *current = *headPtr; 
    Words *next; 
    while(current != NULL) 
    { 
     next = current->next; 
     free(current); 
     current = next; 
    } 
    *headPtr = NULL; 
} 

int main(){ 
    char word[20]; 
    Words *list = NULL; 
    scanf("%s", word); 
    while(!feof(stdin)){ 
     list = addWord(list, word); 
     scanf("%s", word); 
    } 
    printWords(list); 
    deleteList(&list); 
} 
+0

確かに、それ以外の場合は混乱に見える、あなたのブロックにコンパイラに括弧を追加、コメント、あなたのコードに埋め込まれたが、参照してください。すぐに理解できますが、コンパイラだけがコードを読むことを意図していたなら、 'printWords()'関数を呼び出す理由はありません。 –

答えて

1

すると、コード内のいくつかの問題があります。ここで

Words *addWord(Words *headPtr, char* newWord){ 
    Words *current = (Words*) malloc(sizeof(Words)); // Don't malloc here. 
                 // You don't know yet 
                 // whether you need 
                 // a new node or you 
                 // you just need to 
                 // update freq 

    if(current == NULL)     // If current is NULL you have 
              // serious problems, i.e. you 
              // are out of memory. 
              // Did you really intended to do: 
              // if (headPtr == NULL) 

    { 
     current->word = newWord; 
     *current->next = (*headPtr); 
     (*headPtr) = *current;   // I'm not sure what you try here 
              // but it seems strange 
    } 
    else 
    { 
     while(current != NULL) 
      if(strcmp(headPtr->word, newWord)) // This is not the way to compare 
               // strings. Two strings compare 
               // when "strcmp" returns 0. 
               // 
               // Further you don't want to 
               // use headPtr here. 

      { 
       current->freq++; // Use of uninitialized value 
       return;   // Missing argument to return 
      } 
      else 
      { 
       current->word = newWord;  // Use of uninitialized value 
       *current->next = (*headPtr); // Use of uninitialized value 
       (*headPtr) = *current; 
      } 
    } 

    // Missing return 

} 

はで開始するいくつかのコードです:彼らは、単一の行よりも多くを持っているとき

#define WORD_SIZE 20 

struct words{ 
    char word[WORD_SIZE]; // Use a char array 
    int freq; 
    Words *next; 
}; 

Words *addWord(Words *headPtr, char* newWord) 
{ 
    Words *current = headPtr; // Make a copy of headPtr 
    Words* new; 

    if ((current == NULL) || (strcmp(current->word, newWord) > 0)) 
    { 
     // Insert in front of list 
     new = malloc(sizeof(Words)); // Allocate memory 
     if (new == NULL) 
     { 
      // oh, dear - out of memory - print an error message and exit 
      exit(1); 
     } 

     strncpy(new->word, newWord, WORD_SIZE); // Make sure not to overflow 
                // the buffer, so use strncpy 
     (new->word)[WORD_SIZE-1] = '\0'; // Make sure to zero terminate 
     new->freq = 1; 
     new->next = headPtr; 

     return new; 
    } 

    while(1) 
    { 
     int cmp = strcmp(current->word, newWord); 
     if(cmp == 0) 
     { 
      current->freq++; 
      return headPtr; 
     } 

     if(cmp < 0) 
     { 
      if ((current->next == NULL) || (strcmp(current->next->word, newWord) > 0)) 
      { 
       // Add code to insert node after current 

       return headPtr; 
      } 
     } 
     else 
     { 
      // This should never happen... 
      printf("BAD CODE 1\n"); 
      exit(1); 
     } 

     current = current->next; 
    } 
} 
+0

私はもっと正確に何をすべきですか? –

+0

@StefanCiprianIuga - あなたのために何が問題になるのですか? – 4386427

+0

プログラムは動作しなくなり、応答しないエラーが発生します。 私はそれを少し修正しましたが、私はうまくいきませんでした。 –

関連する問題