Search an element in circular singly linked list

Here given code implementation process.

// Java Program 
// Search an element in circular singly linked list
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class CircularLinkedList
{
	public LinkNode head;
	public LinkNode tail;
	public CircularLinkedList()
	{
		// Set initial value
		this.head = null;
		this.tail = null;
	}
	public void insert(int value)
	{
		LinkNode node = new LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	public void display()
	{
		if (this.head == null)
		{
			System.out.println(" Empty Linked List");
		}
		else
		{
			System.out.print("\n Linked List Element \n");
			// First node of linked list
			System.out.print("  " + this.head.data);
			LinkNode temp = this.head.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				System.out.print("  " + temp.data);
				// visit to next node
				temp = temp.next;
			}
		}
	}
	public void searchElement(int value)
	{
		if (this.head == null)
		{
			System.out.println(" Empty Linked List");
		}
		else
		{
			LinkNode temp = this.head;
			// Find node
			while (temp != null)
			{
				if (temp.data == value)
				{
					System.out.print("\n Node value " + 
                                     value + " exist ");
					return;
				}
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When node are not exist
					System.out.print("\n Node value " + 
                                     value + " are not exist ");
					return;
				}
			}
		}
	}
	public static void main(String[] args)
	{
		CircularLinkedList cll = new CircularLinkedList();
		// First linked list
		cll.insert(15);
		cll.insert(5);
		cll.insert(7);
		cll.insert(4);
		cll.insert(63);
		cll.insert(11);
		cll.insert(17);
		cll.display();
		// Test
		cll.searchElement(4);
		cll.searchElement(34);
		cll.searchElement(11);
	}
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Search an element in circular singly linked list
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class CircularLinkedList
{
	public: LinkNode *head;
	LinkNode *tail;
	CircularLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	void insert(int value)
	{
		LinkNode *node = new LinkNode(value);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			this->tail->next = node;
		}
		node->next = this->head;
		this->tail = node;
	}
	// Display node element of circular linked list
	void display()
	{
		if (this->head == NULL)
		{
			cout << " Empty Linked List" << endl;
		}
		else
		{
			cout << "\n Linked List Element \n";
			// First node of linked list
			cout << "  " << this->head->data;
			LinkNode *temp = this->head->next;
			// Display linked list node
			while (temp != NULL && temp != this->head)
			{
				// Display node value
				cout << "  " << temp->data;
				// visit to next node
				temp = temp->next;
			}
		}
	}
	void searchElement(int value)
	{
		if (this->head == NULL)
		{
			cout << " Empty Linked List" << endl;
		}
		else
		{
			LinkNode *temp = this->head;
			// Find node
			while (temp != NULL)
			{
				if (temp->data == value)
				{
					cout << "\n Node value " 
                  		 << value << " exist ";
					return;
				}
				// Visit to next node
				temp = temp->next;
				if (temp == this->head)
				{
					// When node are not exist
					cout << "\n Node value " 
                  		 << value << " are not exist ";
					return;
				}
			}
		}
	}
};
int main()
{
	CircularLinkedList *cll = new CircularLinkedList();
	// First linked list
	cll->insert(15);
	cll->insert(5);
	cll->insert(7);
	cll->insert(4);
	cll->insert(63);
	cll->insert(11);
	cll->insert(17);
	cll->display();
	// Test
	cll->searchElement(4);
	cll->searchElement(34);
	cll->searchElement(11);
	return 0;
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
// Include namespace system
using System;
// Csharp Program 
// Search an element in circular singly linked list
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class CircularLinkedList
{
	public LinkNode head;
	public LinkNode tail;
	public CircularLinkedList()
	{
		// Set initial value
		this.head = null;
		this.tail = null;
	}
	public void insert(int value)
	{
		LinkNode node = new LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	public void display()
	{
		if (this.head == null)
		{
			Console.WriteLine(" Empty Linked List");
		}
		else
		{
			Console.Write("\n Linked List Element \n");
			// First node of linked list
			Console.Write("  " + this.head.data);
			LinkNode temp = this.head.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				Console.Write("  " + temp.data);
				// visit to next node
				temp = temp.next;
			}
		}
	}
	public void searchElement(int value)
	{
		if (this.head == null)
		{
			Console.WriteLine(" Empty Linked List");
		}
		else
		{
			LinkNode temp = this.head;
			// Find node
			while (temp != null)
			{
				if (temp.data == value)
				{
					Console.Write("\n Node value " + 
                                  value + " exist ");
					return;
				}
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When node are not exist
					Console.Write("\n Node value " + 
                                  value + " are not exist ");
					return;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		CircularLinkedList cll = new CircularLinkedList();
		// First linked list
		cll.insert(15);
		cll.insert(5);
		cll.insert(7);
		cll.insert(4);
		cll.insert(63);
		cll.insert(11);
		cll.insert(17);
		cll.display();
		// Test
		cll.searchElement(4);
		cll.searchElement(34);
		cll.searchElement(11);
	}
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
package main
import "fmt"
// Go Program 
// Search an element in circular singly linked list
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int) * LinkNode {
	var me *LinkNode = &LinkNode {}
	me.data = data
	me.next = nil
	return me
}
type CircularLinkedList struct {
	head * LinkNode
	tail * LinkNode
}
func getCircularLinkedList() * CircularLinkedList {
	var me *CircularLinkedList = &CircularLinkedList {}
	// Set initial value
	me.head = nil
	me.tail = nil
	return me
}
func(this *CircularLinkedList) insert(value int) {
	var node * LinkNode = getLinkNode(value)
	if this.head == nil {
		this.head = node
	} else {
		this.tail.next = node
	}
	node.next = this.head
	this.tail = node
}
// Display node element of circular linked list
func(this CircularLinkedList) display() {
	if this.head == nil {
		fmt.Println(" Empty Linked List")
	} else {
		fmt.Print("\n Linked List Element \n")
		// First node of linked list
		fmt.Print("  ", this.head.data)
		var temp * LinkNode = this.head.next
		// Display linked list node
		for (temp != nil && temp != this.head) {
			// Display node value
			fmt.Print("  ", temp.data)
			// visit to next node
			temp = temp.next
		}
	}
}
func(this CircularLinkedList) searchElement(value int) {
	if this.head == nil {
		fmt.Println(" Empty Linked List")
	} else {
		var temp * LinkNode = this.head
		// Find node
		for (temp != nil) {
			if temp.data == value {
				fmt.Print("\n Node value ", value, " exist ")
				return
			}
			// Visit to next node
			temp = temp.next
			if temp == this.head {
				// When node are not exist
				fmt.Print("\n Node value ", value, " are not exist ")
				return
			}
		}
	}
}
func main() {
	var cll * CircularLinkedList = getCircularLinkedList()
	// First linked list
	cll.insert(15)
	cll.insert(5)
	cll.insert(7)
	cll.insert(4)
	cll.insert(63)
	cll.insert(11)
	cll.insert(17)
	cll.display()
	// Test
	cll.searchElement(4)
	cll.searchElement(34)
	cll.searchElement(11)
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
<?php
// Php Program 
// Search an element in circular singly linked list
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class CircularLinkedList
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	public	function insert($value)
	{
		$node = new LinkNode($value);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			$this->tail->next = $node;
		}
		$node->next = $this->head;
		$this->tail = $node;
	}
	// Display node element of circular linked list
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo(" Empty Linked List".
				"\n");
		}
		else
		{
			echo("\n Linked List Element \n");
			// First node of linked list
			echo("  ".$this->head->data);
			$temp = $this->head->next;
			// Display linked list node
			while ($temp != NULL && $temp != $this->head)
			{
				// Display node value
				echo("  ".$temp->data);
				// visit to next node
				$temp = $temp->next;
			}
		}
	}
	public	function searchElement($value)
	{
		if ($this->head == NULL)
		{
			echo(" Empty Linked List\n");
		}
		else
		{
			$temp = $this->head;
			// Find node
			while ($temp != NULL)
			{
				if ($temp->data == $value)
				{
					echo("\n Node value ".$value.
						" exist ");
					return;
				}
				// Visit to next node
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					// When node are not exist
					echo("\n Node value ".$value.
						" are not exist ");
					return;
				}
			}
		}
	}
}

