C 如何从函数返回链表?

C 如何从函数返回链表?,c,linked-list,C,Linked List,首先,很抱歉我的英语不好,希望你能理解我。 我对编程基本上是新手,在老师让我们做的一个小项目中需要帮助。(家庭作业) 在本作业中,我创建了一个链接列表: struct No { char Nome[30]; char Endereco[30]; int Numero; int aux; struct No *prox; }; typedef struct No no; 然后,我添加了一些函数来填充数组。问题是我的一个函数没有以任何方式改变数组,就像我根本

首先,很抱歉我的英语不好,希望你能理解我。 我对编程基本上是新手,在老师让我们做的一个小项目中需要帮助。(家庭作业) 在本作业中,我创建了一个链接列表:

struct No {
    char Nome[30];
    char Endereco[30];
    int Numero;
    int aux;
    struct No *prox;
};
typedef struct No no;
然后,我添加了一些函数来填充数组。问题是我的一个函数没有以任何方式改变数组,就像我根本没有调用这个函数一样。 以下是重要部分:

int main(void)
{
    no *lista = (no *) malloc(sizeof(no)); //This is the linked list I'm talking about.

    Contagem=0;

    int iOpcao;
    while(iOpcao) {
        iOpcao=Escolha();
        if(selecionarOpcao(lista,iOpcao) == 0)
            return 0;
    }

    return 0;
}

int selecionarOpcao(no *entrada, int op)
{
    no *tmp;
    switch(op){
        case 0:
            return 0;

        case 1:
            ListarContatos(entrada);
            break;

        case 2:
            AdicionarContato(entrada);
            break;

        case 3:
            RemoverContato(entrada);
            break;

        case 4:
            EditarContato(entrada);
            break;

        case 5:
            entrada = OrganizarLista(entrada); //This is the function not working correctly, the other ones are working perfectly fine.
            break;

        default:
            printf("Comando invalido\n\n");
    }
    return 1;
}
最后是函数本身:

no* OrganizarLista(no* entrada) {
    no* ordenada = (no *) malloc(sizeof(no));
    no* temp = entrada;
    no* maiorNo = NULL;

    while(1 == 1) {
        while(temp != NULL) {
            temp = temp->prox;
            if(temp != NULL) {
                if(JaExistente(ordenada, temp->Numero) == 1)
                    continue;

                if(temp->prox != NULL && maiorNo == NULL) {
                    if(JaExistente(ordenada, temp->prox->Numero) == 1) {
                        maiorNo = temp;
                        continue;
                    }

                    //printf("%s contra %s", temp->Nome, temp->prox->Nome);
                    if(maiorNo == NULL)
                        maiorNo = CompararNos(temp, temp->prox);

                    //printf("%s ganhou.\n", maiorNo->Nome);
                }
                else if(maiorNo != NULL) {
                    //printf("%s contra %s", maiorNo, temp->Nome);
                    maiorNo = CompararNos(maiorNo, temp);
                    //printf("%s ganhou.\n", maiorNo->Nome);
                }
            }
        }

        if(maiorNo != NULL) {
            //printf("Maiorno->nome = %s", maiorNo->Nome);
            AdicionarLista(ordenada, maiorNo->Nome, maiorNo->Endereco, maiorNo->Numero, 1);
            temp = entrada;
            maiorNo = NULL;
        }
        else {
            temp = entrada;
            while(temp != NULL) {
                if(JaExistente(ordenada, temp->Numero) == 0)
                    AdicionarLista(ordenada, temp->Nome, temp->Endereco, temp->Numero, 1);
                temp = temp->prox;
            }
            break;
        }
    }
    free(temp);

    return ordenada;
}
我花了一整天的时间想弄明白,但它仍然不起作用。当我检查函数中的值时,它们都是正确的,但是当返回值时,程序似乎忽略了它们。
感谢您的帮助,再次为我的英语道歉,谢谢大家

您正在尝试为通过值发送的参数设置新值。 为了向函数发送指针或任何变量,并让函数更改其值,您应该通过引用、地址发送它

假设您具有以下功能:

void changePtr(no **ptr) {
    *ptr = // .. some value
}
void dontChangePtr(no *ptr) {
    ptr = // some value
}
您应该注意函数调用之间的差异:

no *n = (no*)malloc(sizeof(no));
dontChangePtr(n); // n will be sent by value and will not be changed
changePtr(&n); // n will be sent by reference and will be changed

返回指向第一个元素的指针。很抱歉,如果我没有具体说明,问题是,从该函数返回的任何内容都不会更改实际列表。sry.永远不要抛出
malloc
的返回值对不起,我只是对标题做出了反应。代码与标题几乎不匹配。请注意,您在
main()
at
int;while(iOpcao){
所以任何事情都可能发生。不能保证循环会被输入-也不能保证它不会被输入。你需要提供一个MCVE()。C不支持按引用传递!指针是一类类型!我根据你的答案做了一个简单的测试,将我的函数OrganizarLista更改为OrganizarLista(no**entrada){*entrada=NULL}并且结果相同,调用它的函数的列表仍然没有更改。让我给你一个例子:我的列表是1、2和3。我调用上面的测试函数,列表继续为1、2和3。谢谢你的帮助。