2014-06-16 80 views
1

這是我的一個鏈表結構:鏈表的幫助請求

typedef struct intervalo *Lista; 

typedef struct intervalo 
{ 
    int num; 
    Lista next; 
}Lista_int; 

這是我的代碼的部分(我做了),破壞名單:

Lista destroi_lista_res(Lista lista) 
{ 
    Lista temp_ptr; 
    while (lista->next!= NULL) 
    { 
     temp_ptr = lista; 
     lista= lista->next; 
     free(temp_ptr); 
    } 
    free(lista); 
    return NULL; 
} 

不幸的是,我的程序當這個函數被調用時掛起。具體地說,while (lista->next!= NULL)永遠不會終止。

我的問題:爲什麼這條線會導致無限循環?


附加代碼的細節:

在main(),創建了兩個列表。

/* Create linked list. */ 
Lista cria_lista_cab() 
{ 
    Lista aux; 
    aux=(Lista)malloc(sizeof(Lista_int)); 
    if(aux!=NULL) 
    { 
     aux->next=NULL; 
    } 
    return aux; 
} 

下列函數被用來添加數量的節點以結束兩個列表:

/* Insert node at the list tail. */ 
void insere_elem(Lista *lista,int num) 
{ 
    Lista aux,ant_pos=*lista,pos=ant_pos->next; 
    aux=(Lista)malloc(sizeof(Lista_int)); 
    while(pos!=NULL) 
    { 
     ant_pos=ant_pos->next; 
     pos=pos->next; 
    } 
    aux->num=num; 
    aux->next=pos; 
    ant_pos->next=aux; 
} 

下一個函數結合了列表的數量節點,消除在增加數字順序重複。返回結果列表:

Lista cria_lista_una(Lista lista1,Lista lista2) 
{ 
    Lista lista_res=cria_lista_cab(); 
    lista1=lista1->next; 
    lista2=lista2->next; 
    while(lista1!=NULL && lista2!=NULL) 
    { 
     if(lista1->num<lista2->num) 
     { 
      insere_elem(&lista_res,lista1->num); 
      printf("\n1 %d %d",lista1->num,lista2->num); 
      if(lista1!=NULL) 
       lista1=lista1->next; 
     } 
     else if(lista2->num<lista1->num) 
     { 
      insere_elem(&lista_res,lista2->num); 
      printf("\n2 %d %d",lista1->num,lista2->num); 
      if(lista2!=NULL) 
       lista2=lista2->next; 
     } 
     else if(lista2->num==lista1->num) 
     { 
      printf("\n3 %d %d",lista1->num,lista2->num); 
      if(lista1!=NULL) 
       lista1=lista1->next; 
      else if(lista2!=NULL) 
       lista2=lista2->next; 
     } 
    } 
    if(lista1!=NULL) 
    { 
     while(lista1!=NULL) 
     { 
      insere_elem(&lista_res,lista1->num); 
      lista1=lista1->next; 
     } 
    } 
    else if(lista2!=NULL) 
    { 
     while(lista2!=NULL) 
     { 
      insere_elem(&lista_res,lista2->num); 
      lista2=lista2->next; 
     } 
    } 
    return lista_res; 
} 

以下函數用於打印列表。

void imprime_lista_res(Lista lista) 
{ 
    lista=lista->next; 
    while(lista!=NULL) 
    { 
     printf("\nNum-> %d",lista->num); 
     lista=lista->next; 
    } 
} 

除了在清理時調用destroi_lista_res()並且程序掛起時,一切似乎都按預期運行。 。

+0

你看到的錯誤很可能在兩個地方之一:要麼調用這個函數的代碼,要麼代碼創建鏈接列表,你能發佈一個完全兼容的測試用例,展示你正在看到的問題嗎? –

+0

我已經編輯了上面的代碼,使用了我使用的所有功能 –

回答

0

給出代碼:

void insere_elem(Lista *lista,int num) 
{ 
    Lista aux,ant_pos=*lista,pos=ant_pos->next; 
    aux=(Lista)malloc(sizeof(Lista_int)); 

,並記住利斯塔實際上是定義爲 指針結構Lista_int

的實例您代碼變爲: 注意:您實際將節點追加到鏈接列表中,而不是插入

void insere_elem(struct Lista_int* *lista,int num) //note the ptr to ptr 
{ 
    struct Lista_int * aux; 
    struct Lista_int * ant_pos=*lista; //gets ptr to first instance of struct Lista_int 
    struct Lista_int * pos=ant_pos->next; // get ptr to NULL or second instance of.. 

    // get ptr to new instance of struct 
    aux=(struct Lista_int*)malloc(sizeof(Lista_int)); 

    // HERE should be checking that malloc() was successful 
    // otherwise, the lines: 
    // aux->num=num; 
    // aux->next=pos; 
    // will be writing to offsets from address 0 
    // will probably cause a crash 


    // step forward through linked list to find last struct 
    while(pos!=NULL) 
    { 
     ant_pos=ant_pos->next; // step ptr to prior struct 
     pos=pos->next;   // step ptr to current struct 
    } 

    aux->num=num; // set fields in new struct 
    aux->next=pos; // pos is already NULL, so probably (for clarity) just use NULL 
    ant_pos->next=aux; // set old struct instance ptr to new instance of struct 
} 

此外,關於這樣的代碼:

while(lista2!=NULL) 
{ 
    insere_elem(&lista_res,lista2->num); 
    lista2=lista2->next; 
} 

這個循環開始於頭鏈表「lista_res」 所以在目前的鏈接列表中的每個元素,它增加了另一種元素 在一般, 這意味着鏈接列表的大小與此代碼序列中的每個條目一樣大小

3

這可能是因爲lista爲NULL開頭。

更改功能:

Lista destroi_lista_res(Lista lista) 
{ 
    Lista temp_ptr; 
    while (lista!= NULL) 
    { 
     temp_ptr = lista; 
     lista= lista->next; 
     free(temp_ptr); 
    } 
    return NULL; 
} 
+0

我嘗試了,但它給出了一個問題,在行lista = lista-> next,我執行調試,它提出了lista的地址是0xabababab,我不知道它是否是問題...我不'不明白,因爲我已經運行lista與旅行和打印lista的元素,並打印所有元素沒有問題... –

+3

魔術數字0xABABABAB被用於標記「沒有人的在分配的堆內存之後「保護」字節。參考; http://www.nobugs.org/developer/win32/debug_crt_heap.html。看來你沒有正確設置鏈接列表。如果你可以更多地使用你的代碼,那將會很有幫助。 –

+0

我編輯了答案,添加了我使用的所有功能 –

0

在您的distroi函數中,如果Lista已經爲NULL,那麼它會做什麼,然後它會通過分段錯誤。但根據你的說法,如果你是從這個功能中走出來的,那麼你可能不會正確地創建你的列表。 先作如下變化:

Lista destroi_lista_res(Lista lista) 
{ 
    Lista temp_ptr; 
    while (lista!= NULL) 
    { 
     temp_ptr = lista; 
     lista= lista->next; 
     free(temp_ptr); 
    } 
    return NULL; 
} 

這是需要照顧的另一件事是,「有您創建列表正確,如果是,那麼你打印功能將無法正常工作,做到確保此請做一個虛擬遍歷函數用於調試目的,它只是像在你的distroi函數中一樣遍歷列表