Insert node at end of circular linked list
In this post are given an example of how to insert new created nodes of end of circular linked list.
Generally there are two way to solve this problem. method 1 Using of single pointer . And method 2 using of two pointer variables.
Method 1: This method are normally used because we are assume that linked list are contain initial node (assumption to add new node to existing list). So this method are useful. First we are need to find last nodes of linked list. But note that in circular linked list last node, next pointer always hold the reference of first linked list node.
So start iteration from head of linked list and check next upcoming node is not a head. When this condition are satisfied then visit next upcoming node. When condition are fail, That means this is a last node of circular list. And add new node to last position , and assign to this inserted next pointer field to reference of head node of linked list.
Test Cases: There is following test case are useful.
1) In case linked list are empty then create new linked list node and assign this address to head pointer of linked list. But note that Circular linked list last node connect to first node by next pointer. So in this case assign the newly created node address is to next pointer .
2) When linked list are not empty then find last linked list node. This process are requires O(n) time. And add new node to last position connect last node to head node by using of last node next pointer fields.
Time complexity of this method are O(n) (Note that We are reducing this complexity in next method). Suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Here given code implementation process.
// Java Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data, LinkNode first)
{
this.data = data;
this.next = first;
}
}
public class CircularLinkedList
{
public LinkNode head;
// Class constructors
CircularLinkedList()
{
this.head = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
LinkNode temp = this.head;
// Find last node
while (temp.next != this.head)
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
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("Linked List Element :");
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == head)
{
// Stop iteration
return;
}
}
}
}
public static void main(String[] args)
{
CircularLinkedList task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}
Output

