2015-10-02 40 views
0

以下是桶排序程序的代碼。將鏈接列表作爲參數傳遞時出錯

typedef struct node_struct { 
    double d; 
    struct node_struct *next; 
} node; 

我使用插入排序,以

void insert(double value, int index, node *B[]) { 
    node *t; 
    if (B[index] == NULL) { 
     t = (node *)malloc(sizeof(node)); 
     t->d = value; 
     t->next = NULL; 
     B[index] = t; 
    } else { 
     node *p0, *p1;   
     p0 = B[index]; 
     p1 = p0 -> next; 
     while(p1 != NULL) { 
      if (p1 -> d > value) { 
       break; 
      } 
      p1 = p1->next; 
      p0 = p0->next; 
    } 
    t = (node *)malloc(sizeof(node)); 
    t->d = value; 
    t->next = p1; 
    p0->next = t; 
} 

void Bucket_Sort(double A[], int n) { 
    int j, index; 
    double B[n]; 
    node *B1; 
    B1 = (node *)malloc(sizeof(node)); 
    for (int i = 0; i < n; i++) { 
     B[i] = 0; 
    } 
    B1->d = A[0]; 
    B1->next = NULL; 
    for (int i = 1; i <= n; i++) { 
     index = (int) floor(n * A[i]); 
     insert(A[i], index, B1); // This part of the program is where I'm going wrong 
    } 
    for (int = 0; i < n; i++) { 
     printf("%f \n", B[i]); 
    } 
} 

當我嘗試調用插入功能,錯誤發生時說:「期待結構節點**值進行排序,但參數的類型結構節點*「

但是,如果我呼叫插入功能如下: insert(A [i],index,& B1); 然後在編譯時沒有給出錯誤,但是當我運行程序時會導致分段錯誤。有人可以幫我解決困惑嗎?

+2

好吧,沒錯,'B1'是指向'node'的指針,而你的函數需要指向'node'的指針數組。 – mangusta

+0

這個問題很混亂。所以,當你提到一個錯誤時,你怎麼稱呼這個函數呢? – mangusta

+0

我用insert(A [i],index,&B1); – Raghuveer

回答

0

您的插入函數指示b是指向節點對象的指針數組。

但是你沒有傳入一個指針數組,你可以用& b1來調用它,它是一個指向單個節點(不是數組)的指針。而當你使用這樣的數組時,通常需要傳遞一些元素,而通過指向鏈表元素的指針,通常使用null來指示列表的末尾。

如果我是你,我只是通過指針來處理所有事情,並擺脫[],因爲你真的沒有正確傳遞數組。例如。而不是傳入索引,只需傳遞一個指向感興趣對象的指針即可。在更棘手的情況下,你可以使用**指針指針,但這需要很好的理解你正在做什麼。

谷歌鏈接列表的例子,以獲得想法如何正確處理與指針。你會明白的。

否則在傳遞解釋數組並傳入計數並在循環中使用count變量的方式應保持一致。我建議不要嘗試混合[]表格和***範例,直到您對每個表格分別感到滿意爲止。

typedef struct node_struct { 
    double d; 
    struct node_struct *next; 
} node; 

void insert(double value, int index, node *b[]) { 
    node *t; 
    if (b[index] == NULL) { 
     t = (node *)malloc(sizeof(node)); 
     t->d = value; 
     t->next = NULL; 
     b[index] = t; 
    } else { 
     node *p0, *p1;   
     p0 = b[index]; 
     p1 = p0 -> next; 
     while (p1 != NULL) { 
      if (p1 -> d > value) { 
       break; 
      } 
      p1 = p1->next; 
      p0 = p0->next; 
    } 
    t = (node *)calloc(sizeof(node), 1); 
    t->d = value; 
    t->next = p1; 
    p0->next = t; 
} 

void Bucket_Sort(double a[], int n) { 
    int j, index; 
    double b[n]; 
    node *b1 = (node *)calloc(sizeof(node), 1); 
    a1->d = a[0]; 
    b1->next = NULL; 
    for (int i = 1; i <= n; i++) { 
     index = (int) floor(n * a[i]); 
     insert(a[i], index, b1); 
    } 
    for (int = 0; i < n; i++) { 
     printf("%f \n", b[i]); 
    } 
} 

我在你的問題中格式化了程序,並在下面進一步討論。這是我越怎麼看寫的專業代碼庫的代碼,當我做同行代碼審查,等等

一些注意事項:

•如果使用calloc()代替的malloc,你的緩衝區是自動歸零。人們通常使用bzero()memset()來歸零,而不是使用for()循環。

•您可以同時聲明一個變量,如B1,並節省空間/雜波;

•您可以在for循環中聲明變量類型,並將其範圍限定爲for循環。使其清晰並節省垂直空間。

•不要太格格不入。編程界對此感到沮喪。在任何可敬的C編碼地方都有編碼標準,並且非常嚴格,因此代碼看起來乾淨,易讀,易於理解和維護,並且一致。如果每個人都應用自己的轉變,那麼大型編碼基礎就會變成一個難看的維護噩夢。

•不要在指針前後添加空格->沒有人這樣做,而且很難被有經驗的程序員閱讀。在逗號之後留出空格,出於與編寫時相同的原因 - 更直觀地分離項目 - 更易於調試等等。

•資本用於常量。駱駝案例(第一個字母小寫,後面的單詞首字母大寫,例如thisIsMyVariable),用於變量,或者用下劃線this_is_my_variable。用大寫字母命名數組很粗俗,您幾乎從不會在專業代碼中看到它。

+0

我在最初提交答案後做了一系列的編輯,現在我已經完成了。 – clearlight

相關問題