C++ 使用指针c+对单词序列进行排序+;
关于stackoverflow的第一个问题,我需要用指针对一系列单词进行排序,有人能帮我吗? -不确定是否可以点鼠标->点鼠标->单词C++ 使用指针c+对单词序列进行排序+;,c++,pointers,C++,Pointers,关于stackoverflow的第一个问题,我需要用指针对一系列单词进行排序,有人能帮我吗? -不确定是否可以点鼠标->点鼠标->单词 #include <stdio.h> #include <stdlib.h> #include <cstring> struct NOD { char word[20]; struct NOD *NOD_next; }; void sort_list(struct NOD *prim) { cha
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
struct NOD
{
char word[20];
struct NOD *NOD_next;
};
void sort_list(struct NOD *prim)
{
char tmp[50];
struct NOD *nod_curent;
nod_curent=prim;
while(nod_curent!=NULL||nod_curent->NOD_urmator!=NULL)
{
if(strcmp(nod_curent->cuvant,nod_word->NOD_urmator->word)>0)
{
strcpy(tmp,nod_curent->word);
strcpy(nod_curent->word,nod_curent->NOD_urmator->word);
strcpy(nod_curent->NOD_urmator->word,tmp);
}
nod_curent=nod_curent->NOD_urmator;
}
while(nod_curent!=NULL)
{
printf(" %s\n", nod_curent->cuvant);
nod_curent=nod_curent->NOD_urmator;
}
}
#包括
#包括
#包括
结构节点
{
字符字[20];
结构节点*下一个节点;
};
无效排序列表(结构节点*prim)
{
char-tmp[50];
结构节点*节点当前;
nod\u curent=prim;
while(nod_curent!=NULL | | nod_curent->nod_urmator!=NULL)
{
if(strcmp(nod\u curent->cuvant,nod\u word->nod\u urmator->word)>0)
{
strcpy(tmp,nod\u curent->word);
strcpy(nod\u curent->word,nod\u curent->nod\u urmator->word);
strcpy(nod\u curent->nod\u urmator->word,tmp);
}
nod\u curent=nod\u curent->nod\u urmator;
}
while(nod\u curent!=NULL)
{
printf(“%s\n”,nod\u curent->cuvant);
nod\u curent=nod\u curent->nod\u urmator;
}
}
1)NOD\u urmator
怎么样?您使用它,但它在NOD
中未定义。这与下一步的点头相同
2) cuvant怎么样?您使用它,但它在NOD
中未定义。它与word相同
3) 那nod\u word呢?您使用它,但它在排序列表()中未定义。
。它与当前的nod\u\n相同
4) 下面的测试是错误的(我想)和危险的(我肯定)
因为如果nod\u curent
是NULL
使第一个条件(nod\u curent!=NULL
)失败,所以“|
”条件意味着它已经测试了第二个条件(nod\u curent->nod\u urmator!=NULL
),但是使用nod\u curent==NULL
,您正在取消对空指针的引用。撞车
你打算用“和”操作符
while ( (nod_curent != NULL) && (nod_curent->NOD_urmator != NULL) )
这和
while ( nod_curent && nod_curent->NOD_urmator )
看起来像C,但是当你明确地要求C++时,我删除了错误的标签。你能解释一下你有什么错误或者期望的结果是什么吗?
while ( nod_curent && nod_curent->NOD_urmator )