2016-03-23 71 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)这意味着你不会将给定的列表视为循环。