您可以在顯示功能改變循環到這一點:
for(i=0;i<10;i++){
printf("A[%d]: ",i);
L tmp = A[i];
while(tmp!=NULL){
printf("%d\t",tmp->elem);
tmp = tmp->next;
}
printf("\n");
}
沒有必要對整個陣列複製,簡單通過鏈接列表導航的臨時指針就足夠了。
旁註:對於數組的副本,你試圖分配它tempStruct = A;
。有兩個原因不起作用:
在你的函數中,A
沒有數組類型。 C不支持將數組傳遞給函數。當一個函數有一個數組類型的參數時,它會自動調整爲一個指針類型,而不是傳遞一個數組,而是傳遞一個指向數組第一個元素的指針。這種效果通常表示爲陣列衰減爲指針,這就是您的消息incompatible types node** to Dictionary{*node[10]}
的原因。
即使A
有數組類型,但它仍然是行不通的,因爲C不允許分配到一個數組。這有點令人驚訝,因爲同樣的事情可以用於struct
。我想不出在C中不允許分配數組的原因,你應該記住你不能。當然,你可以做手工,如果你不希望每一個元素分配,你可以使用函數memcpy()
,在string.h
聲明:
int foo[5];
int bar[5] = {1, 2, 3, 4, 5};
// instead of foo = bar;
memcpy(foo, bar, sizeof foo);
無關你的問題,但是我很難理解這段代碼。你的typedef
是可讀性災難性的。 永遠不會將指針隱藏在typedef
之後 - 爲了理解處理指針的代碼,重要的是指針是明顯的。數組類型的typedef
至少也是有問題的。我建議以下代碼:
typedef struct node {
int elem;
struct node *next;
} node;
// not strictly necessary, but IMHO, if you want to typedef a struct type,
// it's the least confusing option to name it the same as the struct tag.
#define DICTSIZE 10
void display(node **a) // variable names are often lowercase by convention
{
// to cope with ANY possible size, you need size_t, int might be too small
// include stddef.h or stdlib.h to use it. Of course, with 10 elements,
// int is enough.
for (size_t i = 0; i < DICTSIZE; ++i) {
printf("a[%zu]: ", i);
node *tmp = a[i];
// now it's obvious tmp is a pointer, so no need to explicitly
// write the != NULL ... (any pointer that's not NULL evaluates true)
while (tmp) {
printf("%d\t", tmp->elem);
tmp = tmp->next;
}
printf("\n");
}
}
同樣要注意一些額外的空間大大提高了代碼(因此,使用它們)的可讀性。
我會考慮原來的顯示功能破,因爲它修改它所顯示出來。這不是顯示數據的功能的預期行爲。如果你想進一步改進你的代碼,你應該使用const
來明確該函數不應該修改它接收到的內容,所以編譯器可以捕獲錯誤。在上面的例子中,display
簽名應該更好的是這樣的:
void display(const node *const *a)
第一const
將使任何struct node
不變,第二const
(星號)之後,使你的指針數組一成不變英寸有了這個,你也必須寫
const node *tmp = a[i];
因爲你不能指定一個const指針指向非const指針。
良好的聲音螺母和螺栓編程建議。 –