function main()
{
	$cll = new CircularLinkedList();
	// First linked list
	$cll->insert(15);
	$cll->insert(5);
	$cll->insert(7);
	$cll->insert(4);
	$cll->insert(63);
	$cll->insert(11);
	$cll->insert(17);
	$cll->display();
	// Test
	$cll->searchElement(4);
	$cll->searchElement(34);
	$cll->searchElement(11);
}
main();

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
// Node JS Program 
// Search an element in circular singly linked list
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class CircularLinkedList
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	insert(value)
	{
		var node = new LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	display()
	{
		if (this.head == null)
		{
			console.log(" Empty Linked List");
		}
		else
		{
			process.stdout.write("\n Linked List Element \n");
			// First node of linked list
			process.stdout.write("  " + this.head.data);
			var temp = this.head.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				process.stdout.write("  " + temp.data);
				// visit to next node
				temp = temp.next;
			}
		}
	}
	searchElement(value)
	{
		if (this.head == null)
		{
			console.log(" Empty Linked List");
		}
		else
		{
			var temp = this.head;
			// Find node
			while (temp != null)
			{
				if (temp.data == value)
				{
					process.stdout.write("\n Node value " + 
                                         value + " exist ");
					return;
				}
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When node are not exist
					process.stdout.write("\n Node value " + 
                                         value + " are not exist ");
					return;
				}
			}
		}
	}
}

