Sorted insert for circular linked list

Here given code implementation process.

// Java Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			this.head = node;
			this.tail = node;
			node.next = node;
		}
		else if (this.head.data >= value)
		{
			// Add node at beginning of linked list
			node.next = this.head;
			this.head = node;
			this.tail.next = node;
		}
		else if (this.tail.data <= value)
		{
			// Add node at last position
			this.tail.next = node;
			this.tail = node;
			node.next = this.head;
		}
		else
		{
			// When adding a new node at intermediate position
			LinkNode temp = this.head;
			// Find valid sorted position to add new node
			while (temp != null && temp.next.data < value)
			{
				// visit to next node
				temp = temp.next;
			}
			// Add new node 
			node.next = temp.next;
			temp.next = 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(" Circular 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 static void main(String[] args)
	{
		CircularLinkedList cll = new CircularLinkedList();
		// Add linked list node
		cll.insert(8);
		cll.insert(17);
		cll.insert(26);
		cll.insert(25);
		cll.insert(14);
		cll.insert(5);
		cll.insert(-3);
		cll.insert(62);
		cll.insert(5);
		cll.insert(34);
		// Display va;ie
		cll.display();
	}
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
// Include header file
#include <iostream>
using namespace std;

// C++ Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			this->head = node;
			this->tail = node;
			node->next = node;
		}
		else if (this->head->data >= value)
		{
			// Add node at beginning of linked list
			node->next = this->head;
			this->head = node;
			this->tail->next = node;
		}
		else if (this->tail->data <= value)
		{
			// Add node at last position
			this->tail->next = node;
			this->tail = node;
			node->next = this->head;
		}
		else
		{
			// When adding a new node at intermediate position
			LinkNode *temp = this->head;
			// Find valid sorted position to add new node
			while (temp != NULL && temp->next->data < value)
			{
				// visit to next node
				temp = temp->next;
			}
			// Add new node 
			node->next = temp->next;
			temp->next = node;
		}
	}
	// Display node element of circular linked list
	void display()
	{
		if (this->head == NULL)
		{
			cout << " Empty Linked List" << endl;
		}
		else
		{
			cout << " Circular 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;
			}
		}
	}
};
int main()
{
	CircularLinkedList *cll = new CircularLinkedList();
	// Add linked list node
	cll->insert(8);
	cll->insert(17);
	cll->insert(26);
	cll->insert(25);
	cll->insert(14);
	cll->insert(5);
	cll->insert(-3);
	cll->insert(62);
	cll->insert(5);
	cll->insert(34);
	// Display va;ie
	cll->display();
	return 0;
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
// Include namespace system
using System;
// Csharp Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			this.head = node;
			this.tail = node;
			node.next = node;
		}
		else if (this.head.data >= value)
		{
			// Add node at beginning of linked list
			node.next = this.head;
			this.head = node;
			this.tail.next = node;
		}
		else if (this.tail.data <= value)
		{
			// Add node at last position
			this.tail.next = node;
			this.tail = node;
			node.next = this.head;
		}
		else
		{
			// When adding a new node at intermediate position
			LinkNode temp = this.head;
			// Find valid sorted position to add new node
			while (temp != null && temp.next.data < value)
			{
				// visit to next node
				temp = temp.next;
			}
			// Add new node 
			node.next = temp.next;
			temp.next = node;
		}
	}
	// Display node element of circular linked list
	public void display()
	{
		if (this.head == null)
		{
			Console.WriteLine(" Empty Linked List");
		}
		else
		{
			Console.Write(" Circular 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 static void Main(String[] args)
	{
		CircularLinkedList cll = new CircularLinkedList();
		// Add linked list node
		cll.insert(8);
		cll.insert(17);
		cll.insert(26);
		cll.insert(25);
		cll.insert(14);
		cll.insert(5);
		cll.insert(-3);
		cll.insert(62);
		cll.insert(5);
		cll.insert(34);
		// Display va;ie
		cll.display();
	}
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
<?php
// Php Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			$this->head = $node;
			$this->tail = $node;
			$node->next = $node;
		}
		else if ($this->head->data >= $value)
		{
			// Add node at beginning of linked list
			$node->next = $this->head;
			$this->head = $node;
			$this->tail->next = $node;
		}
		else if ($this->tail->data <= $value)
		{
			// Add node at last position
			$this->tail->next = $node;
			$this->tail = $node;
			$node->next = $this->head;
		}
		else
		{
			// When adding a new node at intermediate position
			$temp = $this->head;
			// Find valid sorted position to add new node
			while ($temp != NULL && $temp->next->data < $value)
			{
				// visit to next node
				$temp = $temp->next;
			}
			// Add new node 
			$node->next = $temp->next;
			$temp->next = $node;
		}
	}
	// Display node element of circular linked list
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo(" Empty Linked List".
				"\n");
		}
		else
		{
			echo(" Circular 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;
			}
		}
	}
}

