Skip to main content

Convert singly linked list to circular list in php

Php program for Convert singly linked list to circular list. Here problem description and explanation.

<?php
// Php program for
// Convert singly linked list into circular list

// Define class of linked list Node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class LinkedList
{
	public $head;
	// Class constructor
	public	function __construct()
	{
		$this->head = NULL;
	}
	// Check circular linked list or not
	// Note that this function is not capable to detect loop
	public	function isCircular()
	{
		if ($this->head == NULL)
		{
			// Case when linked list is empty
			return false;
		}
		else
		{
			$temp = $this->head;
			while ($temp != NULL)
			{
				// Visit to next node
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					// When detecting circular node
					return true;
				}
			}
			// When not circular linked list
			return false;
		}
	}
	// Display node element of linked list
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo "Empty Linked List", "\n";
		}
		else
		{
			echo "Linked List Element :";
			$temp = $this->head;
			// Iterate linked list
			while ($temp != NULL)
			{
				// Display node
				echo "  ".strval($temp->data);
				// Visit to next node
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					// Stop iteration
					return;
				}
			}
			print("\n");
		}
	}
	// Coverted circular Linked list
	public	function makeCircular()
	{
		if ($this->head == NULL)
		{
			echo "Empty Linked List", "\n";
		}
		else
		{
			$temp = $this->head;
			// Find last node
			while ($temp->next != NULL)
			{
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					// Already circular Linked list
					return;
				}
			}
			// Connect last node to first node
			$temp->next = $this->head;
		}
	}
	public static
	function main($args)
	{
		$ll = new LinkedList();
		// Insert element of linked list
		$ll->head = new LinkNode(1);
		$ll->head->next = new LinkNode(2);
		$ll->head->next->next = new LinkNode(3);
		$ll->head->next->next->next = new LinkNode(4);
		$ll->head->next->next->next->next = new LinkNode(5);
		$ll->head->next->next->next->next->next = new LinkNode(6);
		$ll->head->next->next->next->next->next->next = new LinkNode(7);
		$ll->display();
		if ($ll->isCircular())
		{
			echo "Circular Yes", "\n";
		}
		else
		{
			echo "Circular No", "\n";
		}
		echo "After Convert", "\n";
		$ll->makeCircular();
		if ($ll->isCircular())
		{
			echo "Circular Yes", "\n";
		}
		else
		{
			echo "Circular No", "\n";
		}
	}
}
LinkedList::main(array());

Output

Linked List Element :  1  2  3  4  5  6  7
Circular No
After Convert
Circular Yes




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