6 c
我正在实现一个冒泡排序功能,它可以对单词进行排序.交换功能字很好,但我无法得到错误.尝试在线搜索,但无法获得有用的东西.我已经标记了我得到错误的地方.
感谢您的帮助.
void sortWord (struct node** head) {
struct node* temp = (*head);
struct node* temp2 = (*head);
int i;
int j;
int counter = 0;
while(temp != NULL)
{
temp = temp->next; //<-- this is where i get the error.
counter++;
}
for( i = 1; i<counter; i++)
{
temp2=(*head);
for(j = 1; j<counter-1;j++)
{
if(wordCompare(temp2,nodeGetNextNode(temp2))>0)
{
swap(head,temp2,nodeGetNextNode(temp2));
continue;
}
}
temp2 = nodeGetNextNode(temp2);
}
}
Run Code Online (Sandbox Code Playgroud)
das*_*ght 12
当您尝试使用struct已向前声明但未定义的语句时,会出现此错误.虽然声明和操作指向这些结构的指针是绝对可以的,但尝试取消引用它们并不行,因为编译器需要知道它们的大小和布局才能执行访问.
具体来说,在您的情况下,编译器不知道struct node有next,所以
temp->next
Run Code Online (Sandbox Code Playgroud)
不编译.
您需要struct node在编译单元中包含定义sortWord函数的定义,以便解决此问题.
您应该替换此行:
temp = temp->next;
Run Code Online (Sandbox Code Playgroud)
用那条线:
temp = nodeGetNextNode(temp);
Run Code Online (Sandbox Code Playgroud)
原因是在这段代码中,您对node. 我想这就是你nodeGetNextNode为 temp2使用函数的原因。你只需要将它用于临时。