Question: Write a Program to find average of all nodes in a Linked List Given a singly linked list. The task is to find the
Write a Program to find average of all nodes in a Linked List Given a singly linked list. The task is to find the average of all nodes in a linked list is shown as following example: Input: 7->6->8->4->1 Output: 26 Average of nodes: (7+6+8+4+1)/ 5 = 5.2 The algorithm of finding average in a linked list are the following steps: 1. Initialize a pointer ptr with the head of the linked list and a sum variable with 0. 2. Start traversing the linked list using a loop until all the nodes get traversed. 3. Add the value of current node to the sum i.e. sum += ptr -> data. 4. Increment the pointer to the next node of linked list i.e. ptr = ptr ->next. 5. Divide sum by total number of node and Return the average.
Step by Step Solution
There are 3 Steps involved in it
Title Calculate the average of all nodes in a singly linked list Program in C inclu... View full answer
Get step-by-step solutions from verified subject matter experts
