Skip to main content

Find fractional node in linked list

Here given code implementation process.

// C Program
// Find fractional node in linked list
#include <stdio.h>
 //for malloc function
#include <stdlib.h>

//Create structure
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
struct Node *tail = NULL;
//Add new node at end of linked list 
void insert(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)
		{
			head = node;
			tail = node;
		}
		else
		{
			tail->next = node;
			tail = node;
		}
	}
}
//Display linked list element
void display()
{
	if (head == NULL)
	{
		printf("Empty linked list");
		return;
	}
	struct Node *temp = head;
	printf("\n Linked List :");
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		temp = temp->next;
		if (temp == head)
		{
			//When loop existing
			return;
		}
	}
}
//Find fractional node using k value
void find_fractional_node(int k)
{
	if (head == NULL)
	{
		printf("\nFractional of divisible %d are not exist", k);
		//linked list are empty
		return;
	}
	struct Node *temp = head;
	//Define Useful resultant variable
	struct Node *result = NULL;
	int counter = 0;
	//Sum of all nodes
	while (temp != NULL)
	{
		if (counter % k == 0)
		{
			if (result == NULL)
			{
				//Get first node of linked list
				result = head;
			}
			else
			{
				//visit to next node
				result = result->next;
			}
		}
		//visit to next node
		temp = temp->next;
		counter++;
	}
	printf("\n Fractional of divisible %d is : %d", k, result->data);
}
int main()
{
	//insert element of linked list
	insert(8);
	insert(2);
	insert(9);
	insert(4);
	insert(7);
	insert(5);
	insert(3);
	//Display linked list
	display();
	//Fractional_node
	find_fractional_node(2);
	find_fractional_node(3);
	find_fractional_node(11);
	return 0;
}

Output

 Linked List :  8  2  9  4  7  5  3
 Fractional of divisible 2 is : 4
 Fractional of divisible 3 is : 9
 Fractional of divisible 11 is : 8
// Java Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	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;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			System.out.println("Empty Linked List");
		}
	}
	//Find fractional node using k value
	public void find_fractional_node(int k)
	{
		if (this.head == null)
		{
			System.out.print("\nFractional node of divisible " + k + " are not exist");
			//linked list are empty
			return;
		}
		Node temp = this.head;
		//Define Useful resultant variable
		Node result = null;
		//Counter 
		int counter = 0;
		while (temp != null)
		{
			if (counter % k == 0)
			{
				if (result == null)
				{
					//Get first node of linked list
					result = head;
				}
				else
				{
					//visit to next node
					result = result.next;
				}
			}
			//visit to next node
			temp = temp.next;
			counter++;
		}
		System.out.print("\n Fractional node of divisible " + k + " are : " + result.data);
	}
	public static void main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//insert element of linked list
		obj.insert(8);
		obj.insert(2);
		obj.insert(9);
		obj.insert(4);
		obj.insert(7);
		obj.insert(5);
		obj.insert(3);
		//Display linked list
		obj.display();
		//Fractional_node
		obj.find_fractional_node(2);
		obj.find_fractional_node(3);
		obj.find_fractional_node(11);
	}
}

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	void display()
	{
		if (this->head != NULL)
		{
			cout << " Linked List :";
			Node * temp = this->head;
			while (temp != NULL)
			{
				cout << " " << temp->data;
				temp = temp->next;
				if (temp == this->head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			cout << "Empty Linked List";
		}
	}
	//Find fractional node using k value
	void find_fractional_node(int k)
	{
		if (this->head == NULL)
		{
			//linked list are empty
			cout << "\nFractional node of divisible " << k << " are not exist";
			return;
		}
		Node * temp = this->head;
		//Define Useful resultant variable
		Node * result = NULL;
		//Counter 
		int counter = 0;
		while (temp != NULL)
		{
			if (counter % k == 0)
			{
				if (result == NULL)
				{
					//Get first node of linked list
					result = this->head;
				}
				else
				{
					//visit to next node
					result = result->next;
				}
			}
			//visit to next node
			temp = temp->next;
			counter++;
		}
		cout << "\n Fractional node of divisible " << k << " are : " << result->data;
	}
};
int main()
{
	MyLinkedList obj = MyLinkedList();
	//insert element of linked list
	obj.insert(8);
	obj.insert(2);
	obj.insert(9);
	obj.insert(4);
	obj.insert(7);
	obj.insert(5);
	obj.insert(3);
	//Display linked list
	obj.display();
	//Fractional_node
	obj.find_fractional_node(2);
	obj.find_fractional_node(3);
	obj.find_fractional_node(11);
	return 0;
}

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
//Include namespace system
using System;

// C# Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	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;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			Console.WriteLine("Empty Linked List");
		}
	}
	//Find fractional node using k value
	public void find_fractional_node(int k)
	{
		if (this.head == null)
		{
			//linked list are empty
			Console.Write("\nFractional node of divisible " + k + " are not exist");
			return;
		}
		Node temp = this.head;
		//Define Useful resultant variable
		Node result = null;
		//Counter 
		int counter = 0;
		while (temp != null)
		{
			if (counter % k == 0)
			{
				if (result == null)
				{
					//Get first node of linked list
					result = head;
				}
				else
				{
					//visit to next node
					result = result.next;
				}
			}
			//visit to next node
			temp = temp.next;
			counter++;
		}
		Console.Write("\n Fractional node of divisible " + k + " are : " + result.data);
	}
	public static void Main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//insert element of linked list
		obj.insert(8);
		obj.insert(2);
		obj.insert(9);
		obj.insert(4);
		obj.insert(7);
		obj.insert(5);
		obj.insert(3);
		//Display linked list
		obj.display();
		//Fractional_node
		obj.find_fractional_node(2);
		obj.find_fractional_node(3);
		obj.find_fractional_node(11);
	}
}

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
<?php
// Php Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	public	function display()
	{
		if ($this->head != null)
		{
			echo " Linked List :";
			$temp = $this->head;
			while ($temp != null)
			{
				echo " ". $temp->data;
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			echo "Empty Linked List";
		}
	}
	//Find fractional node using k value
	public	function find_fractional_node($k)
	{
		if ($this->head == null)
		{
			//linked list are empty
			echo "\nFractional node of divisible ". $k ." are not exist";
			return;
		}
		$temp = $this->head;
		//Define Useful resultant variable
		$result = null;
		//Counter 
		$counter = 0;
		while ($temp != null)
		{
			if ($counter % $k == 0)
			{
				if ($result == null)
				{
					//Get first node of linked list
					$result = $this->head;
				}
				else
				{
					//visit to next node
					$result = $result->next;
				}
			}
			//visit to next node
			$temp = $temp->next;
			$counter++;
		}
		echo "\n Fractional node of divisible ". $k ." are : ". $result->data;
	}
}

