空长

时间:2015-01-18 07:01:12

标签: c matrix linked-list boggle

此函数用于将节点及其数据插入链接列表。

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;
}

这个函数是我从boggle board得到所有单词的地方(这个函数似乎工作正常,因为当我打印出这里的单词时,它会正确打印出来。

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;
 }

1 个答案:

答案 0 :(得分:1)

我看到的问题:

问题1

newNode->word = word;

不对。链表中的每个节点都将存储指向该节点的指针 内存块,从getAllWords传递。更糟糕的是,那块 memory对应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;
}

如果要将新节点添加到列表末尾,则逻辑为a 更多参与。

问题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);
}