Linked List Element : 1 2 3 4 5 6 7 8
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data, LinkNode *first)
{
this->data = data;
this->next = first;
}
};
class CircularLinkedList
{
public: LinkNode *head;
// Class constructors
CircularLinkedList()
{
this->head = NULL;
}
// Insert node at end of circular linked list
void insert(int value)
{
// Create a node
LinkNode *node = new LinkNode(value, this->head);
if (this->head == NULL)
{
// First node of linked list
this->head = node;
node->next = this->head;
}
else
{
LinkNode *temp = this->head;
// Find last node
while (temp->next != this->head)
{
// Visit to next node
temp = temp->next;
}
// Add new node at the last
temp->next = node;
}
}
// Display node element of circular linked list
void display()
{
if (this->head == NULL)
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Linked List Element :";
LinkNode *temp = this->head;
// iterate linked list
while (temp != NULL)
{
// Display node
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
if (temp == this->head)
{
// Stop iteration
return;
}
}
}
}
};
int main()
{
CircularLinkedList *task = new CircularLinkedList();
// Add linked list node
task->insert(1);
task->insert(2);
task->insert(3);
task->insert(4);
task->insert(5);
task->insert(6);
task->insert(7);
task->insert(8);
// Display node
task->display();
return 0;
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// C Program
// Insert node at last of circular linked lists
#include <stdio.h>
// for malloc function
#include <stdlib.h>
struct LinkNode
{
int data;
struct LinkNode *next;
};
struct CircularLinkedList
{
struct LinkNode *head;
};
// Returns the new linked list
struct CircularLinkedList *newLinkedList()
{
// Create memory of head and tail Nodes
struct CircularLinkedList *cll = (struct CircularLinkedList *)
malloc(sizeof(struct CircularLinkedList));
if (cll == NULL)
{
printf("Memory overflow\n");
}
else
{
cll->head = NULL;
}
return cll;
}
// Insert node at end of circular linked list
void insert(struct CircularLinkedList *cll, int value)
{
// Create a node
struct LinkNode *node = (struct LinkNode *)
malloc(sizeof(struct LinkNode));
if (node == NULL)
{
// Overflow
return;
}
node->data = value;
node->next = cll->head;
if (cll->head == NULL)
{
// First node of linked list
cll->head = node;
node->next = cll->head;
}
else
{
struct LinkNode *temp = cll->head;
// Find last node
while (temp->next != cll->head)
{
// Visit to next node
temp = temp->next;
}
// Add new node at the last
temp->next = node;
}
}
//display element of linked list
void display(struct LinkNode *head)
{
if (head == NULL)
{
printf("Empty linked list");
}
else
{
struct LinkNode *temp = head;
printf("Linked List Element : ");
while (temp)
{
printf("%d ", temp->data);
// visit to next node
temp = temp->next;
if (temp == head)
{
break; //terminate loop
}
}
}
}
int main()
{
// Create node pointer
struct CircularLinkedList *cll = newLinkedList();
// insert element of linked list
insert(cll, 1);
insert(cll, 2);
insert(cll, 3);
insert(cll, 4);
insert(cll, 5);
insert(cll, 6);
insert(cll, 7);
insert(cll, 8);
// display all nodes
display(cll->head);
return 0;
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
package main
import "fmt"
// Go Program
// Insert node at last of circular linked lists
// Define class of linked list Node
type LinkNode struct {
data int
next * LinkNode
}
func getLinkNode(data int, first * LinkNode) * LinkNode {
var me *LinkNode = &LinkNode {data,first}
return me
}
type CircularLinkedList struct {
head * LinkNode
}
func getCircularLinkedList() * CircularLinkedList {
var me *CircularLinkedList = &CircularLinkedList {nil}
return me
}
// Insert node at end of circular linked list
func(this *CircularLinkedList) insert(value int) {
// Create a node
var node * LinkNode = getLinkNode(value, this.head)
if this.head == nil {
// First node of linked list
this.head = node
node.next = this.head
} else {
var temp * LinkNode = this.head
// Find last node
for (temp.next != this.head) {
// Visit to next node
temp = temp.next
}
// Add new node at the last
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("Linked List Element :")
var temp * LinkNode = this.head
// iterate linked list
for (temp != nil) {
// Display node
fmt.Print(" ", temp.data)
// Visit to next node
temp = temp.next
if temp == this.head {
// Stop iteration
return
}
}
}
}
func main() {
var task * CircularLinkedList = getCircularLinkedList()
// Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
// Display node
task.display()
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Include namespace system
using System;
// Csharp Program
// Insert node at last of circular linked lists
// Define class of linked list Node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data, LinkNode first)
{
this.data = data;
this.next = first;
}
}
public class CircularLinkedList
{
public LinkNode head;
// Class constructors
CircularLinkedList()
{
this.head = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
LinkNode temp = this.head;
// Find last node
while (temp.next != this.head)
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
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("Linked List Element :");
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
public static void Main(string[] args)
{
CircularLinkedList task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
<?php
// Php Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
public $data;
public $next;
public function __construct($data, $first)
{
$this->data = $data;
$this->next = $first;
}
}
class CircularLinkedList
{
public $head;
// Class constructors
function __construct()
{
$this->head = NULL;
}
// Insert node at end of circular linked list
public function insert($value)
{
// Create a node
$node = new LinkNode($value, $this->head);
if ($this->head == NULL)
{
// First node of linked list
$this->head = $node;
$node->next = $this->head;
}
else
{
$temp = $this->head;
// Find last node
while ($temp->next != $this->head)
{
// Visit to next node
$temp = $temp->next;
}
// Add new node at the last
$temp->next = $node;
}
}
// Display node element of circular linked list
public function display()
{
if ($this->head == NULL)
{
printf("%s\n", "Empty Linked List");
}
else
{
printf("%s", "Linked List Element :");
$temp = $this->head;
// iterate linked list
while ($temp != NULL)
{
// Display node
printf("%s", " ".strval($temp->data));
// Visit to next node
$temp = $temp->next;
if ($temp == $this->head)
{
// Stop iteration
return;
}
}
}
}
public static
function main($args)
{
$task = new CircularLinkedList();
// Add linked list node
$task->insert(1);
$task->insert(2);
$task->insert(3);
$task->insert(4);
$task->insert(5);
$task->insert(6);
$task->insert(7);
$task->insert(8);
// Display node
$task->display();
}
}
CircularLinkedList::main(array());
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Node JS Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
constructor(data, first)
{
this.data = data;
this.next = first;
}
}
class CircularLinkedList
{
// Class constructors
constructor()
{
this.head = null;
}
// Insert node at end of circular linked list
insert(value)
{
// Create a node
var node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
var temp = this.head;
// Find last node
while (temp.next != this.head)
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
display()
{
if (this.head == null)
{
console.log("Empty Linked List");
}
else
{
process.stdout.write("Linked List Element :");
var temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
}
function main()
{
var task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
main();
Output
Linked List Element : 1 2 3 4 5 6 7 8
# Python 3 Program
# Insert node at last of circular linked lists
# Define class of linked list Node
class LinkNode :
def __init__(self, data, first) :
self.data = data
self.next = first
class CircularLinkedList :
# Class constructors
def __init__(self) :
self.head = None
# Insert node at end of circular linked list
def insert(self, value) :
# Create a node
node = LinkNode(value, self.head)
if (self.head == None) :
# First node of linked list
self.head = node
node.next = self.head
else :
temp = self.head
# Find last node
while (temp.next != self.head) :
# Visit to next node
temp = temp.next
# Add new node at the last
temp.next = node
# Display node element of circular linked list
def display(self) :
if (self.head == None) :
print("Empty Linked List")
else :
print("Linked List Element :", end = "")
temp = self.head
# iterate linked list
while (temp != None) :
# Display node
print(" ", temp.data, end = "")
# Visit to next node
temp = temp.next
if (temp == self.head) :
# Stop iteration
return
def main() :
task = CircularLinkedList()
# Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
# Display node
task.display()
if __name__ == "__main__": main()
Output
Linked List Element : 1 2 3 4 5 6 7 8
# Ruby Program
# Insert node at last of circular linked lists
# Define class of linked list Node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data, first)
self.data = data
self.next = first
end
end
class CircularLinkedList
# Define the accessor and reader of class CircularLinkedList
attr_reader :head
attr_accessor :head
# Class constructors
def initialize()
self.head = nil
end
# Insert node at end of circular linked list
def insert(value)
# Create a node
node = LinkNode.new(value, self.head)
if (self.head == nil)
# First node of linked list
self.head = node
node.next = self.head
else
temp = self.head
# Find last node
while (temp.next != self.head)
# Visit to next node
temp = temp.next
end
# Add new node at the last
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("Linked List Element :")
temp = self.head
# iterate linked list
while (temp != nil)
# Display node
print(" ", temp.data)
# Visit to next node
temp = temp.next
if (temp == self.head)
# Stop iteration
return
end
end
end
end
end
def main()
task = CircularLinkedList.new()
# Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
# Display node
task.display()
end
main()
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Scala Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode(var data: Int,
var next: LinkNode);
class CircularLinkedList(var head: LinkNode)
{
// Class constructors
def this()
{
this(null);
}
// Insert node at end of circular linked list
def insert(value: Int): Unit = {
// Create a node
var node: LinkNode = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
var temp: LinkNode = this.head;
// Find last node
while (temp.next != this.head)
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
def display(): Unit = {
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Element :");
var temp: LinkNode = this.head;
// iterate linked list
while (temp != null)
{
// Display node
print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == head)
{
// Stop iteration
return;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CircularLinkedList = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Swift 4 Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int, _ first: LinkNode? )
{
self.data = data;
self.next = first;
}
}
class CircularLinkedList
{
var head: LinkNode? ;
// Class constructors
init()
{
self.head = nil;
}
// Insert node at end of circular linked list
func insert(_ value: Int)
{
// Create a node
let node: LinkNode = LinkNode(value, self.head);
if (self.head == nil)
{
// First node of linked list
self.head = node;
node.next = self.head;
}
else
{
var temp: LinkNode? = self.head;
// Find last node
while (!(temp!.next === self.head))
{
// Visit to next node
temp = temp!.next;
}
// Add new node at the last
temp!.next = node;
}
}
// Display node element of circular linked list
func display()
{
if (self.head == nil)
{
print("Empty Linked List");
}
else
{
print("Linked List Element :", terminator: "");
var temp: LinkNode? = self.head;
// iterate linked list
while (temp != nil)
{
// Display node
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
if (temp === self.head)
{
// Stop iteration
return;
}
}
}
}
}
func main()
{
let task: CircularLinkedList = CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
main();
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Kotlin Program
// Insert node at last of circular linked lists
// Define class of linked list Node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int, first: LinkNode ? )
{
this.data = data;
this.next = first;
}
}
class CircularLinkedList
{
var head: LinkNode ? ;
// Class constructors
constructor()
{
this.head = null;
}
// Insert node at end of circular linked list
fun insert(value: Int): Unit
{
// Create a node
val node: LinkNode = LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
var temp: LinkNode ? = this.head;
// Find last node
while (temp?.next != this.head)
{
// Visit to next node
temp = temp?.next;
}
// Add new node at the last
temp?.next = node;
}
}
// Display node element of circular linked list
fun display(): Unit
{
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Element :");
var temp: LinkNode ? = this.head;
// iterate linked list
while (temp != null)
{
// Display node
print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: CircularLinkedList = CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
Method 2 : In this scenario used two pointer head and tail. head are store the reference of first inserted nodes. And tail are store the reference of current insert linked list node.

// Java Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data, LinkNode first)
{
this.data = data;
this.next = first;
}
}
public class CircularLinkedList
{
public LinkNode head;
public LinkNode tail;
// Class constructors
CircularLinkedList()
{
this.head = null;
this.tail = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
this.tail = node;
}
else
{
this.tail.next = node;
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("Linked List Element :");
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == head)
{
// Stop iteration
return;
}
}
}
}
public static void main(String[] args)
{
CircularLinkedList task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}

Output
Linked List Element : 1 2 3 4 5 6 7 8
// Include namespace system
using System;
// Csharp Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data, LinkNode first)
{
this.data = data;
this.next = first;
}
}
public class CircularLinkedList
{
public LinkNode head;
public LinkNode tail;
// Class constructors
CircularLinkedList()
{
this.head = null;
this.tail = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
this.tail = node;
}
else
{
this.tail.next = node;
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("Linked List Element :");
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
public static void Main(string[] args)
{
CircularLinkedList task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data, LinkNode *first)
{
this->data = data;
this->next = first;
}
};
class CircularLinkedList
{
public: LinkNode *head;
LinkNode *tail;
// Class constructors
CircularLinkedList()
{
this->head = NULL;
this->tail = NULL;
}
// Insert node at end of circular linked list
void insert(int value)
{
// Create a node
LinkNode *node = new LinkNode(value, this->head);
if (this->head == NULL)
{
// First node of linked list
this->head = node;
node->next = this->head;
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
// Display node element of circular linked list
void display()
{
if (this->head == NULL)
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Linked List Element :";
LinkNode *temp = this->head;
// iterate linked list
while (temp != NULL)
{
// Display node
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
if (temp == this->head)
{
// Stop iteration
return;
}
}
}
}
};
int main()
{
CircularLinkedList *task = new CircularLinkedList();
// Add linked list node
task->insert(1);
task->insert(2);
task->insert(3);
task->insert(4);
task->insert(5);
task->insert(6);
task->insert(7);
task->insert(8);
// Display node
task->display();
return 0;
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// C Program
// Insert node at last of circular linked lists using tail pointer
#include <stdio.h>
// for malloc function
#include <stdlib.h>
struct LinkNode
{
int data;
struct LinkNode *next;
};
struct CircularLinkedList
{
struct LinkNode *head;
struct LinkNode *tail;
};
// Returns the new linked list
struct CircularLinkedList *newLinkedList()
{
// Create memory of head and tail Nodes
struct CircularLinkedList *cll = (struct CircularLinkedList *)
malloc(sizeof(struct CircularLinkedList));
if (cll == NULL)
{
printf("Memory overflow\n");
}
else
{
cll->head = NULL;
cll->tail = NULL;
}
return cll;
}
// Insert node at end of circular linked list
void insert(struct CircularLinkedList *cll, int value)
{
// Create a node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
// Overflow
return;
}
node->data = value;
node->next = cll->head;
if (cll->head == NULL)
{
// First node of linked list
cll->head = node;
cll->tail = node;
node->next = cll->head;
}
else
{
cll->tail->next = node;
// New tail node
cll->tail = node;
}
}
//display element of linked list
void display(struct LinkNode *head)
{
if (head == NULL)
{
printf("Empty linked list");
}
else
{
struct LinkNode *temp = head;
printf("Linked List Element : ");
while (temp)
{
printf("%d ", temp->data);
// visit to next node
temp = temp->next;
if (temp == head)
{
break; //terminate loop
}
}
}
}
int main()
{
// Create node pointer
struct CircularLinkedList *cll = newLinkedList();
// insert element of linked list
insert(cll, 1);
insert(cll, 2);
insert(cll, 3);
insert(cll, 4);
insert(cll, 5);
insert(cll, 6);
insert(cll, 7);
insert(cll, 8);
// display all nodes
display(cll->head);
return 0;
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
package main
import "fmt"
// Go Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
type LinkNode struct {
data int
next * LinkNode
}
func getLinkNode(data int, first * LinkNode) * LinkNode {
var me *LinkNode = &LinkNode {}
me.data = data
me.next = first
return me
}
type CircularLinkedList struct {
head * LinkNode
tail * LinkNode
}
func getCircularLinkedList() * CircularLinkedList {
var me *CircularLinkedList = &CircularLinkedList {}
me.head = nil
me.tail = nil
return me
}
// Insert node at end of circular linked list
func(this *CircularLinkedList) insert(value int) {
// Create a node
var node * LinkNode = getLinkNode(value, this.head)
if this.head == nil {
// First node of linked list
this.head = node
node.next = this.head
this.tail = node
} else {
this.tail.next = node
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("Linked List Element :")
var temp * LinkNode = this.head
// iterate linked list
for (temp != nil) {
// Display node
fmt.Print(" ", temp.data)
// Visit to next node
temp = temp.next
if temp == this.head {
// Stop iteration
return
}
}
}
}
func main() {
var task * CircularLinkedList = getCircularLinkedList()
// Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
// Display node
task.display()
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
<?php
// Php Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
public $data;
public $next;
public function __construct($data, $first)
{
$this->data = $data;
$this->next = $first;
}
}
class CircularLinkedList
{
public $head;
public $tail;
// Class constructors
function __construct()
{
$this->head = NULL;
$this->tail = NULL;
}
// Insert node at end of circular linked list
public function insert($value)
{
// Create a node
$node = new LinkNode($value, $this->head);
if ($this->head == NULL)
{
// First node of linked list
$this->head = $node;
$node->next = $this->head;
$this->tail = $node;
}
else
{
$this->tail->next = $node;
$this->tail = $node;
}
}
// Display node element of circular linked list
public function display()
{
if ($this->head == NULL)
{
printf("%s\n", "Empty Linked List");
}
else
{
printf("%s", "Linked List Element :");
$temp = $this->head;
// iterate linked list
while ($temp != NULL)
{
// Display node
printf("%s", " ".strval($temp->data));
// Visit to next node
$temp = $temp->next;
if ($temp == $this->head)
{
// Stop iteration
return;
}
}
}
}
public static
function main($args)
{
$task = new CircularLinkedList();
// Add linked list node
$task->insert(1);
$task->insert(2);
$task->insert(3);
$task->insert(4);
$task->insert(5);
$task->insert(6);
$task->insert(7);
$task->insert(8);
// Display node
$task->display();
}
}
CircularLinkedList::main(array());
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Node JS Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
constructor(data, first)
{
this.data = data;
this.next = first;
}
}
class CircularLinkedList
{
// Class constructors
constructor()
{
this.head = null;
this.tail = null;
}
// Insert node at end of circular linked list
insert(value)
{
// Create a node
var node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
display()
{
if (this.head == null)
{
console.log("Empty Linked List");
}
else
{
process.stdout.write("Linked List Element :");
var temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
}
function main()
{
var task = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
main();
Output
Linked List Element : 1 2 3 4 5 6 7 8
# Python 3 Program
# Insert node at last of circular linked lists using tail
# Define class of linked list Node
class LinkNode :
def __init__(self, data, first) :
self.data = data
self.next = first
class CircularLinkedList :
# Class constructors
def __init__(self) :
self.head = None
self.tail = None
# Insert node at end of circular linked list
def insert(self, value) :
# Create a node
node = LinkNode(value, self.head)
if (self.head == None) :
# First node of linked list
self.head = node
node.next = self.head
self.tail = node
else :
self.tail.next = node
self.tail = node
# Display node element of circular linked list
def display(self) :
if (self.head == None) :
print("Empty Linked List")
else :
print("Linked List Element :", end = "")
temp = self.head
# iterate linked list
while (temp != None) :
# Display node
print(" ", temp.data, end = "")
# Visit to next node
temp = temp.next
if (temp == self.head) :
# Stop iteration
return
def main() :
task = CircularLinkedList()
# Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
# Display node
task.display()
if __name__ == "__main__": main()
Output
Linked List Element : 1 2 3 4 5 6 7 8
# Ruby Program
# Insert node at last of circular linked lists using tail
# Define class of linked list Node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data, first)
self.data = data
self.next = first
end
end
class CircularLinkedList
# Define the accessor and reader of class CircularLinkedList
attr_reader :head, :tail
attr_accessor :head, :tail
# Class constructors
def initialize()
self.head = nil
self.tail = nil
end
# Insert node at end of circular linked list
def insert(value)
# Create a node
node = LinkNode.new(value, self.head)
if (self.head == nil)
# First node of linked list
self.head = node
node.next = self.head
self.tail = node
else
self.tail.next = node
self.tail = node
end
end
# Display node element of circular linked list
def display()
if (self.head == nil)
print("Empty Linked List", "\n")
else
print("Linked List Element :")
temp = self.head
# iterate linked list
while (temp != nil)
# Display node
print(" ", temp.data)
# Visit to next node
temp = temp.next
if (temp == self.head)
# Stop iteration
return
end
end
end
end
end
def main()
task = CircularLinkedList.new()
# Add linked list node
task.insert(1)
task.insert(2)
task.insert(3)
task.insert(4)
task.insert(5)
task.insert(6)
task.insert(7)
task.insert(8)
# Display node
task.display()
end
main()
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Scala Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode(var data: Int,
var next: LinkNode);
class CircularLinkedList(var head: LinkNode,
var tail: LinkNode)
{
// Class constructors
def this()
{
this(null, null);
}
// Insert node at end of circular linked list
def insert(value: Int): Unit = {
// Create a node
var node: LinkNode = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
def display(): Unit = {
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Element :");
var temp: LinkNode = this.head;
// iterate linked list
while (temp != null)
{
// Display node
print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == head)
{
// Stop iteration
return;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CircularLinkedList = new CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Swift 4 Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int, _ first: LinkNode? )
{
self.data = data;
self.next = first;
}
}
class CircularLinkedList
{
var head: LinkNode? ;
var tail: LinkNode? ;
// Class constructors
init()
{
self.head = nil;
self.tail = nil;
}
// Insert node at end of circular linked list
func insert(_ value: Int)
{
// Create a node
let node: LinkNode = LinkNode(value, self.head);
if (self.head == nil)
{
// First node of linked list
self.head = node;
node.next = self.head;
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
// Display node element of circular linked list
func display()
{
if (self.head == nil)
{
print("Empty Linked List");
}
else
{
print("Linked List Element :", terminator: "");
var temp: LinkNode? = self.head;
// iterate linked list
while (temp != nil)
{
// Display node
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
if (temp === self.head)
{
// Stop iteration
return;
}
}
}
}
}
func main()
{
let task: CircularLinkedList = CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
main();
Output
Linked List Element : 1 2 3 4 5 6 7 8
// Kotlin Program
// Insert node at last of circular linked lists using tail
// Define class of linked list Node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int, first: LinkNode ? )
{
this.data = data;
this.next = first;
}
}
class CircularLinkedList
{
var head: LinkNode ? ;
var tail: LinkNode ? ;
// Class constructors
constructor()
{
this.head = null;
this.tail = null;
}
// Insert node at end of circular linked list
fun insert(value: Int): Unit
{
// Create a node
val node: LinkNode = LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
this.tail = node;
}
else
{
this.tail?.next = node;
this.tail = node;
}
}
// Display node element of circular linked list
fun display(): Unit
{
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Element :");
var temp: LinkNode ? = this.head;
// iterate linked list
while (temp != null)
{
// Display node
print(" " + temp.data);
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: CircularLinkedList = CircularLinkedList();
// Add linked list node
task.insert(1);
task.insert(2);
task.insert(3);
task.insert(4);
task.insert(5);
task.insert(6);
task.insert(7);
task.insert(8);
// Display node
task.display();
}
Output
Linked List Element : 1 2 3 4 5 6 7 8
Don't stop learning
Animation of circular linked list
Insert node at end of singly linked list
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