Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K

Given a singly circular linked list. The task is to find the sum and product of nodes which are divisible by K of the given linked list.

Examples:

Input : List = 5->6->7->8->9->10->11->11
             K = 11
Output : Sum = 22, Product = 121

Input : List = 15->7->3->9->11->5
             K = 5
Output : Product = 75, Sum = 20
Sum And Product of Singly Circular Linked List Node

Sum And Product of Singly Circular Linked List Node

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 is divisible by given key.
    • 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to calculate sum and product from
// singly circular linked list nodes
// which are divisible by given key
  
#include <bits/stdc++.h>
using namespace std;
  
// Circular list node
struct Node {
    int data;
    struct Node* next;
};
  
// 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("\nDisplay List is empty\n");
        return;
    }
    // traverse first to last node
    else {
        do {
            // check if current node's data is
            // divisible by key
            if ((current->data) % key == 0) {
  
                // 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("\nDisplay List 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 firt 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, 5);
    InsertNode(&head, 6);
    InsertNode(&head, 7);
    InsertNode(&head, 8);
    InsertNode(&head, 9);
    InsertNode(&head, 10);
    InsertNode(&head, 11);
    InsertNode(&head, 11);
  
    cout << "Initial List: ";
    displayList(head);
  
    cout << endl;
    sumProduct(head, 11);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate sum and product from 
// singly circular linked list nodes 
// which are divisible by given key 
import java.util.*;
class Solution
{
    
// Circular list node 
static class Node { 
    int data; 
    Node next; 
    
// Function to calculate sum and product 
static void sumProduct( Node head, int key) 
         Node current = head; 
    
    int sum = 0, product = 1
    
    // if list is empty simply show message 
    if (head == null) { 
        System.out.print("\nDisplay List is empty\n"); 
        return
    
    // traverse first to last node 
    else
        do
            // check if current node's data is 
            // divisible by key 
            if ((current.data) % key == 0) { 
    
                // Calculate sum 
                sum += current.data; 
    
                // Calculate product 
                product = current.data; 
            
    
            current = current.next; 
        } while (current != head); 
    
    
    System.out.print( "Sum = " + sum + ", Product = " + product); 
    
// Function print the list 
static void displayList( Node head) 
     Node current = head; 
    
    // if list is empty simply show message 
    if (head == null) { 
        System.out.print("\nDisplay List is empty\n"); 
        return
    
    // traverse first to last node 
    else
        do
            System.out.print( current.data+" "); 
            current = current.next; 
        } while (current != head); 
    
    
// Function to insert a node at the end of 
// a Circular linked list 
static Node InsertNode( Node head, int data) 
     Node current = head; 
    // Create a new node 
     Node newNode = new Node(); 
    
    // check node is created or not 
    if (newNode==null) { 
       System.out.print("\nMemory Error\n"); 
        return head; 
    
    
    // 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 head; 
    
    // if list have already some node 
    else
    
        // move firt 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; 
    
    return head;
    
// Driver Code 
public static void main(String args[])
     Node head=null
    head =InsertNode(head, 5); 
    head =InsertNode(head, 6); 
    head =InsertNode(head, 7); 
    head =InsertNode(head, 8); 
    head =InsertNode(head, 9); 
    head =InsertNode(head, 10); 
    head =InsertNode(head, 11); 
    head =InsertNode(head, 11); 
    
    System.out.print( "Initial List: "); 
    displayList(head); 
    
    System.out.println(); 
    sumProduct(head, 11); 
     
}
}
//contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate sum and product from 
// singly circular linked list nodes 
// which are divisible by given key 
using System;
      
class GFG
{
  
    // Circular list node 
    class Node 
    
        public int data; 
        public Node next; 
    
  
    // Function to calculate sum and product 
    static void sumProduct( Node head, int key) 
    
            Node current = head; 
  
        int sum = 0, product = 1; 
  
        // if list is empty simply show message 
        if (head == null
        
            Console.Write("\nDisplay List is empty\n"); 
            return
        
          
        // traverse first to last node 
        else
        
            do
            
                // check if current node's data is 
                // divisible by key 
                if ((current.data) % key == 0) 
                
  
                    // Calculate sum 
                    sum += current.data; 
  
                    // Calculate product 
                    product = current.data; 
                
  
                current = current.next; 
            } while (current != head); 
        
  
        Console.Write( "Sum = " + sum + ", Product = " + product); 
    
  
    // Function print the list 
    static void displayList( Node head) 
    
        Node current = head; 
  
        // if list is empty simply show message 
        if (head == null
        
            Console.Write("\nDisplay List is empty\n"); 
            return
        
        // traverse first to last node 
        else 
        
            do 
            
                Console.Write( current.data+" "); 
                current = current.next; 
            } while (current != head); 
        
    
  
    // Function to insert a node at the end of 
    // a Circular linked list 
    static Node InsertNode( Node head, int data) 
    
        Node current = head; 
        // Create a new node 
        Node newNode = new Node(); 
  
        // check node is created or not 
        if (newNode==null
        
            Console.Write("\nMemory Error\n"); 
            return head; 
        
  
        // 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 head; 
        
        // if list have already some node 
        else
        
  
            // move firt 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; 
        
        return head;
    
  
    // Driver Code 
    public static void Main()
    
        Node head=null
        head =InsertNode(head, 5); 
        head =InsertNode(head, 6); 
        head =InsertNode(head, 7); 
        head =InsertNode(head, 8); 
        head =InsertNode(head, 9); 
        head =InsertNode(head, 10); 
        head =InsertNode(head, 11); 
        head =InsertNode(head, 11); 
  
        Console.Write( "Initial List: "); 
        displayList(head); 
  
        Console.WriteLine(); 
        sumProduct(head, 11); 
    }
}
  
// This code has been contributed 
// by PrinciRaj1992 

chevron_right


Output:

Initial List: 5 6 7 8 9 10 11 11 
Sum = 22, Product = 121


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

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.



Improved By : andrew1234, princiraj1992