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

//Linked List Node
struct Node
{
	int data;
	struct Node *next;
};
//Add new node at end of linked list 
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;
		if (head == NULL)
		{
			return node;
		}
		else
		{
			struct Node *temp = head;
			//find last node
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			//add node at last possition
			temp->next = node;
		}
	}
	return head;
}
//Display element of Node
void display(struct Node *temp)
{
	if (temp == NULL)
	{
		printf("Empty linked list");
	}
	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 *temp = head;
	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()
{
	struct Node *head = NULL;
	//Create linked list
	head = insert(head, 11);
	head = insert(head, 6);
	head = insert(head, 2);
	head = insert(head, 7);
	head = insert(head, 10);
	head = insert(head, 5);
	head = insert(head, 9);
	head = insert(head, 8);
	head = insert(head, 1);
	//Display all node
	display(head);
	//Test Case
	find_modular_node(head, 3);
	find_modular_node(head, 4);
	find_modular_node(head, 5);
	find_modular_node(head, 10);
	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)
//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int value)
	{
		//Create a node
		Node node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			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 temp = this.head;
		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()
	{
		if (this.head != null)
		{
			System.out.print(" Linked List :");
			Node temp = this.head;
			while (temp != null)
			{
				System.out.print(" " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			System.out.println("Empty Linked List");
		}
	}
	public static void main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//Create linked list
		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)

//Node of LinkedList
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		//set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: 
    Node * head;
	Node * tail;
	//Class constructors
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void insert(int value)
	{
		//Create a node
		Node * node = new Node(value);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Find midular node in given linked list
	void find_modular_node(int modular)
	{
		int counter = 1;
		Node * temp = this->head;
		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()
	{
		if (this->head != NULL)
		{
			cout << " Linked List :";
			Node * temp = this->head;
			while (temp != NULL)
			{
				cout << " " << temp->data;
				temp = temp->next;
			}
		}
		else
		{
			cout << "Empty Linked List";
		}
	}
};
int main()
{
	MyLinkedList obj = MyLinkedList();
	//Create linked list
	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)

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int value)
	{
		//Create a node
		Node node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			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 temp = this.head;
		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()
	{
		if (this.head != null)
		{
			Console.Write(" Linked List :");
			Node temp = this.head;
			while (temp != null)
			{
				Console.Write(" " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			Console.WriteLine("Empty Linked List");
		}
	}
	public static void Main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//Create linked list
		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)

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructors
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function insert($value)
	{
		//Create a node
		$node = new Node($value);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Find midular node in given linked list
	public	function find_modular_node($modular)
	{
		$counter = 1;
		$temp = $this->head;
		$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()
	{
		if ($this->head != null)
		{
			echo " Linked List :";
			$temp = $this->head;
			while ($temp != null)
			{
				echo " ". $temp->data;
				$temp = $temp->next;
			}
		}
		else
		{
			echo "Empty Linked List";
		}
	}
}

function main()
{
	$obj = new MyLinkedList();
	//Create linked list
	$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)

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructors
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	insert(value)
	{
		//Create a node
		var node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Find midular node in given linked list
	find_modular_node(modular)
	{
		var counter = 1;
		var temp = this.head;
		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()
	{
		if (this.head != null)
		{
			process.stdout.write(" Linked List :");
			var temp = this.head;
			while (temp != null)
			{
				process.stdout.write(" " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			process.stdout.write("Empty Linked List");
		}
	}
}

function main()
{
	var obj = new MyLinkedList();
	//Create linked list
	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)

# Node of LinkedList
class Node :
	
	def __init__(self, data) :
		# set node value
		self.data = data
		self.next = None
	
class MyLinkedList :
	
	# Class constructors
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def insert(self, value) :
		# Create a node
		node = Node(value)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Find midular node in given linked list
	def find_modular_node(self, modular) :
		counter = 1
		temp = self.head
		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) :
		if (self.head != None) :
			print(" Linked List :", end = "")
			temp = self.head
			while (temp != None) :
				print(" ", temp.data, end = "")
				temp = temp.next
			
		else :
			print("Empty Linked List", end = "")
		
	

def main() :
	obj = MyLinkedList()
	# Create linked list
	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)

# Node of LinkedList
class Node 

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

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

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail


	
	# Class constructors
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def insert(value)
	
		# Create a node
		node = Node.new(value)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Find midular node in given linked list
	def find_modular_node(modular)
	
		counter = 1
		temp = self.head
		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()
	
		if (self.head != nil)
		
			print(" Linked List :")
			temp = self.head
			while (temp != nil)
			
				print(" ", temp.data)
				temp = temp.next
			end
		else
		
			print("Empty Linked List")
		end
	end
end
def main()

	obj = MyLinkedList.new()
	# Create linked list
	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)

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	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);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			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 temp: Node = this.head;
		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 = {
		if (this.head != null)
		{
			print(" Linked List :");
			var temp: Node = this.head;
			while (temp != null)
			{
				print(" " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			print("Empty Linked List");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyLinkedList = new MyLinkedList();
		//Create linked list
		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)

//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructors
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func insert(_ value: Int)
	{
		//Create a node
		let node: Node? = Node(value);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			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 temp: Node? = self.head;
		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()
	{
		if (self.head != nil)
		{
			print(" Linked List :", terminator: "");
			var temp: Node? = self.head;
			while (temp != nil)
			{
				print(" ", temp!.data, terminator: "");
				temp = temp!.next;
			}
		}
		else
		{
			print("Empty Linked List", terminator: "");
		}
	}
}
func main()
{
	let obj: MyLinkedList = MyLinkedList();
	//Create linked list
	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.

New Comment







© 2021, kalkicode.com, All rights reserved