# 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.

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

}
else{
while(temp){
printf("  %d",temp->data);
temp=temp->next;
break; //terminate loop
}
}

}
}
}
else{

int result=0;
while(temp!=NULL){
result+=temp->data;
temp=temp->next;
break; //terminate loop
}
}
printf("\n Sum of Node Element : %d",result);
}
}
int main(){

//create node pointer

//display all node
}
``````

#### 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{
public:
CircularList();
void insert(int);
void display();
void node_sum();
};
CircularList::CircularList(){
}

//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;
}else{
//find last node
temp=temp->next;
}
temp->next=node;
}
}
}
//display element of Node
void CircularList:: display(){

}
else{
while(temp){
cout<<"  "<<temp->data;
temp=temp->next;
break; //terminate loop
}
}

}
}
void CircularList::node_sum(){
}
else{

int result=0;
while(temp!=NULL){
result+=temp->data;
temp=temp->next;
break; //terminate loop
}
}
cout<<"\n Sum of Node Element : "<<result;
}
}
int main(){
CircularList obj=CircularList();
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
static class Node{
int data;
Node next;
}
//Class constructors
}
//insert node at last of linke list
static void insert(int value){
//Create a node
Node node=new Node();
node.data=value;
}
else{
//find lase node
temp=temp.next;
}
temp.next=node;
}

}
//Display node element of circular linked list
public static void display(){
}else{
while(temp!=null){
System.out.print("  "+temp.data);
temp=temp.next;
}
}
}
public static void nodeSum(){
}else{

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

public static void main(String[] args) {
obj.insert(1);
obj.insert(3);
obj.insert(5);
obj.insert(2);
obj.insert(6);
obj.insert(7);
obj.insert(1);

obj.display();

}
}``````

#### 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

#Insert new node to End of Linked list
def insert(self,data):
node=Node(data)
#when no element of linked list
node.next=node
else:
temp=temp.next
temp.next=node
#display all element of circular linked list
def display(self):
return

while(temp!=None):
print(temp.data)
temp=temp.next
#when find first node
break

print("\n")

def nodeSum(self):
return

result=0
while(temp!=None):
result+=temp.data
temp=temp.next
#when find first node
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)
``````
``````//C# program
//Sum of circular linked list nodes
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){
}
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
//when no element of linked list
}
else{
//get last node
temp=temp.next;
}
temp.next=newNode;
}
}
public void display(){
}else{
Console.Write(" Circular Linked List : ");
while(temp!=null){
Console.Write(" {0}",temp.data);
temp=temp.next;
//End of Loop iteration
}
}
}
public void nodeSum(){
}else{

int result=0;
while(temp!=null){
result+=temp.data;
temp=temp.next;
}
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();
}
}``````

#### 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.