Sum of the nodes of a Circular Linked List

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

Sum of Circular Linked List

Here given code implementation process.

//C Program 
//Sum of circular linked list nodes
#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*);
void node_sum(struct Node*);
//insert Node element at end of linked list
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=*head;
    if(*head==NULL){
      *head=node;
      node->next=*head;
    }else{
      struct Node*temp=*head;
      while(temp->next!=*head){
        temp=temp->next;
      }
      //Add node at last position
      temp->next=node;
    }
  }
}
//Display element of Node
void display(struct Node*head){
  
  if(head==NULL){
    printf("Empty linked list");
  }
  else{
    printf("\n Linked List Element :");
    struct Node*temp=head;
    while(temp){
      printf("  %d",temp->data);
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }

  }
}
void node_sum(struct Node*head){
  if(head==NULL){
    printf("Empty linked list");
  }
  else{
    
    int result=0;
    struct Node*temp=head;
    while(temp!=NULL){
      //add node value
      result+=temp->data;
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }
    printf("\n Sum of Node Element : %d",result);
  }
}
int main(){

  //create node pointer
  struct Node*head=NULL;
  //insert element of linked list
  insert(&head,1);
  insert(&head,3);
  insert(&head,5);
  insert(&head,2);
  insert(&head,6);
  insert(&head,7);
  insert(&head,1);

  //display all node
  display(head);
  node_sum(head);
}

Output

 Linked List Element :  1  3  5  2  6  7  1
 Sum of Node Element : 25
//C++ Program 
//Sum of circular linked list nodes
#include<iostream>
using namespace std;

//create structure
struct Node{
  int data;
  struct Node*next;
};
class CircularList{
  Node *head;
public:
  CircularList();
  void insert(int);
  void display();
  void node_sum();
};
CircularList::CircularList(){
  head=NULL;
}