function main()
{
	$obj = new MyLinkedList();
	//insert element of linked list
	$obj->insert(8);
	$obj->insert(2);
	$obj->insert(9);
	$obj->insert(4);
	$obj->insert(7);
	$obj->insert(5);
	$obj->insert(3);
	//Display linked list
	$obj->display();
	//Fractional_node
	$obj->find_fractional_node(2);
	$obj->find_fractional_node(3);
	$obj->find_fractional_node(11);
}
main();

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
// Node Js Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	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;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			process.stdout.write("Empty Linked List");
		}
	}
	//Find fractional node using k value
	find_fractional_node(k)
	{
		if (this.head == null)
		{
			//linked list are empty
			process.stdout.write("\nFractional node of divisible " + k + " are not exist");
			return;
		}
		var temp = this.head;
		//Define Useful resultant variable
		var result = null;
		//Counter 
		var counter = 0;
		while (temp != null)
		{
			if (counter % k == 0)
			{
				if (result == null)
				{
					//Get first node of linked list
					result = this.head;
				}
				else
				{
					//visit to next node
					result = result.next;
				}
			}
			//visit to next node
			temp = temp.next;
			counter++;
		}
		process.stdout.write("\n Fractional node of divisible " + k + " are : " + result.data);
	}
}

function main()
{
	var obj = new MyLinkedList();
	//insert element of linked list
	obj.insert(8);
	obj.insert(2);
	obj.insert(9);
	obj.insert(4);
	obj.insert(7);
	obj.insert(5);
	obj.insert(3);
	//Display linked list
	obj.display();
	//Fractional_node
	obj.find_fractional_node(2);
	obj.find_fractional_node(3);
	obj.find_fractional_node(11);
}
main();

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
#  Python 3 Program
#  Find fractional node in 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
		
	
	# Display linked list nodes
	def display(self) :
		if (self.head != None) :
			print(" Linked List :", end = "")
			temp = self.head
			while (temp != None) :
				print(" ", temp.data, end = "")
				temp = temp.next
				if (temp == self.head) :
					# avoid loop
					return
				
			
		else :
			print("Empty Linked List", end = "")
		
	
	# Find fractional node using k value
	def find_fractional_node(self, k) :
		if (self.head == None) :
			# linked list are empty
			print("\nFractional node of divisible ", k ," are not exist", end = "")
			return
		
		temp = self.head
		# Define Useful resultant variable
		result = None
		# Counter 
		counter = 0
		while (temp != None) :
			if (counter % k == 0) :
				if (result == None) :
					# Get first node of linked list
					result = self.head
				else :
					# visit to next node
					result = result.next
				
			
			# visit to next node
			temp = temp.next
			counter += 1
		
		print("\n Fractional node of divisible ", k ," are : ", result.data, end = "")
	

def main() :
	obj = MyLinkedList()
	# insert element of linked list
	obj.insert(8)
	obj.insert(2)
	obj.insert(9)
	obj.insert(4)
	obj.insert(7)
	obj.insert(5)
	obj.insert(3)
	# Display linked list
	obj.display()
	# Fractional_node
	obj.find_fractional_node(2)
	obj.find_fractional_node(3)
	obj.find_fractional_node(11)

