查找链表中所有节点的平均值的程序
给定一个单链表。任务是找到给定单链表的所有节点的平均值。
例子:
Input: 7->6->8->4->1
Output: 26
Average of nodes:
(7 + 6 + 8 + 4 + 1 ) / 5 = 5.2
Input: 1->7->3->9->11->5
Output: 6
迭代解决方案:
- 用链表的头部初始化一个指针 ptr,用 0 初始化一个 sum 变量。
- 使用循环开始遍历链表,直到遍历完所有节点。
- 将当前节点的值添加到总和中,即 sum += ptr -> data 。
- 增加指向链表下一个节点的指针,即 ptr = ptr ->next 。
- 将总和除以节点总数并返回平均值。
下面是上述方法的实现:
C++
// C++ implementation to find the average of
// nodes of the Linked List
#include
using namespace std;
/* A Linked list node */
struct Node {
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node;
/* put in the data */
new_node->data = new_data;
/* link the old list to the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// Function to iteratively find the avg of
// nodes of the given linked list
float avgOfNodes(struct Node* head)
{
// if head = NULL
if (!head)
return -1;
int count = 0; // Initialize count
int sum = 0;
float avg = 0.0;
struct Node* current = head; // Initialize current
while (current != NULL) {
count++;
sum += current->data;
current = current->next;
}
// calculate average
avg = (double)sum / count;
return avg;
}
// Driver Code
int main()
{
struct Node* head = NULL;
// create linked list 7->6->8->4->1
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Average of nodes = " << avgOfNodes(head);
return 0;
}
Java
// Java implementation to find the average of
// nodes of the Linked List
class GFG
{
/* A Linked list node */
static class Node
{
int data;
Node next;
};
// function to insert a node at the
// beginning of the linked list
static Node push(Node head_ref, int new_data)
{
/* allocate node */
Node new_node = new Node();
/* put in the data */
new_node.data = new_data;
/* link the old list to the new node */
new_node.next = (head_ref);
/* move the head to point to the new node */
(head_ref) = new_node;
return head_ref;
}
// Function to iteratively find the avg of
// nodes of the given linked list
static double avgOfNodes(Node head)
{
// if head = null
if (head == null)
return -1;
int count = 0; // Initialize count
int sum = 0;
double avg = 0.0;
Node current = head; // Initialize current
while (current != null)
{
count++;
sum += current.data;
current = current.next;
}
// calculate average
avg = (double)sum / count;
return avg;
}
// Driver Code
public static void main(String args[])
{
Node head = null;
// create linked list 7.6.8.4.1
head=push(head, 7);
head=push(head, 6);
head=push(head, 8);
head=push(head, 4);
head=push(head, 1);
System.out.println("Average of nodes = " + avgOfNodes(head));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation to find the average of
# nodes of the Linked List
class newNode:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.next = None
# function to insert a node at the
# beginning of the linked list
def push(node,data):
''' allocate node '''
if (node == None):
return (newNode(data))
else:
node.next = push(node.next, data)
return node
# Function to iteratively find the avg of
# nodes of the given linked list
def avgOfNodes(head):
# if head = NULL
if (head == None):
return -1
count = 0 # Initialize count
sum = 0
avg = 0.0
while (head != None):
count += 1
sum += head.data
head = head.next
# calculate average
avg = sum / count
return avg
# Driver Code
# create linked list 7.6.8.4.1
head = newNode(7)
push(head, 6)
push(head, 8)
push(head, 4)
push(head, 1)
print("Average of nodes = ",avgOfNodes(head))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# implementation to find the average
// of nodes of the Linked List
using System;
class GFG
{
/* A Linked list node */
public class Node
{
public int data;
public Node next;
};
// function to insert a node at the
// beginning of the linked list
static Node push(Node head_ref,
int new_data)
{
/* allocate node */
Node new_node = new Node();
/* put in the data */
new_node.data = new_data;
/* link the old list to the new node */
new_node.next = (head_ref);
/* move the head to point to the new node */
(head_ref) = new_node;
return head_ref;
}
// Function to iteratively find the avg of
// nodes of the given linked list
static double avgOfNodes(Node head)
{
// if head = null
if (head == null)
return -1;
int count = 0; // Initialize count
int sum = 0;
double avg = 0.0;
Node current = head; // Initialize current
while (current != null)
{
count++;
sum += current.data;
current = current.next;
}
// calculate average
avg = (double)sum / count;
return avg;
}
// Driver Code
public static void Main(String []args)
{
Node head = null;
// create linked list 7.6.8.4.1
head=push(head, 7);
head=push(head, 6);
head=push(head, 8);
head=push(head, 4);
head=push(head, 1);
Console.WriteLine("Average of nodes = " +
avgOfNodes(head));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
Average of nodes = 5.2
时间复杂度: O(n)
其中 n 等于节点数。