获取空长度
Getting empty length
这个函数是我将节点及其数据插入链表的地方。
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] = '[=11=]';
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
传来的内存块。更糟糕的是,那块
内存对应于 getAllWords
中的一个函数局部变量,一旦你从 getAllWords
中 return 将不再有效。你会结束
节点指向悬挂内存。
你需要
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
的 return 值。
换行(在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
编辑的值 return。这样,您就可以避免取消引用 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);
}
这个函数是我将节点及其数据插入链表的地方。
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] = '[=11=]';
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
传来的内存块。更糟糕的是,那块
内存对应于 getAllWords
中的一个函数局部变量,一旦你从 getAllWords
中 return 将不再有效。你会结束
节点指向悬挂内存。
你需要
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
的 return 值。
换行(在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
编辑的值 return。这样,您就可以避免取消引用 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);
}