0
我正在嘗試使用將數據從節點切換到節點的函數對國際象棋遊戲中的棋步進行排序。我的比較器是可以返回0,1,2的函數「isCapture」。最後,我希望名單是2222 ... 11111 ... 000 ..。 我到目前爲止的代碼是這樣的:C:對鏈表進行排序導致無限循環?
void sort(move_t* head, game_t game) {
move_t* move, *next;
int x1,y1,x2,y2;
char s;
move = head;
while(move != NULL)
{
next = move->next;
while(next != NULL)
{
if(((isCapture(game,move) == 0) && (isCapture(game,next)== 1)) ||
((isCapture(game,move) == 0) && (isCapture(game,next)== 2)) ||
((isCapture(game,move) == 1) && (isCapture(game,next)== 2)))
{
x1 = move->x1;
y1 = move->y1;
x2 = move->x2;
y2 = move->y2;
s = move->s;
move->x1 = next->x1;
move->y1 = next->y1;
move->x2 = next->x2;
move->y2 = next->y2;
move->s = next->s;
next->x1 = x1;
next->y1 = y1;
next->x2 = x2;
next->y2 = y2;
next->s = s;
if(isCapture(game,move) == 1)
next = next->next;
else
{
move = next;
next = next->next;
}
}
else if(((isCapture(game,move) == 2) && (isCapture(game,next)== 1)) ||
((isCapture(game,move) == 2) && (isCapture(game,next)== 2)) ||
((isCapture(game,move) == 2) && (isCapture(game,next)== 0)))
{
move = move-> next;
next = move-> next;
}
else
{
next = next->next;
}
}
move = move->next;
}
}
我已經得到錯誤的混合物:無限循環,無效未來大小(快)和腐敗的鏈表。我查看了代碼,但無法弄清楚。希望有人能檢查我的代碼。謝謝。