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>

struct LinkNode
{
	int key;
	struct LinkNode *back;
	struct LinkNode *next;
};
struct DoublyLL
{
	struct LinkNode *front;
	struct LinkNode *rear;
};
// Returns a new linked list
struct DoublyLL *createLinkedList()
{
	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
struct LinkNode *newNode(int key, struct LinkNode *back)
{
	// Create dynamic node
	struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
	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;
}
// Display linked list element
void display(struct DoublyLL *dll)
{
	if (dll->front == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	printf(" NULL ←");
	struct LinkNode *temp = dll->front;
	// iterating linked list elements
	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)
	{
		printf("\n Empty linked list\n");
		return;
	}
	struct LinkNode *temp = dll->front;
	int sum = 0;
	// iterating linked list elements
	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()
{
	struct DoublyLL *dll = createLinkedList();
	// Add nodes
	addNode(dll, 14);
	addNode(dll, 4);
	addNode(dll, 21);
	addNode(dll, 10);
	addNode(dll, 13);
	addNode(dll, 15);
	addNode(dll, 28);
	// 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
*/
class LinkNode
{
	public int key;
	public LinkNode back;
	public LinkNode next;
	public LinkNode(int key, LinkNode back)
	{
		this.back = back;
		this.key = key;
		this.next = null;
	}
}
public class DoublyLL
{
	public LinkNode front;
	public LinkNode rear;
	public DoublyLL()
	{
		this.front = null;
		this.rear = null;
	}
	// Handles the request to add new node at the end of linked list
	public void addNode(int data)
	{
		// Create dynamic node
		LinkNode 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 linked list element
	public void display()
	{
		if (this.front == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		System.out.print(" NULL ←");
		LinkNode temp = this.front;
		// iterating linked list elements
		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)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.front;
		int sum = 0;
		// iterating linked list elements
		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();
		// Add nodes
		dll.addNode(14);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// 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
*/

class LinkNode
{
	public: int key;
	LinkNode *back;
	LinkNode *next;
	LinkNode(int key, LinkNode *back)
	{
		this->back = back;
		this->key = key;
		this->next = NULL;
	}
};
class DoublyLL
{
	public: LinkNode *front;
	LinkNode *rear;
	DoublyLL()
	{
		this->front = NULL;
		this->rear = NULL;
	}
	// Handles the request to add new node at the end of linked list
	void addNode(int data)
	{
		// Create dynamic node
		LinkNode *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 linked list element
	void display()
	{
		if (this->front == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		cout << " NULL ←";
		LinkNode *temp = this->front;
		// iterating linked list elements
		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;
		}
		LinkNode *temp = this->front;
		int sum = 0;
		// iterating linked list elements
		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();
	// Add nodes
	dll.addNode(14);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// 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 class LinkNode
{
	public int key;
	public LinkNode back;
	public LinkNode next;
	public LinkNode(int key, LinkNode back)
	{
		this.back = back;
		this.key = key;
		this.next = null;
	}
}
public class DoublyLL
{
	public LinkNode front;
	public LinkNode rear;
	public DoublyLL()
	{
		this.front = null;
		this.rear = null;
	}
	// Handles the request to add new node at the end of linked list
	public void addNode(int data)
	{
		// Create dynamic node
		LinkNode 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 linked list element
	public void display()
	{
		if (this.front == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		Console.Write(" NULL ←");
		LinkNode temp = this.front;
		// iterating linked list elements
		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)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.front;
		int sum = 0;
		// iterating linked list elements
		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();
		// Add nodes
		dll.addNode(14);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// 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
*/
class LinkNode
{
	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
	public	function addNode($data)
	{
		// Create dynamic node
		$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 linked list element
	public	function display()
	{
		if ($this->front == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		echo " NULL ←";
		$temp = $this->front;
		// iterating linked list elements
		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)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->front;
		$sum = 0;
		// iterating linked list elements
		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();
	// Add nodes
	$dll->addNode(14);
	$dll->addNode(4);
	$dll->addNode(21);
	$dll->addNode(10);
	$dll->addNode(13);
	$dll->addNode(15);
	$dll->addNode(28);
	// 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
*/
class LinkNode
{
	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
	addNode(data)
	{
		// 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 linked list element
	display()
	{
		if (this.front == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		process.stdout.write(" NULL ←");
		var temp = this.front;
		// iterating linked list elements
		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)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.front;
		var sum = 0;
		// iterating linked list elements
		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();
	// Add nodes
	dll.addNode(14);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// 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

class LinkNode :
	
	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
	def addNode(self, data) :
		#  Create dynamic node
		node = LinkNode(data, self.rear)
		if (self.front == None) :
			self.front = node
			self.rear = node
		else :
			self.rear.next = node
		
		self.rear = node
	
	#  Display linked list element
	def display(self) :
		if (self.front == None) :
			print("\n Empty linked list")
			return
		
		print(" NULL ←", end = "")
		temp = self.front
		#  iterating linked list elements
		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) :
			print("\n Empty linked list")
			return
		
		temp = self.front
		sum = 0
		#  iterating linked list elements
		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()
	#  Add nodes
	dll.addNode(14)
	dll.addNode(4)
	dll.addNode(21)
	dll.addNode(10)
	dll.addNode(13)
	dll.addNode(15)
	dll.addNode(28)
	#  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

class LinkNode  
	# Define the accessor and reader of class LinkNode  
	attr_reader :key, :back, :next
	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_reader :front, :rear
	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
	def addNode(data) 
		#  Create dynamic node
		node = LinkNode.new(data, self.rear)
		if (self.front == nil) 
			self.front = node
			self.rear = node
		else 
			self.rear.next = node
		end

		self.rear = node
	end

	#  Display linked list element
	def display() 
		if (self.front == nil) 
			print("\n Empty linked list\n")
			return
		end

		print(" NULL ←")
		temp = self.front
		#  iterating linked list elements
		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) 
			print("\n Empty linked list\n")
			return
		end

		temp = self.front
		sum = 0
		#  iterating linked list elements
		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()
	#  Add nodes
	dll.addNode(14)
	dll.addNode(4)
	dll.addNode(21)
	dll.addNode(10)
	dll.addNode(13)
	dll.addNode(15)
	dll.addNode(28)
	#  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
*/
class LinkNode(var key: Int , var back: LinkNode , var next: LinkNode)
{
	def this(key: Int, back: LinkNode)
	{
		this(key, back, null);
	}
}
class DoublyLL(var front: LinkNode , var rear: LinkNode)
{
	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
		var node: LinkNode = new LinkNode(data, this.rear);
		if (this.front == null)
		{
			this.front = node;
			this.rear = node;
		}
		else
		{
			this.rear.next = node;
		}
		this.rear = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.front == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print(" NULL ←");
		var temp: LinkNode = this.front;
		// iterating linked list elements
		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)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.front;
		var sum: Int = 0;
		// iterating linked list elements
		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();
		// Add nodes
		dll.addNode(14);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// 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
*/
class LinkNode
{
	var key: Int;
	var back: LinkNode? ;
	var next: LinkNode? ;
	init(_ key: Int, _ back: LinkNode? )
	{
		self.back = back;
		self.key = key;
		self.next = nil;
	}
}
class DoublyLL
{
	var front: LinkNode? ;
	var rear: LinkNode? ;
	init()
	{
		self.front = nil;
		self.rear = nil;
	}
	// Handles the request to add new node at the end of linked list
	func addNode(_ data: Int)
	{
		// Create dynamic node
		let node: LinkNode? = LinkNode(data, self.rear);
		if (self.front == nil)
		{
			self.front = node;
			self.rear = node;
		}
		else
		{
			self.rear!.next = node;
		}
		self.rear = node;
	}
	// Display linked list element
	func display()
	{
		if (self.front == nil)
		{
			print("\n Empty linked list");
			return;
		}
		print(" NULL ←", terminator: "");
		var temp: LinkNode? = self.front;
		// iterating linked list elements
		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)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.front;
		var sum: Int = 0;
		// iterating linked list elements
		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();
	// Add nodes
	dll.addNode(14);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// 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
*/
class LinkNode
{
	var key: Int;
	var back: LinkNode ? ;
	var next: LinkNode ? ;
	constructor(key: Int, back: LinkNode ? )
	{
		this.back = back;
		this.key = key;
		this.next = null;
	}
}
class DoublyLL
{
	var front: LinkNode ? ;
	var rear: LinkNode ? ;
	constructor()
	{
		this.front = null;
		this.rear = null;
	}
	// Handles the request to add new node at the end of linked list
	fun addNode(data: Int): Unit
	{
		// Create dynamic node
		var node: LinkNode ? = LinkNode(data, this.rear);
		if (this.front == null)
		{
			this.front = node;
			this.rear = node;
		}
		else
		{
			this.rear?.next = node;
		}
		this.rear = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.front == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print(" NULL ←");
		var temp: LinkNode ? = this.front;
		// iterating linked list elements
		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)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.front;
		var sum: Int = 0;
		// iterating linked list elements
		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();
	// Add nodes
	dll.addNode(14);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// 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.

New Comment







© 2021, kalkicode.com, All rights reserved