我有點卡在如何使用戶定義的函數打印輸出。我還必須創建一個用戶定義的函數,將每個節點中的數據相加並打印出總數,但它不能正確累加,並且格式稍微偏離。用戶定義函數和鏈接列表C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char printout();
int sum();
typedef struct node
{
int number;
struct node*next;
} node;
char printout()
{
};
int sum()
{
int s,sum_all=0, node_sum=0;
for(s=0;s=100;s++)
{
sum_all=node_sum+s;
return printf("The sum of all nodes is %d.\n",sum_all);
};
};
int main()
{
srand (time(NULL));
int i, total=0;
struct node*head=malloc(sizeof(struct node));
head->number = rand()%100;
printf("Node #%d contains %d.\n", 0, head->number);
struct node*here=head;
for (i=1; i<100; i++)
{
here->next=malloc(sizeof(struct node));
here->number=rand()%100;
printf("Node #%d contains %d.\n", i, here->number);
};
total=sum(here->number);
printf("%2.2d", total);
return 0;
}
我不明白插入是如何工作的:(你在每次循環時覆蓋'here-> next'而不將它存儲在任何地方,即泄漏它。關於鏈表的事情是元素必須實際上是鏈接:) – ThingyWotsit
第一次迭代時,函數'sum'中'for'循環的重點是什麼? –
爲什麼返回類型的函數'printout'' char'? – BLUEPIXY