-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingly_Linked_List -Program_3.c
72 lines (64 loc) · 1.5 KB
/
Singly_Linked_List -Program_3.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
66
67
68
69
70
71
72
// A lady gardener collects roses from garden and puts them into bags. After filling bag1, she takes bag2 and fills with roses and so on. Later she counts the roses in each bag. Given the count of roses in each bag as data part of a linked list, print the total count of roses found in all bags.
// Example
// If the roses in each bags has the following counts- 100->115->105 the print the output as 320
// Input Format
// The first line contains an integer n, the number of bags.
// Each of the next n, lines contains an integer, that represents the count of roses in each bag
// Sample Input
// 3
// 100
// 115
// 105
// Sample Output
// 320
#include<stdio.h>
#include<stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
node *position;
int sum=0;
void insert(node *list,int e)
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
position=newnode;
}
else
{
newnode->next=NULL;
position->next=newnode;
position=newnode;
}
}
int count(node *list)
{
node *position=list;
while(position->next!=NULL)
{
sum+=position->next->element;
position=position->next;
}
return sum;
}
int main()
{
int n,m;
node *list=malloc(sizeof(node));
list->next=NULL;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&m);
insert(list,m);
}
printf("%d",count(list));
return 0;
}