# Find average of all nodes in a linked list in c

``````// Include header file
#include <stdio.h>
#include <stdlib.h>
/*
C program for
Find average of all nodes in a linked list
*/
{
// Define useful field of LinkNode
int data;

{
// Create dynamic memory of LinkNode
if (ref == NULL)
{
// Failed to create memory
return NULL;
}
ref->data = data;
ref->next = NULL;
return ref;
}
typedef struct SingleLL
{
// Define useful field of SingleLL
}SingleLL;

SingleLL * getSingleLL()
{
// Create dynamic memory of SingleLL
SingleLL * ref = (SingleLL * ) malloc(sizeof(SingleLL));
if (ref == NULL)
{
// Failed to create memory
return NULL;
}
// Set initial value
ref->tail = NULL;
return ref;
}
void insert(SingleLL * ref, int data)
{
{
}
else
{
// Add node at the end position
ref->tail->next = node;
}
// New last node
ref->tail = node;
}
void display(SingleLL * ref)
{
{
return;
}
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
// Calculate average of linked list nodes
void findAverage(SingleLL * ref)
{
{
}
else
{
display(ref);
// Define useful resultant variable
double sum = 0;
int size = 0;
// Get first node of linked list
while (auxiliary != NULL)
{
// Node counter
size++;
// Calculate the sum of node value
sum += auxiliary->data;
// Visit to next node
auxiliary = auxiliary->next;
}
// Display result
if (sum != 0)
{
printf(" Node Average : %0.2f", (sum / size));
}
else
{
printf(" Node Average : 0");
}
}
}
int main()
{
SingleLL * sll = getSingleLL();
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
insert(sll, 2);
insert(sll, 9);
insert(sll, 1);
insert(sll, 5);
insert(sll, 5);
insert(sll, 4);
insert(sll, 3);
insert(sll, 7);
// Test
findAverage(sll);
}``````

Output

`````` Linked Linked  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.50``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.