[英]Having trouble with Insert Sort for Linked List. Only prints first value of list when I try to sort the list in ascending order
我正在為我的一個班級編寫一個程序,並被困在要求按升序對鏈表進行排序的部分。 我試圖對列表進行排序,但是當我運行該函數時,它只打印第一個值。 我知道打印功能有效,因為我可以在沒有插入排序的情況下運行它,並且打印效果很好。
我的代碼如下:
#include "linkedList.h"
#include <stdlib.h>
#include <stdio.h>
void swap(struct Link *first, struct Link *second){
struct Link* temp = first;
temp->next = first->next;
temp->value = first->value;
first = second;
first->next = second->next;
first->value = second->value;
second = temp;
second->next = temp->next;
second->value = temp->value;
}
struct Link* listInsertionSort(struct Link* head) {
/*
* This function should perform an insertion sort on the list whose head is
* provided as the function's argument, so that the values in the list are
* sorted in ascending order, starting at the head.
*
* The sort should be done without allocating any new Link structs or any
* other auxiliary data structures.
*
* Return a pointer to the new head of the list.
*/
struct Link* cur = head;
cur->next = head->next;
struct Link* count;
for(;cur->next != NULL; cur = cur->next){
for(count = cur->next; count != NULL; count = count->next){
if(cur->value < count->value){
swap(cur, count);
}
}
}
return cur;
}
而linkedList.h 文件在這里:
#ifndef __LINKEDLIST_H
#define __LINKEDLIST_H
#define TYPE int
/* Single link structure */
struct Link {
TYPE value;
struct Link* next;
};
struct Link* listInsertionSort(struct Link* head);
struct Link* reverseList(struct Link* head);
struct Link* reverseListRecursive(struct Link* head);
#endif
測試文件在這里(雖然這里不應該有任何錯誤,因為這是我們的老師提供給班上所有學生的):
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <float.h>
#include "linkedList.h"
struct Link* buildLink(int n, int rev, int mod) {
struct Link* head = (struct Link*)malloc(sizeof(struct Link));
struct Link* cur = head;
for (int i = 0; i < n; i++) {
if (rev)
cur->value = n - i - 1; //If rev is 1, creates list from high value to low value
else
cur->value = i; //If rev is 0, creates list from low value to high value
if (mod)
cur->value = cur->value % mod; //Modifies list so that it increments up to value of mod
if (i + 1 < n)
cur->next = (struct Link*)malloc(sizeof(struct Link)); //Creates next link in the array
else
cur->next = 0; //If less than the cap it sets next character to NULL, ending the list
cur = cur->next; //Sets current link to next link to continue for loop
}
return head;
}
void printLL(struct Link* l,char* s) {
printf("LL %s: ",s);
while (l != 0) {
printf("%d ", l->value);
l = l->next;
}
printf("\n");
}
int main() {
// We aren't practicing good memory management
// here....
struct Link* l = buildLink(10, 0, 4);
struct Link* r = listInsertionSort(l);
printLL(r, "Sort 0-9 mod 4"); //This should print 0 0 0 1 1 1 2 2 3 3
}
有誰知道這里的問題是什么? 錯誤出現在 listInsertionSort(struct Link* head) 中,但我嘗試了多種不同的組合但沒有成功。
當前輸出是:LL Sort 0-9 mod 4:1 什么時候應該是:LL Sort 0-9 mod 4: 0 0 0 1 1 1 2 2 3 3
我找到了解決我遇到的問題的方法。
而不是返回 cur ,我應該返回 head 。 我還更改了交換函數,使其不包括列表中鏈接的交換值,因為這是不必要的並且看起來很混亂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.