2016-03-23 14 views
-1

我有種理解如何釋放他們,但我敢肯定我在我的代碼中做錯了。免費的循環鏈接列表

while(*bestFriend != NULL){ 
        temptr = *bestFriend; 
        *bestFriend = (*bestFriend)->next; 
        free(temptr); 
        printf("Freed\n"); 
       } 

它的崩潰我的程序有點不確定是什麼造成它雖然。

編輯:代碼

int duckDuckBoot(jimmysFriend **bestFriend, int rounds, int howManyDucks, int numberOfFriends, int gameCounter){ 

    int roundCounter; 
    int i; 
    jimmysFriend *temptr; 
    temptr = *bestFriend; 
    roundCounter = 0; 
    if(rounds != 0){ 
    do{ 
     for(i = 0; i < howManyDucks;){ 
      i++; 
      if(i == howManyDucks){ 
        temptr = temptr->next; 
       if((*bestFriend)->next == *bestFriend){ 
        temptr = *bestFriend; 
        free(temptr); 
        *bestFriend = NULL; 
        printf("Game %d:\n", gameCounter); 
        printf("Jimmy has friends no more\n"); 
        return 0; 
       } 
       else if(temptr->next == *bestFriend){ 
       jimmysFriend *temptr2; 
       while(temptr->next->next != *bestFriend){ 
         temptr = temptr->next; 
       } 

       temptr2 = temptr->next; 
       temptr->next = *bestFriend; 
       free(temptr2); 
       temptr = *bestFriend; 
       } 
       else if(temptr == *bestFriend){ 
        jimmysFriend *temptr2; 
        temptr2 = *bestFriend; 
        while(temptr->next != *bestFriend){ 
         temptr = temptr->next; 
        } 
        temptr->next = (*bestFriend)->next; 
        (*bestFriend) = (*bestFriend)->next; 
        free(temptr2); 
       } 
       else{ 
        jimmysFriend* temptr2; 
        temptr2 = *bestFriend; 

        while(temptr2->next->next != temptr->next){ 
         temptr2= temptr2->next; 
        } 
        jimmysFriend *temptr3; 
        temptr3 = temptr; 
        temptr2->next = temptr->next; 
        temptr = temptr->next; 
        temptr2 = NULL; 
        free(temptr3); 
        free(temptr2); 


       } 
       roundCounter++; 
       } 
      else{ 
      temptr = temptr->next; 
      } 


     } 

     }while(roundCounter != rounds); 
     if(roundCounter == rounds){ 
      char** nameList; 
      int listSize; 
      nameList = allocMemory(numberOfFriends); 
      listSize = dataTransfer(*bestFriend, nameList, numberOfFriends); 
      printf("Game %d:\n", gameCounter); 
      for(i = 0; i < listSize; i++){ 
       printf("%s\n",nameList[i]); 

       } 

      for(i = 0; i < listSize; i++){ 
        free(nameList[i]); 
        free(nameList); 
       } 
      while(*bestFriend != NULL){ 
        temptr = *bestFriend; 
        *bestFriend = (*bestFriend)->next; 
        free(temptr); 
        printf("Freed\n"); 
       } 


     } 







    } 


    return 1; 
} 
+2

請顯示[最小的完整和可驗證的示例](https://stackoverflow.com/help/mcve)。 – kaylum

+0

您存儲您開始的節點,並在當前節點的下一個節點是起始節點時停止。 – xaxxon

+0

@kaylum你的意思是我的輸出,還是更多的我的程序? – Jude

回答

0

的休息當你做

while(*bestFriend != NULL) 

你忘了這是圓形的。要釋放的最後一個節點的下一個將是您釋放的第一個節點。這會產生一個問題,因爲該內存已從您的程序中解除分配。這會導致分段錯誤。

我的建議是沒有一個圓形莊園的清單,它只會有一個下一個指針沒有填充沒有區別。

0

圓形鏈表不會指向NULL,因爲它有一個或多個節點。

但是你正在做while(*bestFriend != NULL)這意味着你不會將給定的列表視爲循環。