function main()
{
	var cll = new CircularLinkedList();
	// First linked list
	cll.insert(15);
	cll.insert(5);
	cll.insert(7);
	cll.insert(4);
	cll.insert(63);
	cll.insert(11);
	cll.insert(17);
	cll.display();
	// Test
	cll.searchElement(4);
	cll.searchElement(34);
	cll.searchElement(11);
}
main();

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
#  Python 3 Program 
#  Search an element in circular singly linked list
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class CircularLinkedList :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	def insert(self, value) :
		node = LinkNode(value)
		if (self.head == None) :
			self.head = node
		else :
			self.tail.next = node
		
		node.next = self.head
		self.tail = node
	
	#  Display node element of circular linked list
	def display(self) :
		if (self.head == None) :
			print(" Empty Linked List")
		else :
			print("\n Linked List Element ")
			#  First node of linked list
			print("  ", self.head.data, end = "")
			temp = self.head.next
			#  Display linked list node
			while (temp != None and temp != self.head) :
				#  Display node value
				print("  ", temp.data, end = "")
				#  visit to next node
				temp = temp.next
			
		
	
	def searchElement(self, value) :
		if (self.head == None) :
			print(" Empty Linked List")
		else :
			temp = self.head
			#  Find node
			while (temp != None) :
				if (temp.data == value) :
					print("\n Node value ", 
                          value ," exist ", end = "")
					return
				
				#  Visit to next node
				temp = temp.next
				if (temp == self.head) :
					#  When node are not exist
					print("\n Node value ", 
                          value ," are not exist ", end = "")
					return
				
			
		
	

def main() :
	cll = CircularLinkedList()
	#  First linked list
	cll.insert(15)
	cll.insert(5)
	cll.insert(7)
	cll.insert(4)
	cll.insert(63)
	cll.insert(11)
	cll.insert(17)
	cll.display()
	#  Test
	cll.searchElement(4)
	cll.searchElement(34)
	cll.searchElement(11)

if __name__ == "__main__": main()

Output

 Linked List Element
   15   5   7   4   63   11   17
 Node value  4  exist
 Node value  34  are not exist
 Node value  11  exist
#  Ruby Program 
#  Search an element in circular singly linked list
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 CircularLinkedList 
	# Define the accessor and reader of class CircularLinkedList
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	def insert(value) 
		node = LinkNode.new(value)
		if (self.head == nil) 
			self.head = node
		else
 
			self.tail.next = node
		end

		node.next = self.head
		self.tail = node
	end

	#  Display node element of circular linked list
	def display() 
		if (self.head == nil) 
			print(" Empty Linked List", "\n")
		else
 
			print("\n Linked List Element \n")
			#  First node of linked list
			print("  ", self.head.data)
			temp = self.head.next
			#  Display linked list node
			while (temp != nil && temp != self.head) 
				#  Display node value
				print("  ", temp.data)
				#  visit to next node
				temp = temp.next
			end

		end

	end

	def searchElement(value) 
		if (self.head == nil) 
			print(" Empty Linked List", "\n")
		else
 
			temp = self.head
			#  Find node
			while (temp != nil) 
				if (temp.data == value) 
					print("\n Node value ", value ," exist ")
					return
				end

				#  Visit to next node
				temp = temp.next
				if (temp == self.head) 
					#  When node are not exist
					print("\n Node value ", value ," are not exist ")
					return
				end

			end

		end

	end

end

def main() 
	cll = CircularLinkedList.new()
	#  First linked list
	cll.insert(15)
	cll.insert(5)
	cll.insert(7)
	cll.insert(4)
	cll.insert(63)
	cll.insert(11)
	cll.insert(17)
	cll.display()
	#  Test
	cll.searchElement(4)
	cll.searchElement(34)
	cll.searchElement(11)
end

main()

Output

 Linked List Element 
  15  5  7  4  63  11  17
 Node value 4 exist 
 Node value 34 are not exist 
 Node value 11 exist 
