Print the nodes of linked list at given indexes

Given a collection of sorted index, Our goal is to find and print all nodes on linked list which is exist on given index. For example.

 Example 1
 Input List : 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index List : 2 → 4 → 7 → NULL
 Output     : 8   3   10 [value at index]

 Example 2
 Input List : 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index List : 3 → 5 → 6 → 8 → NULL
 Output     : 7  8  6  19

This problem are based on traversal of linked list.

// C Program 
// Print the nodes of linked list at given indexes
#include <stdio.h>
#include <stdlib.h> //for malloc function

// Linked List LinkNode
struct LinkNode
{
	int data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
	// Create memory of head and tail Nodes
	struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
	if (sll == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		sll->head = NULL;
		sll->tail = NULL;
	}
	return sll;
}
// Add new Node at end of linked list 
void appendNode(struct SingleLL *sll, struct LinkNode *node)
{
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	else
	{
		// Append the node at last position
		sll->tail->next = node;
	}
	sll->tail = node;
}
// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
	// Create dynamic node
	struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow to Create LinkNode\n");
		return;
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	appendNode(sll, node);
}
// Display linked list element
void display(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	struct LinkNode *temp = sll->head;
	// iterating linked list elements
	while (temp != NULL)
	{
		printf(" %d →", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf(" NULL\n");
}
// Print the node value at given indexes
void printIndexValue(struct SingleLL *sll1, struct SingleLL *sll2)
{
	if (sll1->head == NULL || sll2->head == NULL)
	{
		return;
	}
	// Display linked list
	printf(" Linked List \n");
	display(sll2);
	// Display index value
	printf(" Index  \n");
	display(sll1);
	printf("\n Index Value \n");
	// Get first node of both linked list
	struct LinkNode *key = sll1->head;
	struct LinkNode *value = sll2->head;
	// Define counter variable
	int counter = 1;
	int status = 0;
	// iterating linked list elements
	while (key != NULL && value != NULL)
	{
		if (counter == key->data)
		{
			status = 1;
			printf("  %d", value->data);
			key = key->next;
		}
		// Visit to next node
		value = value->next;
		counter++;
	}
	if (status == 0)
	{
		// When no index element not present
		printf(" None \n");
	}
}
int main()
{
	// Define linked list
	struct SingleLL *sll1 = newLinkedList();
	struct SingleLL *sll2 = newLinkedList();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	addNode(sll1, 3);
	addNode(sll1, 5);
	addNode(sll1, 6);
	addNode(sll1, 8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	addNode(sll2, 4);
	addNode(sll2, 8);
	addNode(sll2, 7);
	addNode(sll2, 3);
	addNode(sll2, 8);
	addNode(sll2, 6);
	addNode(sll2, 10);
	addNode(sll2, 19);
	addNode(sll2, -5);
	printIndexValue(sll1, sll2);
	return 0;
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
  7  8  6  19
/*
  Java Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" NULL\n");
	}
	// Print the node value at given indexes
	public void printIndexValue(SingleLL values)
	{
		if (this.head == null || values.head == null)
		{
			return;
		}
		// Display linked list
		System.out.print(" Linked List \n");
		values.display();
		// Display index value
		System.out.print(" Index \n");
		this.display();
		System.out.print("\n Index Value \n");
		// Get first node of both linked list
		LinkNode key = this.head;
		LinkNode value = values.head;
		// Define counter variable
		int counter = 1;
		boolean status = false;
		// iterating linked list elements
		while (key != null && value != null)
		{
			if (counter == key.data)
			{
				status = true;
				System.out.print(" " + value.data);
				key = key.next;
			}
			// Visit to next node
			value = value.next;
			counter++;
		}
		if (status == false)
		{
			// When no index element not present
			System.out.print(" None \n");
		}
	}
	public static void main(String[] args)
	{
		SingleLL keys = new SingleLL();
		SingleLL values = new SingleLL();
		// Given sorted index
		// 3 → 5 → 6 → 8 → NULL
		keys.addNode(3);
		keys.addNode(5);
		keys.addNode(6);
		keys.addNode(8);
		// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
		values.addNode(4);
		values.addNode(8);
		values.addNode(7);
		values.addNode(3);
		values.addNode(8);
		values.addNode(6);
		values.addNode(10);
		values.addNode(19);
		values.addNode(-5);
		// Find key value
		keys.printIndexValue(values);
	}
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	// Add new Node at end of linked list
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// Print the node value at given indexes
	void printIndexValue(SingleLL values)
	{
		if (this->head == NULL || values.head == NULL)
		{
			return;
		}
		// Display linked list
		cout << " Linked List \n";
		values.display();
		// Display index value
		cout << " Index \n";
		this->display();
		cout << "\n Index Value \n";
		// Get first node of both linked list
		LinkNode *key = this->head;
		LinkNode *value = values.head;
		// Define counter variable
		int counter = 1;
		bool status = false;
		// iterating linked list elements
		while (key != NULL && value != NULL)
		{
			if (counter == key->data)
			{
				status = true;
				cout << " " << value->data;
				key = key->next;
			}
			// Visit to next node
			value = value->next;
			counter++;
		}
		if (status == false)
		{
			// When no index element not present
			cout << " None \n";
		}
	}
};
int main()
{
	SingleLL keys = SingleLL();
	SingleLL values = SingleLL();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	keys.addNode(3);
	keys.addNode(5);
	keys.addNode(6);
	keys.addNode(8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4);
	values.addNode(8);
	values.addNode(7);
	values.addNode(3);
	values.addNode(8);
	values.addNode(6);
	values.addNode(10);
	values.addNode(19);
	values.addNode(-5);
	// Find key value
	keys.printIndexValue(values);
	return 0;
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
// Include namespace system
using System;
/*
  C# Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" NULL\n");
	}
	// Print the node value at given indexes
	public void printIndexValue(SingleLL values)
	{
		if (this.head == null || values.head == null)
		{
			return;
		}
		// Display linked list
		Console.Write(" Linked List \n");
		values.display();
		// Display index value
		Console.Write(" Index \n");
		this.display();
		Console.Write("\n Index Value \n");
		// Get first node of both linked list
		LinkNode key = this.head;
		LinkNode value = values.head;
		// Define counter variable
		int counter = 1;
		Boolean status = false;
		// iterating linked list elements
		while (key != null && value != null)
		{
			if (counter == key.data)
			{
				status = true;
				Console.Write(" " + value.data);
				key = key.next;
			}
			// Visit to next node
			value = value.next;
			counter++;
		}
		if (status == false)
		{
			// When no index element not present
			Console.Write(" None \n");
		}
	}
	public static void Main(String[] args)
	{
		SingleLL keys = new SingleLL();
		SingleLL values = new SingleLL();
		// Given sorted index
		// 3 → 5 → 6 → 8 → NULL
		keys.addNode(3);
		keys.addNode(5);
		keys.addNode(6);
		keys.addNode(8);
		// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
		values.addNode(4);
		values.addNode(8);
		values.addNode(7);
		values.addNode(3);
		values.addNode(8);
		values.addNode(6);
		values.addNode(10);
		values.addNode(19);
		values.addNode(-5);
		// Find key value
		keys.printIndexValue(values);
	}
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
<?php
/*
  Php Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
}
class SingleLL
{
	public $head;
	public $tail;

	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	// Add new Node at end of linked list
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			echo " ". $temp->data ." →";
			// Visit to next node
			$temp = $temp->next;
		}
		echo " NULL\n";
	}
	// Print the node value at given indexes
	public	function printIndexValue($values)
	{
		if ($this->head == null || $values->head == null)
		{
			return;
		}
		// Display linked list
		echo " Linked List \n";
		$values->display();
		// Display index value
		echo " Index \n";
		$this->display();
		echo "\n Index Value \n";
		// Get first node of both linked list
		$key = $this->head;
		$value = $values->head;
		// Define counter variable
		$counter = 1;
		$status = false;
		// iterating linked list elements
		while ($key != null && $value != null)
		{
			if ($counter == $key->data)
			{
				$status = true;
				echo " ". $value->data;
				$key = $key->next;
			}
			// Visit to next node
			$value = $value->next;
			$counter++;
		}
		if ($status == false)
		{
			// When no index element not present
			echo " None \n";
		}
	}
}

function main()
{
	$keys = new SingleLL();
	$values = new SingleLL();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	$keys->addNode(3);
	$keys->addNode(5);
	$keys->addNode(6);
	$keys->addNode(8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	$values->addNode(4);
	$values->addNode(8);
	$values->addNode(7);
	$values->addNode(3);
	$values->addNode(8);
	$values->addNode(6);
	$values->addNode(10);
	$values->addNode(19);
	$values->addNode(-5);
	// Find key value
	$keys->printIndexValue($values);
}
main();

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
/*
  Node Js Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	// Print the node value at given indexes
	printIndexValue(values)
	{
		if (this.head == null || values.head == null)
		{
			return;
		}
		// Display linked list
		process.stdout.write(" Linked List \n");
		values.display();
		// Display index value
		process.stdout.write(" Index \n");
		this.display();
		process.stdout.write("\n Index Value \n");
		// Get first node of both linked list
		var key = this.head;
		var value = values.head;
		// Define counter variable
		var counter = 1;
		var status = false;
		// iterating linked list elements
		while (key != null && value != null)
		{
			if (counter == key.data)
			{
				status = true;
				process.stdout.write(" " + value.data);
				key = key.next;
			}
			// Visit to next node
			value = value.next;
			counter++;
		}
		if (status == false)
		{
			// When no index element not present
			process.stdout.write(" None \n");
		}
	}
}

function main()
{
	var keys = new SingleLL();
	var values = new SingleLL();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	keys.addNode(3);
	keys.addNode(5);
	keys.addNode(6);
	keys.addNode(8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4);
	values.addNode(8);
	values.addNode(7);
	values.addNode(3);
	values.addNode(8);
	values.addNode(6);
	values.addNode(10);
	values.addNode(19);
	values.addNode(-5);
	// Find key value
	keys.printIndexValue(values);
}
main();

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
#   Python 3 Program 
#   Print the nodes of linked list at given indexes

#  Linked list node
class LinkNode :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			#  Append the node at last position
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  Print the node value at given indexes
	def printIndexValue(self, values) :
		if (self.head == None or values.head == None) :
			return
		
		#  Display linked list
		print(" Linked List ")
		values.display()
		#  Display index value
		print(" Index ")
		self.display()
		print("\n Index Value ")
		#  Get first node of both linked list
		key = self.head
		value = values.head
		#  Define counter variable
		counter = 1
		status = False
		#  iterating linked list elements
		while (key != None and value != None) :
			if (counter == key.data) :
				status = True
				print(" ", value.data, end = "")
				key = key.next
			
			#  Visit to next node
			value = value.next
			counter += 1
		
		if (status == False) :
			#  When no index element not present
			print(" None ")
		
	

def main() :
	keys = SingleLL()
	values = SingleLL()
	#  Given sorted index
	#  3 → 5 → 6 → 8 → NULL
	keys.addNode(3)
	keys.addNode(5)
	keys.addNode(6)
	keys.addNode(8)
	#  4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4)
	values.addNode(8)
	values.addNode(7)
	values.addNode(3)
	values.addNode(8)
	values.addNode(6)
	values.addNode(10)
	values.addNode(19)
	values.addNode(-5)
	#  Find key value
	keys.printIndexValue(values)

if __name__ == "__main__": main()

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
  7  8  6  19
#   Ruby Program 
#   Print the nodes of linked list at given indexes

#  Linked list node
class LinkNode  
	# Define the accessor and reader of class LinkNode  
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL  
	# Define the accessor and reader of class SingleLL  
	attr_reader :head, :tail
	attr_accessor :head, :tail
 
	
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	#  Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

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

		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL\n")
	end

	#  Print the node value at given indexes
	def printIndexValue(values) 
		if (self.head == nil || values.head == nil) 
			return
		end

		#  Display linked list
		print(" Linked List \n")
		values.display()
		#  Display index value
		print(" Index \n")
		self.display()
		print("\n Index Value \n")
		#  Get first node of both linked list
		key = self.head
		value = values.head
		#  Define counter variable
		counter = 1
		status = false
		#  iterating linked list elements
		while (key != nil && value != nil) 
			if (counter == key.data) 
				status = true
				print(" ", value.data)
				key = key.next
			end

			#  Visit to next node
			value = value.next
			counter += 1
		end

		if (status == false) 
			#  When no index element not present
			print(" None \n")
		end

	end

end

def main() 
	keys = SingleLL.new()
	values = SingleLL.new()
	#  Given sorted index
	#  3 → 5 → 6 → 8 → NULL
	keys.addNode(3)
	keys.addNode(5)
	keys.addNode(6)
	keys.addNode(8)
	#  4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4)
	values.addNode(8)
	values.addNode(7)
	values.addNode(3)
	values.addNode(8)
	values.addNode(6)
	values.addNode(10)
	values.addNode(19)
	values.addNode(-5)
	#  Find key value
	keys.printIndexValue(values)
end

main()

Output

 Linked List 
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index 
 3 → 5 → 6 → 8 → NULL

 Index Value 
 7 8 6 19
/*
  Scala Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
	def this()
	{
		this(null, null);
	}
	// Add new Node at end of linked list
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Print the node value at given indexes
	def printIndexValue(values: SingleLL): Unit = {
		if (this.head == null || values.head == null)
		{
			return;
		}
		// Display linked list
		print(" Linked List \n");
		values.display();
		// Display index value
		print(" Index \n");
		this.display();
		print("\n Index Value \n");
		// Get first node of both linked list
		var key: LinkNode = this.head;
		var value: LinkNode = values.head;
		// Define counter variable
		var counter: Int = 1;
		var status: Boolean = false;
		// iterating linked list elements
		while (key != null && value != null)
		{
			if (counter == key.data)
			{
				status = true;
				print(" " + value.data);
				key = key.next;
			}
			// Visit to next node
			value = value.next;
			counter += 1;
		}
		if (status == false)
		{
			// When no index element not present
			print(" None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var keys: SingleLL = new SingleLL();
		var values: SingleLL = new SingleLL();
		// Given sorted index
		// 3 → 5 → 6 → 8 → NULL
		keys.addNode(3);
		keys.addNode(5);
		keys.addNode(6);
		keys.addNode(8);
		// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
		values.addNode(4);
		values.addNode(8);
		values.addNode(7);
		values.addNode(3);
		values.addNode(8);
		values.addNode(6);
		values.addNode(10);
		values.addNode(19);
		values.addNode(-5);
		// Find key value
		keys.printIndexValue(values);
	}
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19
/*
  Swift 4 Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		//iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// Print the node value at given indexes
	func printIndexValue(_ values: SingleLL)
	{
		if (self.head == nil || values.head == nil)
		{
			return;
		}
		// Display linked list
		print(" Linked List ");
		values.display();
		// Display index value
		print(" Index ");
		self.display();
		print("\n Index Value ");
		// Get first node of both linked list
		var key: LinkNode? = self.head;
		var value: LinkNode? = values.head;
		// Define counter variable
		var counter: Int = 1;
		var status: Bool = false;
		// iterating linked list elements
		while (key  != nil && value  != nil)
		{
			if (counter == key!.data)
			{
				status = true;
				print(" ", value!.data, terminator: "");
				key = key!.next;
			}
			// Visit to next node
			value = value!.next;
			counter += 1;
		}
		if (status == false)
		{
			// When no index element not present
			print(" None ");
		}
	}
}
func main()
{
	let keys: SingleLL = SingleLL();
	let values: SingleLL = SingleLL();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	keys.addNode(3);
	keys.addNode(5);
	keys.addNode(6);
	keys.addNode(8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4);
	values.addNode(8);
	values.addNode(7);
	values.addNode(3);
	values.addNode(8);
	values.addNode(6);
	values.addNode(10);
	values.addNode(19);
	values.addNode(-5);
	// Find key value
	keys.printIndexValue(values);
}
main();

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
  7  8  6  19
/*
  Kotlin Program 
  Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	fun addNode(data: Int): Unit
	{
		var node: LinkNode = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Print the node value at given indexes
	fun printIndexValue(values: SingleLL ): Unit
	{
		if (this.head == null || values.head == null)
		{
			return;
		}
		// Display linked list
		print(" Linked List \n");
		values.display();
		// Display index value
		print(" Index \n");
		this.display();
		print("\n Index Value \n");
		// Get first node of both linked list
		var key: LinkNode ? = this.head;
		var value: LinkNode ? = values.head;
		// Define counter variable
		var counter: Int = 1;
		var status: Boolean = false;
		// iterating linked list elements
		while (key != null && value != null)
		{
			if (counter == key.data)
			{
				status = true;
				print(" " + value.data);
				key = key.next;
			}
			// Visit to next node
			value = value.next;
			counter += 1;
		}
		if (status == false)
		{
			// When no index element not present
			print(" None \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var keys: SingleLL = SingleLL();
	var values: SingleLL = SingleLL();
	// Given sorted index
	// 3 → 5 → 6 → 8 → NULL
	keys.addNode(3);
	keys.addNode(5);
	keys.addNode(6);
	keys.addNode(8);
	// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
	values.addNode(4);
	values.addNode(8);
	values.addNode(7);
	values.addNode(3);
	values.addNode(8);
	values.addNode(6);
	values.addNode(10);
	values.addNode(19);
	values.addNode(-5);
	// Find key value
	keys.printIndexValue(values);
}

Output

 Linked List
 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
 Index
 3 → 5 → 6 → 8 → NULL

 Index Value
 7 8 6 19

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