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