0

この関数は、ノードとそのデータをリンク リストに挿入する場所です。

void insertNodeAndWord(struct ListNode ** pointerToHead, char word[16]) {
    struct ListNode * newNode = (struct ListNode *)malloc(sizeof(struct      ListNode));
    newNode->word = word;
    newNode->next = NULL;

    //printf("%s\n", newNode->word); // Prints out the correct words when i     try to print from here.

    if(*pointerToHead == NULL) {
        newNode->next = *pointerToHead;
    }
    *pointerToHead = newNode;
}

この関数は、ボグル ボードからすべての単語を取得する場所です (ここで単語を出力すると、正しく出力されるため、この関数は正しく動作しているようです。

struct ListNode * getAllWords(char currWord[16], int x, int y, const char     board[4][4], int check[4][4], struct ListNode * list) {
    if(x<0||y<0||x>=4||y>=4) { //base case
        return list;
    } else if (check[x][y] == 0) {
        char newWord[16];
        strcpy(newWord, currWord);
        if(isPrefix(newWord) == 0) {
            return list;
        }
        int length = strlen(newWord);
        newWord[length] = board[x][y];
        newWord[length+1] = '\0';

        if(isWord(newWord) != 0) {
            insertNodeAndWord(&list, newWord);
            //printf("%s\n", list->word); // Prints out the correct words when i try to print from here.
            printf("Length: %d\n", listLength(list)); // Prints out 1 every time.
        }
        int row, col;
        for(row =-1; row<=1; row++) {
            for(col=-1; col<=1; col++) {//
                check[x][y] = 1; //marks the board tile as visited
                getAllWords(newWord, x+row, y+col, board, check, list);
                check[x][y] = 0; //unmarks the board tile as visited
            }
        }
    }
    return list;
}


struct ListNode * findWords(const char board[4][4]) {
    int x, y;
    int check[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0,    0}};
    char word[16] = "";
    struct ListNode * list;
    list = NULL;
    for(x=0; x<4; x++) {
        for(y=0; y<4; y++) {
            getAllWords(word, x, y, board, check, list);
            // printf("%s\n", list->word); // I get a "has stopped working" error here when i try to print out the words.
        }
    }
    return list;
 }
4

1 に答える 1

1

私が見る問題:

問題1

newNode->word = word;

正しくありません。リンク リスト内のすべてのノードは、 から渡された同じメモリ ブロックへのポインタを格納しますgetAllWords。さらに悪いことに、そのメモリ ブロックは関数のローカル変数に対応し、getAllWordsから戻ると無効になりますgetAllWords。ぶら下がっているメモリを指すノードになってしまいます。

次のようなものが必要です

newNode->word = strdup(word);

問題 2

insertNodeAndWord新しいノードをリストの最後に追加するか、リストの最初に追加するかは明確ではありません。

リストの先頭に追加する場合、関数は次のようになります。

void insertNodeAndWord(struct ListNode ** pointerToHead, char word[16]) {
   struct ListNode * newNode = malloc(sizeof(struct ListNode));
   newNode->word = strdup(word);
   newNode->next = *pointerToHead;
   *pointerToHead = newNode;
}

新しいノードをリストの最後に追加する場合、ロジックはもう少し複雑になります。

問題 3

呼び出された場所の戻り値を使用していませんgetAllWords

行を変更する ( getAllWords)

        getAllWords(newWord, x+row, y+col, board, check, list);

        list = getAllWords(newWord, x+row, y+col, board, check, list);

行を変更する ( findWords)

     getAllWords(word, x, y, board, check, list);

     list = getAllWords(word, x, y, board, check, list);

その他

優れたプログラミング手法として、常に から返される値を確認してくださいmalloc。そうすれば、NULL ポインターを逆参照することによる不快な結果を回避できます。

struct ListNode * newNode = malloc(sizeof(struct ListNode));
if ( newNode == NULL )
{
   // Deal with error condition.
   // This is one way to deal with it - print an error message and exit.
   perror("Unable to get memory for a ListNode.\n");
   exit(EXIT_FAILURE);
}
于 2015-01-18T07:12:12.543 に答える