//insert Node at end of linked list 
void CircularList:: insert(int value){
  //Create dynamic node
  struct Node*node=new Node;
  if(node==NULL){
    cout<<"Memory overflow"<<endl;
  }else{
    node->data=value;
    node->next=head;
    if(head==NULL){
      //when linked list empty
      head=node;
      node->next=head;
    }else{
      Node*temp=head;
      //find last node
      while(temp->next!=head){
        temp=temp->next;
      }
      //Add node at last
      temp->next=node;
    }
  }
}
//display element of Node
void CircularList:: display(){
  
  if(head==NULL){
    cout<<"Empty linked list";
  }
  else{
    cout<<"Element of linked list :";
    struct Node*temp=head;
    while(temp){
      cout<<"  "<<temp->data;
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }

  }
}
//Add all circular linked list elements
void CircularList::node_sum(){
  if(head==NULL){
    cout<<"Empty linked list";
  }
  else{
    
    int result=0;
    struct Node*temp=head;
    while(temp!=NULL){
      //add node value
      result+=temp->data;
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }
   cout<<"\n Sum of Node Element : "<<result;
  }
}
int main(){
  CircularList obj=CircularList();
  //insert element of linked list
  obj.insert(1);
  obj.insert(3);
  obj.insert(5);
  obj.insert(2);
  obj.insert(6);
  obj.insert(7);
  obj.insert(1);

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

Output

Element of linked list :  1  3  5  2  6  7  1
 Sum of Node Element : 25
//Java Program to 
//Sum of circular linked list nodes
public class LinkedList{
  static class Node{
    int data;
    Node next;
  }
  static Node head;
    //Class constructors
  LinkedList(){
    head=null;
  } 
    //insert node at last of linke list
  static void insert(int value){
      //Create a node
    Node node=new Node();
    node.data=value;
    node.next=head;
    if(head==null){
      head=node;
      node.next=head;
    }
    else{
      Node temp=head;
          //find lase node
      while(temp.next!=head){
        temp=temp.next;
      }
          //add node
      temp.next=node;
    }

  }
    //Display node element of circular linked list
  public static void display(){
    if(head==null){
      System.out.println("Empty Linked List");
    }else{
      System.out.print("Circular Linked List Element :");
      Node temp=head;
      while(temp!=null){
        System.out.print("  "+temp.data);
        temp=temp.next;
        if(temp==head) break;
      }
    }
  }
    //add all linked list nodes
  public static void nodeSum(){
    if(head==null){
      System.out.println("Empty Linked List");
    }else{

      Node temp=head;
      int result=0;
      while(temp!=null){
        result+=temp.data;
        temp=temp.next;
        if(temp==head) break;
      }
      System.out.println("\nSum of nodes :"+result);
    }
  }

  public static void main(String[] args) {
    LinkedList obj=new LinkedList();
    //insert element of linked list
    obj.insert(1);
    obj.insert(3);
    obj.insert(5);
    obj.insert(2);
    obj.insert(6);
    obj.insert(7);
    obj.insert(1);
    

    obj.display();
    obj.nodeSum();//add node element

  }
}

Output

Circular Linked List Element :  1  3  5  2  6  7  1
Sum of nodes :25
#Python
#Sum of circular linked list nodes
class Node:
    def __init__(self,data):
        self.data=data
        self.next=None

#create class CircularList    
class CircularList:
    def __init__(self):
        #Assign default value
        self.head=None

    #Insert new node to End of Linked list  
    def insert(self,data):
        node=Node(data)
        node.next=self.head
        if(self.head==None):
            #when no element of linked list
            self.head=node
            node.next=node
        else:
            temp=self.head
            while(temp.next!=self.head):
                temp=temp.next
            #add node    
            temp.next=node    
    #display all element of circular linked list      
    def display(self):
        if(self.head==None):
            print("Empty Linked List")
            return

        temp=self.head
        print("Linked list Elements : ")
        while(temp!=None):
            print(temp.data)
            temp=temp.next
            #when find first node
            if(temp==self.head):
                break

        print("\n")

    #add all element of circular linked list      
    def nodeSum(self):
        if(self.head==None):
            print("Empty Linked List")
            return

        temp=self.head
        result=0
        while(temp!=None):
            #add node value
            result+=temp.data
            temp=temp.next
            #when find first node
            if(temp==self.head):
                break

        print("Sum of nodes :",result)

def main():
    #Create Object of class CircularList
    obj=CircularList()
    #Insert element
    obj.insert(1)
    obj.insert(3)
    obj.insert(5)
    obj.insert(2)
    obj.insert(6)
    obj.insert(7)
    obj.insert(1)
    obj.display()
    obj.nodeSum()    

if __name__ == '__main__':
    main()

Output

Linked list Elements : 
1
3
5
2
6
7
1


('Sum of nodes :', 25)
Python : Sum of Circular Linked List
//C# program 
//Sum of circular linked list nodes
using System;
//node class
public class Node{
  public  int data;
  public  Node next;
}
class Program
{
  public 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=head;
    if(head==null){
      //when no element of linked list
      head=newNode;
      newNode.next=head;
    }
    else{
      Node temp=head;
      //get last node
      while(temp.next!=head ){
        temp=temp.next;
      }
      //add new node
      temp.next=newNode;
    }
  }
  //Display element of linked list
  public void display(){
    if(head==null){
      Console.Write("Empty Linked List");
    }else{
      Console.Write(" Circular Linked List : ");
      Node temp=head;
      while(temp!=null){
        Console.Write(" {0}",temp.data);
        temp=temp.next;
        //End of Loop iteration 
        if(temp==head) break;
      }
    }
  }
  //add all linked list nodes
  public void nodeSum(){
    if(head==null){
      Console.Write("Empty Linked List");
    }else{

      Node temp=head;
      int result=0;
      while(temp!=null){
        result+=temp.data;
        temp=temp.next;
        if(temp==head) break;
      }
      Console.Write("\n Sum of nodes : {0}",result);
    }
  }

  static void Main(){

    Program obj=new Program();
    //Insert node value
    obj.insert(1);
    obj.insert(3);
    obj.insert(5);
    obj.insert(2);
    obj.insert(6);
    obj.insert(7);
    obj.insert(1);
    

    obj.display();
    obj.nodeSum();//add node element
  }
}

Output

 Circular Linked List :  1 3 5 2 6 7 1
 Sum of nodes : 25


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