Sum of smaller elements of nodes in a linked list

Suppose that linked list combination of two data element which are contain in integer value. Sum the all smallest nodes value of this linked list.

We can solve this problem by iterator linked list node from head to last node and compare node value. Before write an algorithm to solve this problem consider following test cases

1) There is possible to linked list empty. In this situation display a message(linked list is empty).

2) There is possible to node are contain both same value. then add any one value of the resultant.

Suppose we are inserted the following ((3 9),(11 4),(3 3),(6 1),(2 9),(4 7) ) node in a sequence.

Sum of smaller elements

Here given code implementation process.

//C Program
//Sum of smallest element of linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
  int data1,data2;
  struct Node*next;
};
//function prototype
void insert(struct Node**,int,int);
void display(struct Node*);

//insert Node element
void insert(struct Node**head,int v1,int v2){
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL){
    printf("Memory overflow\n");
  }else{
    //set data values
    node->data1=v1;
    node->data2=v2;

    node->next=NULL;
    if(*head==NULL){
      *head=node;
    }else{
      struct Node*temp=*head;
      //find last node
      while(temp->next!=NULL){
        temp=temp->next;
      }
      //add node at last possition
      temp->next=node;
    }
  }
}
//Display element of Node
void display(struct Node*temp){
  
  if(temp==NULL){
    printf("Empty linked list");
  }
  while(temp!=NULL){
    printf("(%d %d)->",temp->data1,temp->data2);
    temp=temp->next;
  }
  printf("NULL");
}
//add all smallest element
void min_sum(struct Node*temp){
  if(temp==NULL){
    printf("Empty linked list");
  }
  int result=0;
  while(temp!=NULL){

    if(temp->data1<temp->data2){
      //when node data1 is smaller
       result+=temp->data1;
    }else{
      //when  data2 is  less than and equal to  data1
      result+=temp->data2;
    }
    temp=temp->next;
  }
  printf("\nSum of Smaller Elements: %d\n",result );
}
int main(){
  //Create head pointer
  struct Node*head=NULL;
  //Insert element of linked list

  insert(&head,3,9);
  insert(&head,11,4);
  insert(&head,3,3);
  insert(&head,6,1);
  insert(&head,2,9);
  insert(&head,4,7);
  //Display all node
  display(head);
  min_sum(head);
  return 0;
}

Output

(3 9)->(11 4)->(3 3)->(6 1)->(2 9)->(4 7)->NULL
Sum of Smaller Elements: 17
//C++ Program 
//Sum of smallest element of linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
  int data1,data2;
  struct Node*next;
};
class LinkedList{
  Node*head;//head node
  public:
    LinkedList();
    void insert(int,int);
    void display();
    void min_sum();
};
LinkedList::LinkedList(){
  head=NULL;
}
//insert node at end of linked list
void LinkedList::insert(int v1,int v2){
    //Create dynamic node
  struct Node*node=new Node;
  if(node==NULL){
    cout<<"Memory overflow\n";
  }else{
    node->data1=v1;
    node->data2=v2;
    node->next=NULL;
    if(head==NULL){
      //base condition
      head=node;
    }else{
      Node*temp=head;
      while(temp->next!=NULL){
        temp=temp->next;
      }
      //add newly node at last
      temp->next=node;
    }
  }
}
//display all node value in linked list
void LinkedList:: display(){
  if(head==NULL){
    cout<<"Empty linked list";
  }
  else{
    Node*temp=head;
    cout<<"Linked List : ";
    while(temp!=NULL){
      //print node value
      cout<<"("<<temp->data1<<","<<temp->data2<<")->";
      temp=temp->next;
    }
    cout<<"NULL";
  }

}

//add all smallest element
void LinkedList:: min_sum(){
  struct Node*temp=head;
  if(temp==NULL){
    cout<<"Empty linked list"<<endl;
  }
  int result=0;
  while(temp!=NULL){

    if(temp->data1<temp->data2){
      //when node data1 is smaller
       result+=temp->data1;
    }else{
      //when  data2 is  less than and equal to  data1
      result+=temp->data2;
    }
    temp=temp->next;
  }
  cout<<"\nSum of Smaller Elements: "<<result <<endl;
}
int main(){
 
  //create object
  LinkedList obj;
  //insert element of linked list


  obj.insert(3,9);
  obj.insert(11,4);
  obj.insert(3,3);
  obj.insert(6,1);
  obj.insert(2,9);
  obj.insert(4,7);

  //display all node
  obj.display();
  obj.min_sum();
  return 0;
}

Output