function main()
{
	$cll = new CircularLinkedList();
	// Add linked list node
	$cll->insert(8);
	$cll->insert(17);
	$cll->insert(26);
	$cll->insert(25);
	$cll->insert(14);
	$cll->insert(5);
	$cll->insert(-3);
	$cll->insert(62);
	$cll->insert(5);
	$cll->insert(34);
	// Display va;ie
	$cll->display();
}
main();

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
// Node JS Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			this.head = node;
			this.tail = node;
			node.next = node;
		}
		else if (this.head.data >= value)
		{
			// Add node at beginning of linked list
			node.next = this.head;
			this.head = node;
			this.tail.next = node;
		}
		else if (this.tail.data <= value)
		{
			// Add node at last position
			this.tail.next = node;
			this.tail = node;
			node.next = this.head;
		}
		else
		{
			// When adding a new node at intermediate position
			var temp = this.head;
			// Find valid sorted position to add new node
			while (temp != null && temp.next.data < value)
			{
				// visit to next node
				temp = temp.next;
			}
			// Add new node 
			node.next = temp.next;
			temp.next = node;
		}
	}
	// Display node element of circular linked list
	display()
	{
		if (this.head == null)
		{
			console.log(" Empty Linked List");
		}
		else
		{
			process.stdout.write(" Circular 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;
			}
		}
	}
}

