# Find modular node in a linked list

Given a linked list which is contain data nodes, Our goal is to detect last modular node at end of linked list. Here given code implementation process.

``````// C Program
// Find modular node in a linked list
// from end of linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

struct Node
{
int data;
struct Node *next;
};
struct Node *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;
{
return node;
}
else
{
//find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
}
//Display element of Node
void display(struct Node *temp)
{
if (temp == NULL)
{
}
while (temp != NULL)
{
printf("  %d", temp->data);
temp = temp->next;
}
}
//Find midular node in given linked list
void find_modular_node(struct Node *head, int modular)
{
int counter = 1;
struct Node *result = NULL;
while (temp != NULL)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp->next;
counter++;
}
if (result == NULL)
{
printf("\n Modular node [%d] not exist", modular);
}
else
{
printf("\n Modular Node [%d] is %d", modular, result->data);
}
}
int main()
{
//Display all node
//Test Case
return 0;
}``````

#### Output

``````  11  6  2  7  10  5  9  8  1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Java Program
//Find modular node in a linked list (from end of linked list)
class Node
{
public int data;
public Node next;
public Node(int data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int value)
{
//Create a node
Node node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
//Find midular node in given linked list
public void find_modular_node(int modular)
{
int counter = 1;
Node result = null;
while (temp != null)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp.next;
counter++;
}
if (result == null)
{
System.out.print("\n Modular node [" + modular + "] not exist");
}
else
{
System.out.print("\n Modular Node [" + modular + "] is " + result.data);
}
}
public void display()
{
{
while (temp != null)
{
System.out.print(" " + temp.data);
temp = temp.next;
}
}
else
{
}
}
public static void main(String[] args)
{
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
}
}``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Include header file
#include <iostream>
using namespace std;

//C++ Program
//Find modular node in a linked list (from end of linked list)

class Node
{
public: int data;
Node * next;
Node(int data)
{
//set node value
this->data = data;
this->next = NULL;
}
};
{
public:
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int value)
{
//Create a node
Node * node = new Node(value);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
//Find midular node in given linked list
void find_modular_node(int modular)
{
int counter = 1;
Node * result = NULL;
while (temp != NULL)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp->next;
counter++;
}
if (result == NULL)
{
cout << "\n Modular node [" << modular << "] not exist";
}
else
{
cout << "\n Modular Node [" << modular << "] is " << result->data;
}
}
void display()
{
{
cout << " Linked List :";
while (temp != NULL)
{
cout << " " << temp->data;
temp = temp->next;
}
}
else
{
}
}
};
int main()
{
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
return 0;
}``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Include namespace system
using System;

//C# Program
//Find modular node in a linked list (from end of linked list)

class Node
{
public int data;
public Node next;
public Node(int data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int value)
{
//Create a node
Node node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
//Find midular node in given linked list
public void find_modular_node(int modular)
{
int counter = 1;
Node result = null;
while (temp != null)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp.next;
counter++;
}
if (result == null)
{
Console.Write("\n Modular node [" + modular + "] not exist");
}
else
{
Console.Write("\n Modular Node [" + modular + "] is " + result.data);
}
}
public void display()
{
{
while (temp != null)
{
Console.Write(" " + temp.data);
temp = temp.next;
}
}
else
{
}
}
public static void Main(String[] args)
{
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
}
}``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````<?php
//Php Program
//Find modular node in a linked list (from end of linked list)

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$value)
{
//Create a node
\$node = new Node(\$value);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
//Find midular node in given linked list
public	function find_modular_node(\$modular)
{
\$counter = 1;
\$result = null;
while (\$temp != null)
{
if (\$counter % \$modular == 0)
{
\$result = \$temp;
}
// visit to next node
\$temp = \$temp->next;
\$counter++;
}
if (\$result == null)
{
echo "\n Modular node [". \$modular ."] not exist";
}
else
{
echo "\n Modular Node [". \$modular ."] is ". \$result->data;
}
}
public	function display()
{
{
while (\$temp != null)
{
echo " ". \$temp->data;
\$temp = \$temp->next;
}
}
else
{
}
}
}

function main()
{
\$obj->insert(11);
\$obj->insert(6);
\$obj->insert(2);
\$obj->insert(7);
\$obj->insert(10);
\$obj->insert(5);
\$obj->insert(9);
\$obj->insert(8);
\$obj->insert(1);
//Display all node
\$obj->display();
//Test Case
\$obj->find_modular_node(3);
\$obj->find_modular_node(4);
\$obj->find_modular_node(5);
\$obj->find_modular_node(10);
}
main();``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Node Js Program
//Find modular node in a linked list (from end of linked list)