Linked List : (3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->NULL
Sum of Smaller Elements: 17
//Java Program
//Sum of smallest element of linked list
public class LinkedList{

  public class Node{
    int data1,data2;
    Node next;
  }
  public Node head;
  //Class constructors
  LinkedList(){
    head=null;
  } 
  //insert element
  public void insert(int v1,int v2){
    //Create  node
    Node node=new Node();
    node.data1=v1;
    node.data2=v2;
    node.next=null;
    if(head==null) head=node;
    else{
      Node temp=head;
      //find last node
      while(temp.next!=null){
        temp=temp.next;
      }
      temp.next=node;
    }
    
  }
  public void minSum(){
    Node temp=head;
    if(temp==null){
     System.out.print("Empty Linked List");
    }
    int result=0;
    while(temp!=null){

      if(temp.data1<temp.data2){
        //when node data1 is smaller
         result+=temp.data1;
      }else{
        //when  data2 is  less than and equal to  data1
        result+=temp.data2;
      }
      temp=temp.next;
    }
    System.out.println("Sum of Smaller Elements: "+result);
    
  }

  //Display all Linked List elements
  public void display(){
    if(head!=null){
      System.out.print("Linked List Element :");
      Node temp=head;
      while(temp!=null){
        System.out.print("("+temp.data1+","+ temp.data2 +").");
        temp=temp.next;
      }
    }else{
      System.out.println("Empty Linked list"); 
    }
  }

  static public void main(String[] args) {
    
    LinkedList obj=new LinkedList();
    //insert element
  obj.insert(3,9);
  obj.insert(11,4);
  obj.insert(3,3);
  obj.insert(6,1);
  obj.insert(2,9);
  obj.insert(4,7);

  //display all node
  obj.display();
  obj.minSum();
  }
}

Output

Linked List : (3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->NULL
Sum of Smaller Elements: 17
#Python Program
#Sum of smallest element of linked list
class Node:
    def __init__(self,data1,data2):
        self.data1=data1
        self.data2=data2
        self.next=None

#Create Class Linked    
class Linked:
    def __init__(self):
        #Assign default value
        self.head=None

    #insert new node to linked list  
    def insert(self,v1,v2):
        node=Node(v1,v2)
        node.next=None
        if self.head==None:
            self.head=node
        else:
            temp=self.head
            while temp.next!=None:
                temp=temp.next
            #add node    
            temp.next=node

    def display(self):
        if(self.head==None):
            print("Empty Linked List")
            return

        temp=self.head
        print("Linked List Elements : ")
        while(temp!=None):
          print("(",temp.data1,",",temp.data2,")->",end="")
          temp=temp.next
        print("None")
        
    #Sum of all smallest element
    def minSum(self):
      temp=self.head
      if(temp==None):
        print("Empty Linked List")
     
      result=0
      while(temp!=None):

        if(temp.data1<temp.data2):
          #when node data1 is smaller
          result+=temp.data1
        else:
          #when  data2 is  less than and equal to  data1
          result+=temp.data2

        temp=temp.next
     
      print("Sum of Smaller Elements: ",result)
      
   

def main():
    #Create Object of class Linked
    obj=Linked()
    obj.insert(3,9)
    obj.insert(11,4)
    obj.insert(3,3)
    obj.insert(6,1)
    obj.insert(2,9)
    obj.insert(4,7)

    #display all node
    obj.display()
    obj.minSum()
if __name__=="__main__":
    main()

Output

Linked List Elements : 
( 3 , 9 )->( 11 , 4 )->( 3 , 3 )->( 6 , 1 )->( 2 , 9 )->( 4 , 7 )->None
Sum of Smaller Elements:  17
//C# Program
//Sum of smallest element of linked list
using System;
//node class
public class Node{
  public  int data1,data2;
  public  Node next;
}
class Program
{
  Node head;
  public Program(){
    head=null;
  }
  //insert node of linked list
  public void insert(int v1,int v2){
    Node newNode=new Node();
    newNode.data1=v1;
    newNode.data2=v2;
    newNode.next=null;
    if(head==null) head=newNode;
    else{
      Node temp=head;
      //get last node
      while(temp.next!=null){
        temp=temp.next;
      }
      //add new node
      temp.next=newNode;
    }
  }
  //display linked list nodes value
  public void display(){
    if(head==null){
      Console.Write("Empty List");
    }else{
      Node temp=head;
      while(temp!=null){
        Console.Write("({0},{1})->",temp.data1,temp.data2);
        temp=temp.next;
      }
      Console.Write("null");
    }
  }
  public void minSum(){
    Node temp=head;
    if(temp==null){
      Console.Write("Empty Linked List");
    }
    int result=0;
    while(temp!=null){

      if(temp.data1<temp.data2){
        //when node data1 is smaller
        result+=temp.data1;
      }else{
        //when  data2 is  less than and equal to  data1
        result+=temp.data2;
      }
      temp=temp.next;
    }
    Console.Write("\nSum of Smaller Elements: {0}",result);

  }
  static void Main()
  {
    Program obj=new Program();
    //insert element
    obj.insert(3,9);
    obj.insert(11,4);
    obj.insert(3,3);
    obj.insert(6,1);
    obj.insert(2,9);
    obj.insert(4,7);

    //display all node
    obj.display();
    obj.minSum();
  }
}

Output

(3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->null
Sum of Smaller Elements: 17


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.

New Comment







© 2021, kalkicode.com, All rights reserved