# Find average of all nodes in a linked list

Here given code implementation process.

``````// C Program
// Find average of all nodes in a linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
{
printf(" →");
}
printf(" %d", temp->data);
//visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}
//Calculate average of linked list nodes
{
{
}
else
{
// 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 : %lf \n", (sum / size));
}
else
{
printf(" Node Average : 0\n");
}
}
}
int main()
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
return 0;
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.500000``````
``````// Java Program
// Find average of all nodes in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
//Calculate average of linked list nodes
public void find_average()
{
{
}
else
{
this.display();
// 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)
{
System.out.print(" Node Average : " + (sum / size) + " \n");
}
else
{
System.out.print(" Node Average : 0\n");
}
}
}
public static void main(String[] args)
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
}
}``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Find average of all nodes in a linked list

class Node
{
public: int data;
Node *next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node *tail;
//Class constructor
{
this->tail = NULL;
}
//insert node at last of linke list
{
//Create a node
Node *node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "Linked List : ";
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
//visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
//Calculate average of linked list nodes
void find_average()
{
{
}
else
{
this->display();
// 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)
{
cout << " Node Average : " << (sum / size) << " \n";
}
else
{
cout << " Node Average : 0\n";
}
}
}
};
int main()
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
return 0;
}``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````//Include namespace system
using System;
// C# Program
// Find average of all nodes in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
//Calculate average of linked list nodes
public void find_average()
{
{
}
else
{
this.display();
// 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)
{
Console.Write(" Node Average : " + (sum / size) + " \n");
}
else
{
Console.Write(" Node Average : 0\n");
}
}
}
public static void Main(String[] args)
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
}
}``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````<?php
// Php Program
// Find average of all nodes in a linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructor
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
while (\$temp != null)
{
{
echo " →";
}
echo " ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo " → NULL\n";
}
//Calculate average of linked list nodes
public	function find_average()
{
{
}
else
{
\$this->display();
// Define useful resultant variable
\$sum = 0;
\$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)
{
echo " Node Average : ". ((\$sum / \$size)) ." \n";
}
else
{
echo " Node Average : 0\n";
}
}
}
}

function main()
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
\$obj->find_average();
}
main();``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````// Node Js Program
// Find average of all nodes in a linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructor
constructor()
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
//Calculate average of linked list nodes
find_average()
{
{
}
else
{
this.display();
// Define useful resultant variable
var sum = 0;
var 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)
{
process.stdout.write(" Node Average : " + ((sum / size)) + " \n");
}
else
{
process.stdout.write(" Node Average : 0\n");
}
}
}
}

function main()
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
}
main();``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````#  Python 3 Program
#  Find average of all nodes in a linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructor
def __init__(self) :
self.tail = None

# insert node at last of linke list
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\n Empty linked list\n", end = "")
return

print("Linked List : ", end = "")
while (temp != None) :
print(" →", end = "")

print(" ", temp.data, end = "")
# visit to next node
temp = temp.next

print(" → NULL\n", end = "")

# Calculate average of linked list nodes
def find_average(self) :
print("\n Empty linked List", end = "")
else :
self.display()
#  Define useful resultant variable
sum = 0
size = 0
#  Get first node of linked list
while (auxiliary != None) :
# Node counter
size += 1
# Calculate the sum of node value
sum += auxiliary.data
#  Visit to next node
auxiliary = auxiliary.next

# Display result
if (sum != 0) :
print(" Node Average : ", ((sum / size)) ," \n", end = "")
else :
print(" Node Average : 0\n", end = "")

def main() :
#  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average()

if __name__ == "__main__": main()``````

#### Output

`````` Linked Linked
Linked List :   2 →  9 →  1 →  5 →  5 →  4 →  3 →  7 → NULL
Node Average :  4.5``````
``````#  Ruby Program
#  Find average of all nodes in a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)
# Set node value
self.data = data
self.next = nil
end
end

# Class constructor
def initialize()
self.tail = nil
end
# insert node at last of linke list

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

print(" →")
end
print(" ", temp.data)
# visit to next node
temp = temp.next
end
print(" → NULL\n")
end
# Calculate average of linked list nodes
def find_average()

else

self.display()
#  Define useful resultant variable
sum = 0.0
size = 0
#  Get first node of linked list
while (auxiliary != nil)

# Node counter
size += 1
# Calculate the sum of node value
sum += auxiliary.data
#  Visit to next node
auxiliary = auxiliary.next
end
# Display result
if (sum != 0)

print(" Node Average : ", (sum / size) ," \n")
else

print(" Node Average : 0\n")
end
end
end
end
def main()

#  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average()
end
main()``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5
``````
``````// Scala Program
// Find average of all nodes in a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructor
def this()
{
this(null, null);
}
//insert node at last of linke list
def add_node(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
//visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
//Calculate average of linked list nodes
def find_average(): Unit = {
{
}
else
{
this.display();
// Define useful resultant variable
var sum: Double = 0;
var size: Int = 0;
// Get first node of linked list
while (auxiliary != null)
{
//Node counter
size += 1;
//Calculate the sum of node value
sum += auxiliary.data;
// Visit to next node
auxiliary = auxiliary.next;
}
//Display result
if (sum != 0)
{
print(" Node Average : " + (sum / size) + " \n");
}
else
{
print(" Node Average : 0\n");
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
}
}``````

#### Output

`````` Linked Linked
Linked List :  2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
Node Average : 4.5``````
``````// Swift 4 Program
// Find average of all nodes in a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructor
init()
{
self.tail = nil;
}
//insert node at last of linke list
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
print("\n Empty linked list\n", terminator: "");
return;
}
print("Linked List : ", terminator: "");
while (temp != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print(" → NULL\n", terminator: "");
}
//Calculate average of linked list nodes
func find_average()
{
{
print("\n Empty linked List", terminator: "");
}
else
{
self.display();
// Define useful resultant variable
var sum: Double = 0;
var size: Int = 0;
// Get first node of linked list
while (auxiliary != nil)
{
//Node counter
size += 1;
//Calculate the sum of node value
sum += Double(auxiliary!.data);
// Visit to next node
auxiliary = auxiliary!.next;
}
//Display result
if (sum != 0)
{
print(" Node Average : ", (sum / Double(size)) ," \n", terminator: "");
}
else
{
print(" Node Average : 0\n", terminator: "");
}
}
}
}
func main()
{
// 2 → 9 → 1 → 5 → 5 → 4 → 3 → 7 → NULL
obj.find_average();
}
main();``````

#### Output

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

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.