Sum and Product of nodes with value as even digit sum in Circular Linked List


Given a circular singly linked list containing N nodes, The task is to find the sum and product of all the nodes from the list whose data value has an even digit sum.

Examples:

Input: List = 15 -> 16 -> 8 -> 6 -> 13
Output: Sum = 42, Product = 9360
Explanation:
The circular linked list contains:
15 -> 1 + 5 = 6
16 -> 1 + 6 = 7
8 -> 8
6 -> 6
13 -> 1 + 3 = 4
The list contains 4 Even Digit Sum data values 15, 8, 6 and 13.
Sum = 15 + 8 + 6 + 13 = 42
Product = 15 * 8 * 6 * 13 = 9360

Input: List = 5 -> 3 -> 4 -> 2 -> 9
Output: Sum = 6, Product = 8
Explanation:
The list contains 2 Even Digit Sum data values 4 and 2.
Sum = 4 + 2 = 6
Product = 4 * 2 = 8

Approach:



  1. Initialize a pointer current with the head of the circular linked list and a sum variable sum with 0 and a product variable product with 1.
  2. Start traversing the linked list using a do-while loop until all the nodes get traversed.
  3. If current node data value has an even digit sum.
    • Add the value of current node to the sum i.e. sum = sum + current -> data.
    • Multiply the value of current node to the product i.e. product = product * current -> data.
    • Increment the pointer to the next node of linked list i.e. temp = temp -> next.
  4. Print the sum and product.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the sum and
// product of all of the Even Digit sum nodes
// in a circularly linked list
#include <bits/stdc++.h>
using namespace std;
  
// Circular list node
struct Node {
    int data;
    struct Node* next;
};
  
// Function to find the digit sum
// for a number
int digitSum(int num)
{
    int sum = 0;
    while (num) {
        sum += (num % 10);
        num /= 10;
    }
  
    return sum;
}
  
// Function to calculate sum and
// product
void SumProduct(struct Node* head,
                int key)
{
    struct Node* current = head;
  
    int sum = 0, product = 1;
  
    // If list is empty simply 
    // show message
    if (head == NULL) {
        printf("\nList is empty\n");
        return;
    }
    // Traverse first to last node
    else {
        do {
            // Check if current node's
            // data has an even digit sum
            if (!(digitSum(current->data) 
                  & 1))
            {
  
                // Calculate sum
                sum += current->data;
  
                // Calculate product
                product *= current->data;
            }
  
            current = current->next;
        } while (current != head);
    }
  
    cout << "Sum = " << sum 
        << ", Product = " << product;
}
  
// Function print the list
void DisplayList(struct Node* head)
{
    struct Node* current = head;
  
    // If list is empty simply
    // show message
    if (head == NULL)
    {
        printf("\nList is empty\n");
        return;
    }
    // Traverse first to last node
    else {
        do {
            printf("%d ", current->data);
            current = current->next;
        } while (current != head);
    }
}
  
// Function to insert a node at the 
// end of a Circular linked list
void InsertNode(struct Node** head,
                int data)
{
    struct Node* current = *head;
      
    // Create a new node
    struct Node* newNode = new Node;
  
    // Check node is created or not
    if (!newNode) {
        printf("\nMemory Error\n");
        return;
    }
  
    // Insert data into newly
    // created node
    newNode->data = data;
  
    // Check list is empty
    // if not have any node then
    // make first node it
    if (*head == NULL) {
        newNode->next = newNode;
        *head = newNode;
        return;
    }
    // If list have already some node
    else {
  
        // Move first node to last 
        // node
        while (current->next != *head)
        {
            current = current->next;
        }
  
        // Put first or head node 
        // address in new node link
        newNode->next = *head;
  
        // Put new node address into 
        // last node link(next)
        current->next = newNode;
    }
}
  
// Driver Code
int main()
{
    struct Node* head = NULL;
    InsertNode(&head, 13);
    InsertNode(&head, 6);
    InsertNode(&head, 8);
    InsertNode(&head, 15);
    InsertNode(&head, 16);
  
    cout << "Initial List: ";
    DisplayList(head);
  
    cout << endl;
    SumProduct(head, 11);
  
    return 0;
}

chevron_right


Output:

Initial List: 13 6 8 15 16 
Sum = 42, Product = 9360

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.