-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumofelementsll.c
65 lines (60 loc) · 1.15 KB
/
sumofelementsll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Sum of all elements in a sll
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} *root=NULL;
//function to append the elements into the list
void append(int ar[], int n)
{
struct node *last,*p;
root=(struct node *)malloc(sizeof(struct node));
root->data=ar[0];
root->next=NULL;
last=root;
for(int i=1;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=ar[i];
p->next=NULL;
last->next=p;
last=p;
}
}
//sunction to display the list
void display()
{
printf("\nThe array into SLL: ");
struct node *p;
p=root;
while(p) //=> while(p!=NULL)
{
printf("%d -> ",p->data);
p=p->next;
}
}
//function to all the elements into the list
int addition()
{
int sum=0;
struct node *p;
p=root;
while(p)
{
sum=sum+p->data;
p=p->next;
}
return sum;
}
int main()
{
int len=6,sum;
int ar[]={1,3,5,7,9,11};
append(ar,len);
display();
sum=addition();
printf("\nThe sum: %d", sum);
return 0;
}