// Scala Program 
// Search an element in circular singly linked list
class LinkNode(var data: Int,
	var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null)
	}
}
class CircularLinkedList(var head: LinkNode,
	var tail: LinkNode)
{
	def this()
	{
		this(null, null)
	}
	def insert(value: Int): Unit = {
		var node: LinkNode = new LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	def display(): Unit = {
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			print("\n Linked List Element \n");
			// First node of linked list
			print("  " + this.head.data);
			var temp: LinkNode = this.head.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				print("  " + temp.data);
				// visit to next node
				temp = temp.next;
			}
		}
	}
	def searchElement(value: Int): Unit = {
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			var temp: LinkNode = this.head;
			// Find node
			while (temp != null)
			{
				if (temp.data == value)
				{
					print("\n Node value " + value + " exist ");
					return;
				}
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When node are not exist
					print("\n Node value " + value + " are not exist ");
					return;
				}
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var cll: CircularLinkedList = new CircularLinkedList();
		// First linked list
		cll.insert(15);
		cll.insert(5);
		cll.insert(7);
		cll.insert(4);
		cll.insert(63);
		cll.insert(11);
		cll.insert(17);
		cll.display();
		// Test
		cll.searchElement(4);
		cll.searchElement(34);
		cll.searchElement(11);
	}
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 exist
// Swift 4 Program 
// Search an element in circular singly linked list
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class CircularLinkedList
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	func insert(_ value: Int)
	{
		let node: LinkNode = LinkNode(value);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			self.tail!.next = node;
		}
		node.next = self.head;
		self.tail = node;
	}
	// Display node element of circular linked list
	func display()
	{
		if (self.head == nil)
		{
			print(" Empty Linked List");
		}
		else
		{
			print("\n Linked List Element ");
			// First node of linked list
			print("  ", self.head!.data, terminator: "");
			var temp: LinkNode? = self.head!.next;
			// Display linked list node
			while (temp  != nil && !(temp === self.head))
			{
				// Display node value
				print("  ", temp!.data, terminator: "");
				// visit to next node
				temp = temp!.next;
			}
		}
	}
	func searchElement(_ value: Int)
	{
		if (self.head == nil)
		{
			print(" Empty Linked List");
		}
		else
		{
			var temp: LinkNode? = self.head;
			// Find node
			while (temp  != nil)
			{
				if (temp!.data == value)
				{
					print("\n Node value ", 
                          value ," exist ", terminator: "");
					return;
				}
				// Visit to next node
				temp = temp!.next;
				if (temp === self.head)
				{
					// When node are not exist
					print("\n Node value ", 
                          value ," are not exist ", terminator: "");
					return;
				}
			}
		}
	}
}
func main()
{
	let cll: CircularLinkedList = CircularLinkedList();
	// First linked list
	cll.insert(15);
	cll.insert(5);
	cll.insert(7);
	cll.insert(4);
	cll.insert(63);
	cll.insert(11);
	cll.insert(17);
	cll.display();
	// Test
	cll.searchElement(4);
	cll.searchElement(34);
	cll.searchElement(11);
}
main();

Output

 Linked List Element
   15   5   7   4   63   11   17
 Node value  4  exist
 Node value  34  are not exist
 Node value  11  exist
// Kotlin Program 
// Search an element in circular singly linked list
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class CircularLinkedList
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	fun insert(value: Int): Unit
	{
		val node: LinkNode = LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail?.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	fun display(): Unit
	{
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			print("\n Linked List Element \n");
			// First node of linked list
			print("  " + this.head?.data);
			var temp: LinkNode ? = this.head?.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				print("  " + temp.data);
				// visit to next node
				temp = temp.next;
			}
		}
	}
	fun searchElement(value: Int): Unit
	{
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			var temp: LinkNode ? = this.head;
			// Find node
			while (temp != null)
			{
				if (temp.data == value)
				{
					print("\n Node value " + value + " exist ");
					return;
				}
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When node are not exist
					print("\n Node value " + value + " are not exist ");
					return;
				}
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val cll: CircularLinkedList = CircularLinkedList();
	// First linked list
	cll.insert(15);
	cll.insert(5);
	cll.insert(7);
	cll.insert(4);
	cll.insert(63);
	cll.insert(11);
	cll.insert(17);
	cll.display();
	// Test
	cll.searchElement(4);
	cll.searchElement(34);
	cll.searchElement(11);
}

Output

 Linked List Element
  15  5  7  4  63  11  17
 Node value 4 exist
 Node value 34 are not exist
 Node value 11 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