class Node
{
constructor(data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(value)
{
//Create a node
var node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
//Find midular node in given linked list
find_modular_node(modular)
{
var counter = 1;
var result = null;
while (temp != null)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp.next;
counter++;
}
if (result == null)
{
process.stdout.write("\n Modular node [" + modular + "] not exist");
}
else
{
process.stdout.write("\n Modular Node [" + modular + "] is " + result.data);
}
}
display()
{
{
while (temp != null)
{
process.stdout.write(" " + temp.data);
temp = temp.next;
}
}
else
{
}
}
}

function main()
{
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
}
main();``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````# Python 3 Program
# Find modular node in a linked list (from end of linked list)

class Node :

def __init__(self, data) :
# set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, value) :
# Create a node
node = Node(value)
self.tail = node
else :
self.tail.next = node
self.tail = node

# Find midular node in given linked list
def find_modular_node(self, modular) :
counter = 1
result = None
while (temp != None) :
if (counter % modular == 0) :
result = temp

#  visit to next node
temp = temp.next
counter += 1

if (result == None) :
print("\n Modular node [", modular ,"] not exist", end = "")
else :
print("\n Modular Node [", modular ,"] is ", result.data, end = "")

def display(self) :
print(" Linked List :", end = "")
while (temp != None) :
print(" ", temp.data, end = "")
temp = temp.next

else :
print("Empty Linked List", end = "")

def main() :
obj.insert(11)
obj.insert(6)
obj.insert(2)
obj.insert(7)
obj.insert(10)
obj.insert(5)
obj.insert(9)
obj.insert(8)
obj.insert(1)
# Display all node
obj.display()
# Test Case
obj.find_modular_node(3)
obj.find_modular_node(4)
obj.find_modular_node(5)
obj.find_modular_node(10)

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

#### Output

`````` Linked List :  11  6  2  7  10  5  9  8  1
Modular Node [ 3 ] is  1
Modular Node [ 4 ] is  8
Modular Node [ 5 ] is  10
Modular node [ 10 ] not exist``````
``````# Ruby Program
# Find modular node in a linked list (from end of linked list)

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)
# set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(value)

# Create a node
node = Node.new(value)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
# Find midular node in given linked list
def find_modular_node(modular)

counter = 1
result = nil
while (temp != nil)

if (counter % modular == 0)

result = temp
end
#  visit to next node
temp = temp.next
counter += 1
end
if (result == nil)

print("\n Modular node [", modular ,"] not exist")
else

print("\n Modular Node [", modular ,"] is ", result.data)
end
end
def display()

while (temp != nil)

print(" ", temp.data)
temp = temp.next
end
else

end
end
end
def main()

obj.insert(11)
obj.insert(6)
obj.insert(2)
obj.insert(7)
obj.insert(10)
obj.insert(5)
obj.insert(9)
obj.insert(8)
obj.insert(1)
# Display all node
obj.display()
# Test Case
obj.find_modular_node(3)
obj.find_modular_node(4)
obj.find_modular_node(5)
obj.find_modular_node(10)
end
main()``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Scala Program
//Find modular node in a linked list (from end of linked list)

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(value: Int): Unit = {
//Create a node
var node: Node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
//Find midular node in given linked list
def find_modular_node(modular: Int): Unit = {
var counter: Int = 1;
var result: Node = null;
while (temp != null)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp.next;
counter += 1;
}
if (result == null)
{
print("\n Modular node [" + modular + "] not exist");
}
else
{
print("\n Modular Node [" + modular + "] is " + result.data);
}
}
def display(): Unit = {
{
while (temp != null)
{
print(" " + temp.data);
temp = temp.next;
}
}
else
{
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
}
}``````

#### Output

`````` Linked List : 11 6 2 7 10 5 9 8 1
Modular Node [3] is 1
Modular Node [4] is 8
Modular Node [5] is 10
Modular node [10] not exist``````
``````//Swift Program
//Find modular node in a linked list (from end of linked list)

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ value: Int)
{
//Create a node
let node: Node? = Node(value);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
//Find midular node in given linked list
func find_modular_node(_ modular: Int)
{
var counter: Int = 1;
var result: Node? = nil;
while (temp != nil)
{
if (counter % modular == 0)
{
result = temp;
}
// visit to next node
temp = temp!.next;
counter += 1;
}
if (result == nil)
{
print("\n Modular node [", modular ,"] not exist", terminator: "");
}
else
{
print("\n Modular Node [", modular ,"] is ", result!.data, terminator: "");
}
}
func display()
{
{
print(" Linked List :", terminator: "");
while (temp != nil)
{
print(" ", temp!.data, terminator: "");
temp = temp!.next;
}
}
else
{
}
}
}
func main()
{
obj.insert(11);
obj.insert(6);
obj.insert(2);
obj.insert(7);
obj.insert(10);
obj.insert(5);
obj.insert(9);
obj.insert(8);
obj.insert(1);
//Display all node
obj.display();
//Test Case
obj.find_modular_node(3);
obj.find_modular_node(4);
obj.find_modular_node(5);
obj.find_modular_node(10);
}
main();``````

#### Output

`````` Linked List :  11  6  2  7  10  5  9  8  1
Modular Node [ 3 ] is  1
Modular Node [ 4 ] is  8
Modular Node [ 5 ] is  10
Modular node [ 10 ] not exist``````

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.