[英]Nested Linked Lists and Functions in C
我有一個鏈表,其中存儲了兩個 integer 變量,稱為身高和體重。
struct measurement {
int height;
int weight;
struct measurement *next;
};
我還有另一個鏈接列表,其中嵌套了度量。
struct person {
struct measurement *measurement;
struct person *next;
};
我已經創建了一個主要的 function,它接受重量/高度的輸入並將其傳遞到 function 以將其添加到“人”鏈接列表中。
int add_data(struct person *person, int height, int weight) {
struct person *add = (struct person *)malloc(sizeof(struct person));
add->measurement = (struct measurement *)malloc(sizeof(struct measurement));
int check = 0;
if (height < 250 && height > 0 && weight > 0) {
check = 1;
}
if (check == 1) {
add->measurement->height = height;
add->measurement->weight = weight;
add->measurement = add->measurement->next;
return 1;
} else {
return 0;
}
// This function returns an integer if the measurements are successfully added.
但是,當 function 似乎沒有按預期移動到下一個節點時。 測試時,我可以分別打印出身高和體重的整數,但每次再次運行 function 時,它似乎與之前的測量值重疊。 (這個程序是為了記錄一個人的成長加班,所以才會有多次測量身高和體重)
我認為你在這里有一個錯字:
add->measurement = add->measure->next;
它應該是:
add->measurement = add->measurement->next;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.