gpt4 book ai didi

c - 获取空长度

转载 作者:行者123 更新时间:2023-11-30 15:25:16 26 4
gpt4 key购买 nike

在这个函数中,我将节点及其数据插入到链表中。

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

最佳答案

我看到的问题:

问题1

newNode->word = word;

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

关于c - 获取空长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com