-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLL_sumAverage.c
47 lines (43 loc) · 914 Bytes
/
LL_sumAverage.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct llist
{
int data;
struct llist *next;
};
typedef struct llist node;
void main()
{
node *ptr, *head, *new1;
int count = 0, sum = 0;
char op;
head = (node*)malloc(sizeof(node));
ptr = head;
do{
printf("Enter the data\n");
scanf("%d",&ptr->data);
printf("Continue LinkedList? Y or N\n");
op = getch();
if (op == 'y')
{
new1 = (node*)malloc(sizeof(node));
ptr->next = new1;
ptr = new1;
}
else
{
ptr->next = NULL;
}
}while(op != 'n');
ptr = head;
while (ptr!= NULL)
{
sum = sum + ptr->data;
ptr = ptr->next;
count++;
}
system("cls");
printf("Sum of %d nodes: %d", count, sum);
printf("\nAverage of %d nodes: %d", count, sum/count);
}