# Find the sum of all nodes which is divisible by K in doubly linked list

Here given code implementation process.

``````// C Program
// Find the sum of all nodes which is divisible by K in doubly linked list
#include <stdio.h>

#include <stdlib.h>

{
int key;
};
struct DoublyLL
{
};
// Returns a new linked list
{
struct DoublyLL *dll = (struct DoublyLL *) malloc(sizeof(struct DoublyLL));
if (dll == NULL)
{
printf("\n Memory overflow , When creating a new linked list");
}
else
{
// Set initial value of linked list
dll->front = NULL;
dll->rear = NULL;
}
return dll;
}
// Returns a new node of linked list
{
// Create dynamic node
if (node != NULL)
{
node->key = key;
node->back = back;
node->next = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return node;
}
// Handles the request to add new node at the end of linked list
void addNode(struct DoublyLL *dll, int data)
{
// Create dynamic node
struct LinkNode *node = newNode(data, dll->rear);
if (dll->front == NULL)
{
dll->front = node;
dll->rear = node;
}
else
{
dll->rear->next = node;
}
dll->rear = node;
}
void display(struct DoublyLL *dll)
{
if (dll->front == NULL)
{
return;
}
printf(" NULL ←");
while (temp != NULL)
{
if (temp != dll->front)
{
printf("↔");
}
printf(" %d ", temp->key);
// Visit to next node
temp = temp->next;
}
printf("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
void divisibleSum(struct DoublyLL *dll, int k)
{
if (dll->front == NULL)
{
return;
}
int sum = 0;
while (temp != NULL)
{
if ((temp->key % k) == 0)
{
sum += temp->key;
}
// Visit to next node
temp = temp->next;
}
printf("\n Given k : %d ", k);
printf("\n Sum of nodes is : %d\n", sum);
}
int main()
{
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
display(dll);
// k = 5
divisibleSum(dll, 5);
// k = 7
divisibleSum(dll, 7);
return 0;
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````/*
Java Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
public int key;
{
this.back = back;
this.key = key;
this.next = null;
}
}
public class DoublyLL
{
public DoublyLL()
{
this.front = null;
this.rear = null;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (this.front == null)
{
this.front = node;
this.rear = node;
}
else
{
this.rear.next = node;
}
this.rear = node;
}
public void display()
{
if (this.front == null)
{
return;
}
System.out.print(" NULL ←");
while (temp != null)
{
if (temp != this.front)
{
System.out.print("↔");
}
System.out.print(" " + temp.key + " ");
// Visit to next node
temp = temp.next;
}
System.out.print("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
public void divisibleSum(int k)
{
if (this.front == null)
{
return;
}
int sum = 0;
while (temp != null)
{
if ((temp.key % k) == 0)
{
sum += temp.key;
}
// Visit to next node
temp = temp.next;
}
System.out.print("\n Given k : " + k);
System.out.print("\n Sum of nodes is : " + sum + "\n");
}
public static void main(String[] args)
{
DoublyLL dll = new DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Remove all the even digit sum nodes from a doubly linked list
*/

{
public: int key;
{
this->back = back;
this->key = key;
this->next = NULL;
}
};
class DoublyLL
{
DoublyLL()
{
this->front = NULL;
this->rear = NULL;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (this->front == NULL)
{
this->front = node;
this->rear = node;
}
else
{
this->rear->next = node;
}
this->rear = node;
}
void display()
{
if (this->front == NULL)
{
cout << "\n Empty linked list\n";
return;
}
cout << " NULL ←";
while (temp != NULL)
{
if (temp != this->front)
{
cout << "↔";
}
cout << " " << temp->key << " ";
// Visit to next node
temp = temp->next;
}
cout << "→ NULL";
}
// Handles the request of finding sum of all nodes which is divisible by given number k
void divisibleSum(int k)
{
if (this->front == NULL)
{
cout << "\n Empty linked list\n";
return;
}
int sum = 0;
while (temp != NULL)
{
if ((temp->key % k) == 0)
{
sum += temp->key;
}
// Visit to next node
temp = temp->next;
}
cout << "\n Given k : " << k;
cout << "\n Sum of nodes is : " << sum << "\n";
}
};
int main()
{
DoublyLL dll = DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
return 0;
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````// Include namespace system
using System;
/*
C# Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
public int key;
{
this.back = back;
this.key = key;
this.next = null;
}
}
public class DoublyLL
{
public DoublyLL()
{
this.front = null;
this.rear = null;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (this.front == null)
{
this.front = node;
this.rear = node;
}
else
{
this.rear.next = node;
}
this.rear = node;
}
public void display()
{
if (this.front == null)
{
return;
}
Console.Write(" NULL ←");
while (temp != null)
{
if (temp != this.front)
{
Console.Write("↔");
}
Console.Write(" " + temp.key + " ");
// Visit to next node
temp = temp.next;
}
Console.Write("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
public void divisibleSum(int k)
{
if (this.front == null)
{
return;
}
int sum = 0;
while (temp != null)
{
if ((temp.key % k) == 0)
{
sum += temp.key;
}
// Visit to next node
temp = temp.next;
}
Console.Write("\n Given k : " + k);
Console.Write("\n Sum of nodes is : " + sum + "\n");
}
public static void Main(String[] args)
{
DoublyLL dll = new DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````<?php
/*
Php Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
public \$key;
public \$back;
public \$next;

function __construct(\$key, \$back)
{
\$this->back = \$back;
\$this->key = \$key;
\$this->next = null;
}
}
class DoublyLL
{
public \$front;
public \$rear;

function __construct()
{
\$this->front = null;
\$this->rear = null;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (\$this->front == null)
{
\$this->front = \$node;
\$this->rear = \$node;
}
else
{
\$this->rear->next = \$node;
}
\$this->rear = \$node;
}
public	function display()
{
if (\$this->front == null)
{
return;
}
echo " NULL ←";
\$temp = \$this->front;
while (\$temp != null)
{
if (\$temp != \$this->front)
{
echo "↔";
}
echo " ". \$temp->key ." ";
// Visit to next node
\$temp = \$temp->next;
}
echo "→ NULL";
}
// Handles the request of finding sum of all nodes which is divisible by given number k
public	function divisibleSum(\$k)
{
if (\$this->front == null)
{
return;
}
\$temp = \$this->front;
\$sum = 0;
while (\$temp != null)
{
if ((\$temp->key % \$k) == 0)
{
\$sum += \$temp->key;
}
// Visit to next node
\$temp = \$temp->next;
}
echo "\n Given k : ". \$k;
echo "\n Sum of nodes is : ". \$sum ."\n";
}
}

function main()
{
\$dll = new DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
\$dll->display();
// k = 5
\$dll->divisibleSum(5);
// k = 7
\$dll->divisibleSum(7);
}
main();``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````/*
Node Js Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
constructor(key, back)
{
this.back = back;
this.key = key;
this.next = null;
}
}
class DoublyLL
{
constructor()
{
this.front = null;
this.rear = null;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
var node = new LinkNode(data, this.rear);
if (this.front == null)
{
this.front = node;
this.rear = node;
}
else
{
this.rear.next = node;
}
this.rear = node;
}
display()
{
if (this.front == null)
{
return;
}
process.stdout.write(" NULL ←");
var temp = this.front;
while (temp != null)
{
if (temp != this.front)
{
process.stdout.write("↔");
}
process.stdout.write(" " + temp.key + " ");
// Visit to next node
temp = temp.next;
}
process.stdout.write("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
divisibleSum(k)
{
if (this.front == null)
{
return;
}
var temp = this.front;
var sum = 0;
while (temp != null)
{
if ((temp.key % k) == 0)
{
sum += temp.key;
}
// Visit to next node
temp = temp.next;
}
process.stdout.write("\n Given k : " + k);
process.stdout.write("\n Sum of nodes is : " + sum + "\n");
}
}

function main()
{
var dll = new DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}
main();``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````#   Python 3 Program
#   Remove all the even digit sum nodes from a doubly linked list

def __init__(self, key, back) :
self.back = back
self.key = key
self.next = None

class DoublyLL :

def __init__(self) :
self.front = None
self.rear = None

#  Handles the request to add new node at the end of linked list
#  Create dynamic node
if (self.front == None) :
self.front = node
self.rear = node
else :
self.rear.next = node

self.rear = node

def display(self) :
if (self.front == None) :
return

print(" NULL ←", end = "")
temp = self.front
while (temp != None) :
if (temp != self.front) :
print("↔", end = "")

print("", temp.key ,"", end = "")
#  Visit to next node
temp = temp.next

print("→ NULL", end = "")

#  Handles the request of finding sum of all nodes which is divisible by given number k
def divisibleSum(self, k) :
if (self.front == None) :
return

temp = self.front
sum = 0
while (temp != None) :
if ((temp.key % k) == 0) :
sum += temp.key

#  Visit to next node
temp = temp.next

print("\n Given k : ", k, end = "")
print("\n Sum of nodes is : ", sum )

def main() :
dll = DoublyLL()
#  NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display()
#  k = 5
dll.divisibleSum(5)
#  k = 7
dll.divisibleSum(7)

if __name__ == "__main__": main()``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k :  5
Sum of nodes is :  25

Given k :  7
Sum of nodes is :  63``````
``````#   Ruby Program
#   Remove all the even digit sum nodes from a doubly linked list

attr_accessor :key, :back, :next

def initialize(key, back)
self.back = back
self.key = key
self.next = nil
end

end

class DoublyLL
# Define the accessor and reader of class DoublyLL
attr_accessor :front, :rear

def initialize()
self.front = nil
self.rear = nil
end

#  Handles the request to add new node at the end of linked list
#  Create dynamic node
if (self.front == nil)
self.front = node
self.rear = node
else
self.rear.next = node
end

self.rear = node
end

def display()
if (self.front == nil)
return
end

print(" NULL ←")
temp = self.front
while (temp != nil)
if (temp != self.front)
print("↔")
end

print(" ", temp.key ," ")
#  Visit to next node
temp = temp.next
end

print("→ NULL")
end

#  Handles the request of finding sum of all nodes which is divisible by given number k
def divisibleSum(k)
if (self.front == nil)
return
end

temp = self.front
sum = 0
while (temp != nil)
if ((temp.key % k) == 0)
sum += temp.key
end

#  Visit to next node
temp = temp.next
end

print("\n Given k : ", k)
print("\n Sum of nodes is : ", sum ,"\n")
end

end

def main()
dll = DoublyLL.new()
#  NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display()
#  k = 5
dll.divisibleSum(5)
#  k = 7
dll.divisibleSum(7)
end

main()``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63
``````
``````/*
Scala Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
{
this(key, back, null);
}
}
{
def this()
{
this(null, null);
}
// Handles the request to add new node at the end of linked list
def addNode(data: Int): Unit = {
// Create dynamic node
if (this.front == null)
{
this.front = node;
this.rear = node;
}
else
{
this.rear.next = node;
}
this.rear = node;
}
def display(): Unit = {
if (this.front == null)
{
return;
}
print(" NULL ←");
while (temp != null)
{
if (temp != this.front)
{
print("↔");
}
print(" " + temp.key + " ");
// Visit to next node
temp = temp.next;
}
print("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
def divisibleSum(k: Int): Unit = {
if (this.front == null)
{
return;
}
var sum: Int = 0;
while (temp != null)
{
if ((temp.key % k) == 0)
{
sum += temp.key;
}
// Visit to next node
temp = temp.next;
}
print("\n Given k : " + k);
print("\n Sum of nodes is : " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var dll: DoublyLL = new DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````
``````/*
Swift 4 Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
var key: Int;
init(_ key: Int, _ back: LinkNode? )
{
self.back = back;
self.key = key;
self.next = nil;
}
}
class DoublyLL
{
init()
{
self.front = nil;
self.rear = nil;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (self.front == nil)
{
self.front = node;
self.rear = node;
}
else
{
self.rear!.next = node;
}
self.rear = node;
}
func display()
{
if (self.front == nil)
{
return;
}
print(" NULL ←", terminator: "");
while (temp  != nil)
{
if (!(temp  === self.front))
{
print("↔", terminator: "");
}
print("", temp!.key ,"", terminator: "");
// Visit to next node
temp = temp!.next;
}
print("→ NULL", terminator: "");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
func divisibleSum(_ k: Int)
{
if (self.front == nil)
{
return;
}
var sum: Int = 0;
while (temp  != nil)
{
if ((temp!.key % k) == 0)
{
sum += temp!.key;
}
// Visit to next node
temp = temp!.next;
}
print("\n Given k : ", k, terminator: "");
print("\n Sum of nodes is : ", sum );
}
}
func main()
{
let dll: DoublyLL = DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}
main();``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k :  5
Sum of nodes is :  25

Given k :  7
Sum of nodes is :  63``````
``````/*
Kotlin Program
Remove all the even digit sum nodes from a doubly linked list
*/
{
var key: Int;
constructor(key: Int, back: LinkNode ? )
{
this.back = back;
this.key = key;
this.next = null;
}
}
class DoublyLL
{
constructor()
{
this.front = null;
this.rear = null;
}
// Handles the request to add new node at the end of linked list
{
// Create dynamic node
if (this.front == null)
{
this.front = node;
this.rear = node;
}
else
{
this.rear?.next = node;
}
this.rear = node;
}
fun display(): Unit
{
if (this.front == null)
{
return;
}
print(" NULL ←");
var temp: LinkNode ? = this.front;
while (temp != null)
{
if (temp != this.front)
{
print("↔");
}
print(" " + temp.key + " ");
// Visit to next node
temp = temp.next;
}
print("→ NULL");
}
// Handles the request of finding sum of all nodes which is divisible by given number k
fun divisibleSum(k: Int): Unit
{
if (this.front == null)
{
return;
}
var temp: LinkNode ? = this.front;
var sum: Int = 0;
while (temp != null)
{
if ((temp.key % k) == 0)
{
sum += temp.key;
}
// Visit to next node
temp = temp.next;
}
print("\n Given k : " + k);
print("\n Sum of nodes is : " + sum + "\n");
}
}
fun main(args: Array <String> ): Unit
{
var dll: DoublyLL = DoublyLL();
// NULL ← 14 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
dll.display();
// k = 5
dll.divisibleSum(5);
// k = 7
dll.divisibleSum(7);
}``````

#### Output

`````` NULL ← 14 ↔ 4 ↔ 21 ↔ 10 ↔ 13 ↔ 15 ↔ 28 → NULL
Given k : 5
Sum of nodes is : 25

Given k : 7
Sum of nodes is : 63``````

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.