Sum of nodes which are greater than upcoming nodes of linked list

Write an algorithm which are fulfill given of following test cases.

If linked list empty or only one node then result is zero. In a situation only single node of linked list then this case there are no upcoming node.

Compare linked list nodes from first node to last node if current node is greater than next node get the current element value and add to resultant.

Final test condition, If more then two nodes of linked list then check that last node value is greater than of first node in linked list. If greater, then in this situation add last node value in resultant.

Suppose we are inserted the following (4,2,9,3,7,10,5) node in a sequence.

Sum of greater upcoming nodes

Here given code implementation process.

//C Program 
//Sum of nodes which are greater than upcoming nodes of linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

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

//insert Node element
void insert(struct Node**head,int value){
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL){
    printf("Memory overflow\n");
  }else{
    node->data=value;
    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  ",temp->data);
    temp=temp->next;
  }
}
int sum_big(struct Node*head){

  int result=0;
  struct Node*temp=head;
  if(temp==NULL){
    printf("Empty linked list");
  }else{
      while(temp!=NULL && temp->next!=NULL){

        if(temp->data > temp->next->data){
          //when upcoming node is less than current nodes
          result+=temp->data;
        }
        temp=temp->next;
      }
      //compare tail node value is greater than head node
      if(temp->data > head->data){
        result+=temp->data;
      }

  }

  return result;

}
int main(){
  //create node pointer
  struct Node*head=NULL;
  //insert element of linked list

  insert(&head,4);
  insert(&head,2);
  insert(&head,9);
  insert(&head,3);
  insert(&head,7);
  insert(&head,10);
  insert(&head,5);

  //display all node
  display(head);
  printf("\nResult : %d\n",sum_big(head));
  return 0;
}

Output

4  2  9  3  7  10  5  
Result : 28
//C++ Program 
//Sum of nodes which are greater than upcoming nodes of linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
  int data;
  struct Node*next;
};
class LinkedList{
  Node*head;//head node
  public:
    LinkedList();
    void insert(int);
    void display();
    int sum_big();
};
LinkedList::LinkedList(){
  head=NULL;
}
//insert node at end of linked list
void LinkedList::insert(int value){
    //Create dynamic node
  struct Node*node=new Node;
  if(node==NULL){
    cout<<"Memory overflow\n";
  }else{
    node->data=value;
    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->data<<" ";
      temp=temp->next;
    }
  }

}

int LinkedList:: sum_big(){

  int result=0;
  struct Node*temp=head;
  if(temp==NULL){
    cout<<"Empty linked list"<<endl;
  }else{
      while(temp!=NULL && temp->next!=NULL){

        if(temp->data > temp->next->data){
          //when upcoming node is less than current nodes
          result+=temp->data;
        }
        temp=temp->next;
      }
      //compare tail node value is greater than head node
      if(temp->data > head->data){
        result+=temp->data;
      }

  }

  return result;

}
int main(){
 
  //create object
  LinkedList obj;
  //insert element of linked list
  obj.insert(4);
  obj.insert(2);
  obj.insert(9);
  obj.insert(3);
  obj.insert(7);
  obj.insert(10);
  obj.insert(5);

  //display all node
  obj.display();
  cout<<"\nResult : "<<obj.sum_big()<<endl;
}

Output

Linked List : 4 2 9 3 7 10 5 
Result : 28
//Java Program
//Sum of nodes which are greater than upcoming nodes of linked list
class Node {
  public int data;
  public Node next;
  public Node(int value) {
    data = value;
    next = null;
  }
}
public class LinkedList {


  public Node head;
  //Class constructors
  public LinkedList() {
    head = null;
  }
  //insert element
  public void insert(int value) {
    //Create  node
    Node node = new Node(value);

    if (head == null) head = node;
    else {
      Node temp = head;
      //find last node
      while (temp.next != null) {
        temp = temp.next;
      }
      temp.next = node;
    }

  }
  //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.data);
        temp = temp.next;
      }
    } else {
      System.out.println("Empty Linked list");
    }
  }
  public int sumBig() {

    int result = 0;
    Node temp = head;
    if (temp == null) {
      System.out.println("Empty linked list");
    } else {
      while (temp != null && temp.next != null) {

        if (temp.data > temp.next.data) {
          //when upcoming node is less than current nodes
          result += temp.data;
        }
        temp = temp.next;
      }
      //compare tail node value is greater than head node
      if (temp.data > head.data) {
        result += temp.data;
      }

    }

    return result;

  }

  public static void main(String[] args) {

    LinkedList obj = new LinkedList();
    //insert element of linked list
    obj.insert(4);
    obj.insert(2);
    obj.insert(9);
    obj.insert(3);
    obj.insert(7);
    obj.insert(10);
    obj.insert(5);

    //display all node
    obj.display();
    System.out.println("\nResult : " + obj.sumBig());
  }
}

Output