function main()
{
	var cll = new CircularLinkedList();
	// Add linked list node
	cll.insert(8);
	cll.insert(17);
	cll.insert(26);
	cll.insert(25);
	cll.insert(14);
	cll.insert(5);
	cll.insert(-3);
	cll.insert(62);
	cll.insert(5);
	cll.insert(34);
	// Display va;ie
	cll.display();
}
main();

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
#  Python 3 Program 
#  Sorted insert for circular 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) :
			#  First node of linked list
			self.head = node
			self.tail = node
			node.next = node
		elif (self.head.data >= value) :
			#  Add node at beginning of linked list
			node.next = self.head
			self.head = node
			self.tail.next = node
		elif (self.tail.data <= value) :
			#  Add node at last position
			self.tail.next = node
			self.tail = node
			node.next = self.head
		else :
			#  When adding a new node at intermediate position
			temp = self.head
			#  Find valid sorted position to add new node
			while (temp != None and temp.next.data < value) :
				#  visit to next node
				temp = temp.next
			
			#  Add new node 
			node.next = temp.next
			temp.next = node
		
	
	#  Display node element of circular linked list
	def display(self) :
		if (self.head == None) :
			print(" Empty Linked List")
		else :
			print(" Circular 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 main() :
	cll = CircularLinkedList()
	#  Add linked list node
	cll.insert(8)
	cll.insert(17)
	cll.insert(26)
	cll.insert(25)
	cll.insert(14)
	cll.insert(5)
	cll.insert(-3)
	cll.insert(62)
	cll.insert(5)
	cll.insert(34)
	#  Display vaie
	cll.display()

if __name__ == "__main__": main()

Output

 Circular Linked List Element
   -3   5   5   8   14   17   25   26   34   62
#  Ruby Program 
#  Sorted insert for circular 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) 
			#  First node of linked list
			self.head = node
			self.tail = node
			node.next = node
		elsif (self.head.data >= value) 
			#  Add node at beginning of linked list
			node.next = self.head
			self.head = node
			self.tail.next = node
		elsif (self.tail.data <= value) 
			#  Add node at last position
			self.tail.next = node
			self.tail = node
			node.next = self.head
		else
 
			#  When adding a new node at intermediate position
			temp = self.head
			#  Find valid sorted position to add new node
			while (temp != nil && temp.next.data < value) 
				#  visit to next node
				temp = temp.next
			end

			#  Add new node 
			node.next = temp.next
			temp.next = node
		end

	end

	#  Display node element of circular linked list
	def display() 
		if (self.head == nil) 
			print(" Empty Linked List", "\n")
		else
 
			print(" Circular 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

end

def main() 
	cll = CircularLinkedList.new()
	#  Add linked list node
	cll.insert(8)
	cll.insert(17)
	cll.insert(26)
	cll.insert(25)
	cll.insert(14)
	cll.insert(5)
	cll.insert(-3)
	cll.insert(62)
	cll.insert(5)
	cll.insert(34)
	#  Display vaie
	cll.display()
end

main()

Output

 Circular Linked List Element 
  -3  5  5  8  14  17  25  26  34  62
// Scala Program 
// Sorted insert for circular linked list
class LinkNode(var data: Int,
	var next: LinkNode);
class CircularLinkedList(var head: LinkNode,
	var tail: LinkNode)
{
	def this()
	{
		this(null,null);
	}
	def insert(value: Int): Unit = {
		var node: LinkNode = new LinkNode(value,null);
		if (this.head == null)
		{
			// First node of linked list
			this.head = node;
			this.tail = node;
			node.next = node;
		}
		else if (this.head.data >= value)
		{
			// Add node at beginning of linked list
			node.next = this.head;
			this.head = node;
			this.tail.next = node;
		}
		else if (this.tail.data <= value)
		{
			// Add node at last position
			this.tail.next = node;
			this.tail = node;
			node.next = this.head;
		}
		else
		{
			// When adding a new node at intermediate position
			var temp: LinkNode = this.head;
			// Find valid sorted position to add new node
			while (temp != null && temp.next.data < value)
			{
				// visit to next node
				temp = temp.next;
			}
			// Add new node 
			node.next = temp.next;
			temp.next = node;
		}
	}
	// Display node element of circular linked list
	def display(): Unit = {
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			print(" Circular 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;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var cll: CircularLinkedList = new CircularLinkedList();
		// Add linked list node
		cll.insert(8);
		cll.insert(17);
		cll.insert(26);
		cll.insert(25);
		cll.insert(14);
		cll.insert(5);
		cll.insert(-3);
		cll.insert(62);
		cll.insert(5);
		cll.insert(34);
		// Display va;ie
		cll.display();
	}
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
// Swift 4 Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			self.head = node;
			self.tail = node;
			node.next = node;
		}
		else if (self.head!.data >= value)
		{
			// Add node at beginning of linked list
			node.next = self.head;
			self.head = node;
			self.tail!.next = node;
		}
		else if (self.tail!.data <= value)
		{
			// Add node at last position
			self.tail!.next = node;
			self.tail = node;
			node.next = self.head;
		}
		else
		{
			// When adding a new node at intermediate position
			var temp: LinkNode? = self.head;
			// Find valid sorted position to add new node
			while (temp  != nil && temp!.next!.data < value)
			{
				// visit to next node
				temp = temp!.next;
			}
			// Add new node 
			node.next = temp!.next;
			temp!.next = node;
		}
	}
	// Display node element of circular linked list
	func display()
	{
		if (self.head == nil)
		{
			print(" Empty Linked List");
		}
		else
		{
			print(" Circular 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 main()
{
	let cll: CircularLinkedList = CircularLinkedList();
	// Add linked list node
	cll.insert(8);
	cll.insert(17);
	cll.insert(26);
	cll.insert(25);
	cll.insert(14);
	cll.insert(5);
	cll.insert(-3);
	cll.insert(62);
	cll.insert(5);
	cll.insert(34);
	// Display va;ie
	cll.display();
}
main();

Output

 Circular Linked List Element
   -3   5   5   8   14   17   25   26   34   62
// Kotlin Program 
// Sorted insert for circular 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)
		{
			// First node of linked list
			this.head = node;
			this.tail = node;
			node?.next = node;
		}
		else if (this.head!!.data >= value)
		{
			// Add node at beginning of linked list
			node?.next = this.head;
			this.head = node;
			this.tail?.next = node;
		}
		else if (this.tail!!.data <= value)
		{
			// Add node at last position
			this.tail?.next = node;
			this.tail = node;
			node?.next = this.head;
		}
		else
		{
			// When adding a new node at intermediate position
			var temp: LinkNode? = this.head;
			// Find valid sorted position to add new node
			while (temp != null && temp.next!!.data < value)
			{
				// visit to next node
				temp = temp.next;
			}
			// Add new node 
			node?.next = temp?.next;
			temp?.next = node;
		}
	}
	// Display node element of circular linked list
	fun display(): Unit
	{
		if (this.head == null)
		{
			println(" Empty Linked List");
		}
		else
		{
			print(" Circular 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 main(args: Array < String > ): Unit
{
	val cll: CircularLinkedList = CircularLinkedList();
	// Add linked list node
	cll.insert(8);
	cll.insert(17);
	cll.insert(26);
	cll.insert(25);
	cll.insert(14);
	cll.insert(5);
	cll.insert(-3);
	cll.insert(62);
	cll.insert(5);
	cll.insert(34);
	// Display va;ie
	cll.display();
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62
package main
import "fmt"
// Go Program 
// Sorted insert for circular 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 {
		// First node of linked list
		this.head = node
		this.tail = node
		node.next = node
	} else if this.head.data >= value {
		// Add node at beginning of linked list
		node.next = this.head
		this.head = node
		this.tail.next = node
	} else if this.tail.data <= value {
		// Add node at last position
		this.tail.next = node
		this.tail = node
		node.next = this.head
	} else {
		// When adding a new node at intermediate position
		var temp * LinkNode = this.head
		// Find valid sorted position to add new node
		for (temp != nil && temp.next.data < value) {
			// visit to next node
			temp = temp.next
		}
		// Add new node 
		node.next = temp.next
		temp.next = node
	}
}
// Display node element of circular linked list
func(this CircularLinkedList) display() {
	if this.head == nil {
		fmt.Println(" Empty Linked List")
	} else {
		fmt.Print(" Circular 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 main() {
	var cll * CircularLinkedList = getCircularLinkedList()
	// Add linked list node
	cll.insert(8)
	cll.insert(17)
	cll.insert(26)
	cll.insert(25)
	cll.insert(14)
	cll.insert(5)
	cll.insert(-3)
	cll.insert(62)
	cll.insert(5)
	cll.insert(34)
	// Display vaie
	cll.display()
}

Output

 Circular Linked List Element
  -3  5  5  8  14  17  25  26  34  62

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