用指针c ++对单词序列进行排序

时间:2016-05-07 00:21:02

标签: c++ pointers

关于stackoverflow的第一个问题,我需要用指针对一系列单词进行排序,任何人都可以帮助我吗? - 确定没关系nod_curent-> NOD_urmator-> word

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

1 个答案:

答案 0 :(得分:1)

1)NOD_urmator怎么样?您使用它但在NOD中未定义。它与NOD_next相同?

2)cuvant怎么样?您使用它但在NOD中未定义。它与word相同?

3)nod_word怎么样?您使用它但在sort_list()中未定义。它与nod_curent相同?

4)以下测试是错误的(我猜)并且很危险(我确定)

while(nod_curent!=NULL||nod_curent->NOD_urmator!=NULL)

因为如果nod_curent NULL失败了第一个条件(nod_curent!=NULL),那么“||”条件意味着它已经测试了第二个条件(nod_curent->NOD_urmator!=NULL })但是nod_curent == NULL你取消引用空指针。崩溃!

你的目的是使用“和”操作符吗?

while ( (nod_curent != NULL) && (nod_curent->NOD_urmator != NULL) )

相同
while ( nod_curent && nod_curent->NOD_urmator )