//using iteration
int Add(struct Node *p){
int sum=0;
while(p){
sum= sum+ p->data;
p= p->next;
}
return(sum);
}
//using recursion
int Add(struct node *p){
if (p==0)
return(0);
else
return Add(p->next) + p->data;
}
//the data will be added at the time of returning
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter