-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS(5,3).cpp
56 lines (44 loc) · 1.17 KB
/
DS(5,3).cpp
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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node*traversal(struct node*ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
return 0;
}
int main(){
struct node*head = (struct node*)malloc(sizeof(struct node));
struct node*one = (struct node*)malloc(sizeof(struct node));
one->data = 10;
struct node*second=(struct node*)malloc(sizeof(struct node));
second->data = 30;
struct node*third=(struct node*)malloc(sizeof(struct node));
third->data = 20;
struct node*fourth=(struct node*)malloc(sizeof(struct node));
fourth->data = 40;
struct node*fifth=(struct node*)malloc(sizeof(struct node));
fifth->data = 50;
head = one;
one->next = second;
second->next = third;
third->next = fourth;
fourth->next = fifth;
fifth->next = NULL;
printf("Original List: ");
traversal(head);
int sum=0;
struct node* ptr = head;
while (ptr != NULL) {
sum += ptr->data;
ptr = ptr->next;
}
printf("sum of all elements in the list is:%d",sum);
return 0;
}