我正在实现一个冒泡排序函数,它对单词进行排序。交换功能的话完全好,但我无法得到的错误。试着在网上搜索,但找不到有用的东西。我在哪里得到了错误的标记。
谢谢你的帮助
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);
}
}
字符串
2条答案
按热度按时间xuo3flqw1#
当您尝试使用已前向声明但未 * 定义 * 的
struct
时,会出现此错误。虽然声明和操作指向此类结构体的 * 指针 * 是完全可以的,但尝试解引用它们是不可以的,因为编译器需要知道它们的大小和布局以便执行访问。具体来说,在您的例子中,编译器不知道
struct node
有next
,所以字符串
不编译。
您需要在定义
sortWord
函数的编译单元中包含struct node
的定义来解决此问题。raogr8fs2#
您应该替换此行:
字符串
用这句话:
型
原因是在这段代码中,您对
node
的结构一无所知。我想这就是为什么你对temp2使用nodeGetNextNode
函数的原因。你只需要用它来做临时工。