我想排序一個鏈接的節點列表。我遍歷列表中的每個節點,將它與當前頭進行比較,如果它「較大」,則交換。最後我再次用head->next
。 我想讓函數返回新排序函數的「頭部」。C中的通用鏈接列表排序功能
第一個問題是它似乎卡在第一個for循環中。
我哪裏出錯了?
struct Node{
char firstName[50];
char lastName[50];
int age;
struct Node *next;
};
struct Node * sortList(struct Node * head,
int(*cmp)(struct Node *, struct Node *)){
printf("Entered Sorting Function\n");
struct Node * target;
struct Node * p;
struct Node * first;
bool firstRun = TRUE;
target=head;
first=head;
for (p=head; p!=NULL; p=p->next) {
if ((*cmp)(p, target) == 1)
printf("Comparing %s with %s\n", p->firstName, target->firstName);
target = p;
}
if (head!=target) {
printf("swapping nodes");
swapNodes(head, target);
}
if (firstRun==TRUE) {
first = head;
printf("setting very first to: %s\n", first->firstName);
firstRun = FALSE;
}
if (target->next != NULL) {
printf("recurring with %s\n", target->next->firstName);
sortList(target->next, cmp);
}
return first;
}
更新:
在第一循環的問題是缺少周圍的語句括號中if
,如nemetroid在此answer指出 - 感謝,nemetroid。代碼應爲:
for (p=head; p!=NULL; p=p->next) {
if ((*cmp)(p, target) == 1) {
printf("Comparing %s with %s\n", p->firstName, target->firstName);
target = p;
}
}
但是,排序仍然不起作用。還有什麼錯誤?
我發現我的swap不起作用,正如人們提到的。這是包含交換的更新代碼。
更新:我修復了交換,現在我只交換節點的內容。
void swapNodes(struct Node * nodeA, struct Node * nodeB) {
struct Node * tmp = malloc(sizeof(struct Node));
strcpy(tmp->firstName, nodeA->firstName);
strcpy(tmp->lastName, nodeA->lastName);
tmp->age = nodeA->age;
strcpy(nodeA->firstName, nodeB->firstName);
strcpy(nodeA->lastName, nodeB->lastName);
nodeA->age = nodeB->age;
strcpy(nodeB->firstName, tmp->firstName);
strcpy(nodeB->firstName, tmp->firstName);
nodeB->age = tmp->age;
}
但排序是仍然沒有工作,這裏是我的排序當前代碼:
void sortList(struct Node * head,
int(*cmp)(struct Node *, struct Node *)){
if (head==NULL || head->next==NULL)
return;
int length = listLength(head);
int i, j;
struct Node * p = head;
struct Node * q = p;
bool firstRun = TRUE;
for (i=0; i<length; i++) {
if (firstRun != TRUE)
p=p->next;
else if (firstRun == TRUE)
firstRun = FALSE;
for (j=i+1; j<length-1; j++) {
q = q->next;
if ((*cmp)(q, p) == 1)
swapNodes(p, q);
}
}
}
我固定它。在答案中發佈排序代碼。感謝大家的幫助。
'我哪裏出錯了?' - 不調試代碼/數據。 – 2014-12-13 17:03:47
請不要通過黑客問題使答案無效;更新問題以顯示部分修復。這次我會做,說明。 – 2014-12-13 17:39:32
你需要顯示'swapNodes()'的主體,因爲這個調用至少是可疑的:'swapNodes(head,target);'。通常情況下,您需要將指針傳遞給要交換的指針,但它取決於代碼的作用。 firstRun代碼也是可疑的;你應該簡單地將遞歸調用的返回值記錄在合適的地方(也許'first-> next = sortList(head-> next,cmp);')。 – 2014-12-13 17:46:28