-3

それで、私はCでリンクリストアルゴリズムを実装しようとしていました.コードは次のとおりです:

#include <stdio.h>
#include <stdlib.h>

typedef struct lista{
int info;
struct lista *next;
} *aplinked, strulinked;

aplinked insereNoFim(aplinked inicio, aplinked novo)
{
aplinked temp;
if (inicio == NULL)
    inicio = novo;
else{
    temp = inicio;
    while (temp->next != NULL)
        temp=temp->next;
    temp->next = novo;
}
return inicio;

}

aplinked lenovo()
{
aplinked x;
x = (aplinked) malloc (sizeof(strulinked));
scanf ("%d", &x->info);
return x;
}

void exibe(aplinked inicio){
aplinked temp = inicio;
if (temp == NULL)
printf ("Não existe dados");
else
while (temp!=NULL){
printf ("\n info: %d \n", temp->info);
temp = temp->next;
}
}


aplinked remover(aplinked inicio, int x)
{
aplinked ant = NULL;
aplinked temp = inicio;
//procura o elemento na lista, guardando o anterior.
while (temp!=NULL && temp->info != x){
        ant = temp;
        temp = temp->next;
}
//verifica se achou
if (temp == NULL)
    return inicio; //no caso de não achar
if (ant == NULL)
    inicio = temp->next; //retirar o 1o elemento
else
    ant->next = temp->next;
free (temp);
return inicio;
}



int pesquisa (aplinked inicio, int x){
aplinked temp = inicio;
while (temp!=NULL){
    if (temp->info == x)
        return 1;
    temp = temp->next;
    }
return 0;
}

int main ()
{
int cont = 1;
aplinked inicio = NULL;
while (cont){
inicio = insereNoFim(inicio, lenovo());
scanf ("%d", &cont);
}
exibe(inicio);
printf ("Digite numero a ser pesquisado: \n");
scanf ("%d", &cont);
if (pesquisa (inicio, cont))
    printf ("achou o elemento buscado \n");
else
    printf ("não achou");

printf ("Digite elemento a ser removido: \n");
scanf ("%d", &cont);
inicio = remover (inicio, cont);
exibe (inicio);
}

ええ、コードは英語ではありませんが、それが何であるかについてのアイデアがあるかもしれません。そのため、リンクされたリスト、削除/挿入/検索/印刷関数は、Linux では正常に実行されますが、Windows では正常に実行されません! !

何か案は?

4

2 に答える 2

2

デバッガの使い方を学ぶことを強くお勧めします。これはあなたの問題です:

aplinked temp;
if (inicio == NULL)
    inicio = novo;
else{
    temp = inicio;
    while (temp->next != NULL)  //  you never set this to NULL
        temp=temp->next;

明示的に設定temp->nextNULLたことがないため、何かを含めることができます。どうやら Linux では NULL (運がよかった) で、Windows ではただのジャンクなjunk != NULLので、設定して逆参照しようとするとクラッシュします。

だけでなく、関数内の構造体のすべての要素を入力する必要がありますlenovo()info

于 2013-04-11T18:26:58.200 に答える
1

next新しいノードを割り当てるとき、そのポインターを初期化しません。これは、一見ランダムな場所を指すことを意味し、リストの最後を見つけようとすると、実際の最後をループし続けます。

于 2013-04-11T18:26:28.040 に答える