資源簡介
已知head為單鏈表的表頭指針,鏈表中存儲的都是整形數據,實現下列運算的遞歸算法:
(1)求鏈表中的最大值。
(2)求鏈表中的結點個數。
(3)求所有整數的平均值。
代碼片段和文件信息
#include
using?namespace?std;
struct?Node
{
int?num;
Node*?next;
};
Node?*?Creat(int?a[]int?n)
{
int?i;
Node?*head=(Node*)malloc(sizeof(Node));
head->num=a[0];
Node?*pre=head;
Node?*p;
for(i=1;i {
p=(Node*)malloc(sizeof(Node));
p->num=a[i];
p->next=pre->next;
pre->next=p;
pre=p;
}
????p->next=NULL;
return?head;
}
int?Find(Node?*headint?n)
{
int?flat=head->num;
int?i=0;
for(;inexti++)
{
if(flatnum)
flat=head->num;
}
return?flat;
評論
共有 條評論