0% found this document useful (0 votes)
26 views2 pages

10.1 67. Count SumLinked List PDF

This C program defines functions to count the number of nodes and sum the data values in a linked list. It includes functions to create a linked list from an array, count nodes iteratively and recursively, and sum values iteratively and recursively. It then tests these functions by creating a linked list from a sample array and printing the count and sum.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

10.1 67. Count SumLinked List PDF

This C program defines functions to count the number of nodes and sum the data values in a linked list. It includes functions to create a linked list from an array, count nodes iteratively and recursively, and sum values iteratively and recursively. It then tests these functions by creating a linked list from a sample array and printing the count and sum.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Count and Sum Linked List

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
}*first=NULL;

void create(int A[],int n)


{
int i;
struct Node *t,*last;
first=(struct Node *)malloc(sizeof(struct Node));
first->data=A[0];
first->next=NULL;
last=first;

for(i=1;i<n;i++)
{
t=(struct Node*)malloc(sizeof(struct Node));
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}
}

int count(struct Node *p)


{
int l=0;
while(p)
{
l++;
p=p->next;
}
return l;
}

int Rcount(struct Node *p)


{
if(p!=NULL)
return Rcount(p->next)+1;
else
return 0;
}
int sum(struct Node *p)
{
int s=0;

while(p!=NULL)
{
s+=p->data;
p=p->next;
}
return s;
}

int Rsum(struct Node *p)


{
if(p==NULL)
return 0;
else
return Rsum(p->next)+p->data;
}

int main()
{

int A[]={3,5,7,10,25,8,32,2};
create(A,8);

printf(“Count %d\n”,count(first));
printf(“Sum %d\n”,sum(first);

return 0;
}

You might also like