我有種理解如何釋放他們,但我敢肯定我在我的代碼中做錯了。免費的循環鏈接列表
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;
}
請顯示[最小的完整和可驗證的示例](https://stackoverflow.com/help/mcve)。 – kaylum
您存儲您開始的節點,並在當前節點的下一個節點是起始節點時停止。 – xaxxon
@kaylum你的意思是我的輸出,還是更多的我的程序? – Jude