if __name__ == "__main__": main()

Output

 Linked List :  8  2  9  4  7  5  3
 Fractional node of divisible  2  are :  4
 Fractional node of divisible  3  are :  9
 Fractional node of divisible  11  are :  8
#  Ruby Program
#  Find fractional node in 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
	# Display linked list nodes
	def display()
	
		if (self.head != nil)
		
			print(" Linked List :")
			temp = self.head
			while (temp != nil)
			
				print(" ", temp.data)
				temp = temp.next
				if (temp == self.head)
				
					# avoid loop
					return
				end
			end
		else
		
			print("Empty Linked List")
		end
	end
	# Find fractional node using k value
	def find_fractional_node(k)
	
		if (self.head == nil)
		
			# linked list are empty
			print("\nFractional node of divisible ", k ," are not exist")
			return
		end
		temp = self.head
		# Define Useful resultant variable
		result = nil
		# Counter 
		counter = 0
		while (temp != nil)
		
			if (counter % k == 0)
			
				if (result == nil)
				
					# Get first node of linked list
					result = @head
				else
				
					# visit to next node
					result = result.next
				end
			end
			# visit to next node
			temp = temp.next
			counter += 1
		end
		print("\n Fractional node of divisible ", k ," are : ", result.data)
	end
end
def main()

	obj = MyLinkedList.new()
	# insert element of linked list
	obj.insert(8)
	obj.insert(2)
	obj.insert(9)
	obj.insert(4)
	obj.insert(7)
	obj.insert(5)
	obj.insert(3)
	# Display linked list
	obj.display()
	# Fractional_node
	obj.find_fractional_node(2)
	obj.find_fractional_node(3)
	obj.find_fractional_node(11)
end
main()

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
// Scala Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	def display(): Unit = {
		if (this.head != null)
		{
			print(" Linked List :");
			var temp: Node = this.head;
			while (temp != null)
			{
				print(" " + temp.data);
				temp = temp.next;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			print("Empty Linked List");
		}
	}
	//Find fractional node using k value
	def find_fractional_node(k: Int): Unit = {
		if (this.head == null)
		{
			//linked list are empty
			print("\nFractional node of divisible " + k + " are not exist");
			return;
		}
		var temp: Node = this.head;
		//Define Useful resultant variable
		var result: Node = null;
		//Counter 
		var counter: Int = 0;
		while (temp != null)
		{
			if (counter % k == 0)
			{
				if (result == null)
				{
					//Get first node of linked list
					result = head;
				}
				else
				{
					//visit to next node
					result = result.next;
				}
			}
			//visit to next node
			temp = temp.next;
			counter += 1;
		}
		print("\n Fractional node of divisible " + k + " are : " + result.data);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyLinkedList = new MyLinkedList();
		//insert element of linked list
		obj.insert(8);
		obj.insert(2);
		obj.insert(9);
		obj.insert(4);
		obj.insert(7);
		obj.insert(5);
		obj.insert(3);
		//Display linked list
		obj.display();
		//Fractional_node
		obj.find_fractional_node(2);
		obj.find_fractional_node(3);
		obj.find_fractional_node(11);
	}
}

Output

 Linked List : 8 2 9 4 7 5 3
 Fractional node of divisible 2 are : 4
 Fractional node of divisible 3 are : 9
 Fractional node of divisible 11 are : 8
// Swift Program
// Find fractional node in 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;
		}
	}
	//Display linked list nodes
	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;
				if (temp === self.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			print("Empty Linked List", terminator: "");
		}
	}
	//Find fractional node using k value
	func find_fractional_node(_ k: Int)
	{
		if (self.head == nil)
		{
			//linked list are empty
			print("\nFractional node of divisible ", k ," are not exist", terminator: "");
			return;
		}
		var temp: Node? = self.head;
		//Define Useful resultant variable
		var result: Node? = nil;
		//Counter 
		var counter: Int = 0;
		while (temp != nil)
		{
			if (counter % k == 0)
			{
				if (result == nil)
				{
					//Get first node of linked list
					result = self.head;
				}
				else
				{
					//visit to next node
					result = result!.next;
				}
			}
			//visit to next node
			temp = temp!.next;
			counter += 1;
		}
		print("\n Fractional node of divisible ", k ," are : ", result!.data, terminator: "");
	}
}
func main()
{
	let obj: MyLinkedList = MyLinkedList();
	//insert element of linked list
	obj.insert(8);
	obj.insert(2);
	obj.insert(9);
	obj.insert(4);
	obj.insert(7);
	obj.insert(5);
	obj.insert(3);
	//Display linked list
	obj.display();
	//Fractional_node
	obj.find_fractional_node(2);
	obj.find_fractional_node(3);
	obj.find_fractional_node(11);
}
main();

Output

 Linked List :  8  2  9  4  7  5  3
 Fractional node of divisible  2  are :  4
 Fractional node of divisible  3  are :  9
 Fractional node of divisible  11  are :  8




Comment

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