Linked List Element :  4  2  9  3  7  10  5
Result : 28
#Python 
#Sum of nodes which are greater than upcoming nodes of linked list
class Node:
    def __init__(self,data):
        self.data=data
        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,data):
        node=Node(data)
        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):
        temp=self.head
        print("Linked List Elements : "),
        while(temp!=None):
          print(temp.data,end=" ")
          temp=temp.next
        print("\n")

    def sumBig(self):

        result=0
        temp=self.head
        if(temp==None):
          print("Empty linked list")
        else :
            while(temp!=None and temp.next!=None):

              if(temp.data > temp.next.data):
                #when upcoming node is less than current nodes
                result+=temp.data
             
              temp=temp.next
           
            #compare tail node value is greater than head node
            if(temp.data > self.head.data):
              result+=temp.data
        
        return result

     
def main():
    #Create Object of class Linked
    obj=Linked()
    #Perform Linked operation
    #insert element of linked list
    obj.insert(4)
    obj.insert(2)
    obj.insert(9)
    obj.insert(3)
    obj.insert(7)
    obj.insert(10)
    obj.insert(5)

    #display all node
    obj.display()
    print("\nResult : ",obj.sumBig())
if __name__=="__main__":
    main()

Output

Linked List Elements : 
4 2 9 3 7 10 5 


Result :  28
//C# Program
//Sum of nodes which are greater than upcoming nodes of linked list
using System;
//node class
public class Node{
   public  int data;
   public  Node next;
}
class Program
{
    Node head;
    public Program(){
        head=null;
    }
    //insert node of linked list
    public void insert(int data){
        Node newNode=new Node();
        newNode.data=data;
        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;
            Console.Write("Linked list");
            while(temp!=null){
                Console.Write("  {0}",temp.data);
                temp=temp.next;
            }
            
        }
    }
    public int sumBig(){

        int result=0;
        Node temp=head;
        if(temp==null){
          Console.Write("Empty linked list");
        }else{
            while(temp!=null && temp.next!=null){

              if(temp.data > temp.next.data){
                //when upcoming node is less than current nodes
                result+=temp.data;
              }
              temp=temp.next;
            }
            //compare tail node value is greater than head node
            if(temp.data > head.data){
              result+=temp.data;
            }

        }

        return result;

      }

    static void Main()
    {
        Program obj=new Program();
        //insert element of linked list
        obj.insert(4);
        obj.insert(2);
        obj.insert(9);
        obj.insert(3);
        obj.insert(7);
        obj.insert(10);
        obj.insert(5);

        //display all node
        obj.display();
        Console.Write("\nResult : "+obj.sumBig());
    }
}

Output

Linked list 4 2 9 3 7 10 5
Result : 28
<?php
//Php program 
//Sum of nodes which are greater than upcoming nodes of linked list

class Node
{
    public $data;
    public $next;
    function __construct($data)
    {
        $this->data = $data;
        $this->next = NULL;
    }
}
class LinkedList{

    private $head;
    function __construct()
    {
        $head=NULL;
    }
    /*
    * Append the Given data value at end of linked list
    * Fun : insert
    * Parm: data value
    *@return None
    */
    function insert($data)
    {
        $newNode=new Node($data); 
        if($this->head==NULL)
        {
            $this->head=$newNode;
        }else{
            $temp=$this->head;
            //find last node of linked list
            while($temp->next!=NULL){
                $temp=$temp->next;
            }
            //add new node to last of linked list
            $temp->next=$newNode;
        }
    }
    //Display all inserted node in linked list
    function display()
    {
        if($this->head==NULL)
        {
            echo "Empty Linked List";
        }
        else{
            $temp=$this->head;
            echo "Linked List :";
            while($temp!=NULL){
                //display node value
                echo "  ".$temp->data;
                $temp=$temp->next; //visit to next node
            }
        }   
    }
    function sum_big()
    {

        $result=0;
        $temp=$this->head;
        if($temp==NULL)
        {
          echo "Empty linked list<br>";
        }else
        {
            while($temp!=NULL && $temp->next!=NULL)
            {

              if($temp->data > $temp->next->data)
              {
                //when upcoming node is less than current nodes
                $result+=$temp->data;
              }
              $temp=$temp->next;
            }
            //compare tail node value is greater than head node
            if($temp->data > $this->head->data)
            {
              $result+=$temp->data;
            }

        }

        return $result;

      }
}
function main(){
  //Make a object of LinkedList class
  $obj= new LinkedList();

  /*
  *Insert following nodes in linked list
  */
  $obj->insert(4);
  $obj->insert(2);
  $obj->insert(9);
  $obj->insert(3);
  $obj->insert(7);
  $obj->insert(10);
  $obj->insert(5);
  $obj->display();
  echo "<br> Result:".$obj->sum_big();
}
main();
?>

Output

Linked List : 4 2 9 3 7 10 5
Result:28


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