Insert node at beginning of circular linked list

Circular linked list, last node next pointer are not NULL. That are contain the reference of first node of linked list. That is difference of single and circular linked list. Basically we can choose two variant to make a circular single linked list. First only one head pointer. And another is two pointers head and tail.

First Approach : That is very simple and similar to single linked list. And inserting a new node to beginning of this linked list this are need O(1) time. But the properties of circular linked list, always last node next pointer hold the address of first node of linked list.

So finding last node and provide next pointer reference to new head this process are tack O(n) time. So overall time complexity of first approach is O(n).

For example, suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Circular linked list

Here given code implementation process.

//C Program 
//Insert linked list node at beginning of circular single linked list
#include <stdio.h>
//for malloc function
#include <stdlib.h> 

//Create structure
struct Node
{
  int data;
  struct Node*next;
};

//insert Node at beginning of linked list
void insert(struct Node**head,int value)
{
  //Create dynamic node
  struct Node*node = (struct Node*)malloc(sizeof(struct Node));
  if(node==NULL)
  {
    printf("Memory overflow\n");
  }
  else
  {
    node->data=value;
    node->next=*head;
    
    if(*head==NULL)
    {
      //When empty linked list
      *head=node;
      node->next=node;
    }
    else
    {

      struct Node*temp=*head;
      //Find last node
      while(temp!=NULL && temp->next!=*head)
      {
        temp=temp->next;
      }
      //connect last node next pointer is to new node
      temp->next=node;
      //make new head
      *head=node;
    }
    
  }
}
//display element of linked list
void display(struct Node*head)
{

  if(head==NULL)
  {
    printf("Empty linked list");
  }
  else
  {
    struct Node*temp=head;
    printf("Linked List Element : ");
    while(temp)
    {
      printf("%d  ",temp->data);
      temp=temp->next;
      if(temp==head)
      {
        break; //terminate loop
      }
    }

  }
}
int main()
{

  //Create node pointer
  struct Node*head=NULL;
  //insert element of linked list
  insert(&head,8);
  insert(&head,7);
  insert(&head,6);
  insert(&head,5);
  insert(&head,4);
  insert(&head,3);
  insert(&head,2);
  insert(&head,1);
  //display all node
  display(head);
  return 0;
}

Output

Linked List Element : 1  2  3  4  5  6  7  8
//C++ Program 
//Insert circular linked list at beginning position
//Define class of linked list Node
#include<iostream>

using namespace std;
class Node {
  public:
  int data;
  Node *next;
  Node(int data, Node *next) {
    this->data = data;
    this->next = next;
  }
};
class LinkedList {
  public:
  Node *head;
  //Class constructors
  LinkedList() {
    this->head = NULL;
  }
  //Insert node at begining of linked list
  void insert(int value) {
    //Create a node
    Node *node = new Node(value, this->head);
    if (this->head == NULL) {
      //First node of linked list
      this->head = node;
      node->next = this->head;
    } else {
      Node *temp = this->head;
      //Find last node
      while (temp->next != this->head) {
        temp = temp->next;
      }
      //Add node
      temp->next = node;
      //make new head node
      this->head = node;
    }
  }
  //Display node element of circular linked list
  void display() {
    if (this->head == NULL) {
      cout << "Empty Linked List";
    } else {
      cout << "Circular Linked List Element :";
      Node *temp = this->head;
      while (temp != NULL) {
        cout << " " << temp->data;
        temp = temp->next;
        if (temp == this->head) {
          break;
        }
      }
    }
  }
};
int main() {
  LinkedList obj =  LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
  return 0;
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Java Program 
//Insert circular linked list at beginning position


//Define class of linked list Node
class Node
{
  public int data;
  public Node next;
  public Node(int data, Node next)
  {
    this.data = data;
    this.next = next;
  }
}
public class LinkedList
{
 
  public Node head;

  //Class constructors
  LinkedList()
  {
    head=null;
  } 
  //Insert node at begining of linked list
  public void insert(int value)
  {
    //Create a node
    Node node=new Node(value,head);

    if(head==null)
    {
      //First node of linked list
      head = node;
      node.next = head;
    }
    else
    {
      Node temp = head;
      //Find last node
      while(temp.next != head)
      {
        temp=temp.next;
      }
      //Add node
      temp.next=node;
      //make new head node
      head=node;
    }
    
  }
  //Display node element of circular linked list
  public void display()
  {
    if(head==null)
    {
      System.out.println("Empty Linked List");
    }
    else
    {
      System.out.print("Circular Linked List Element :");

      Node temp = head;

      while(temp!=null)
      {
        System.out.print("  "+temp.data);

        temp=temp.next;

        if(temp==head) 
        {
          break;
        }
      }
    }
  }


  
  public static void main(String[] args) 
  {
    LinkedList obj=new LinkedList();
    //insert following linked list nodes
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);

    obj.display();
    
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
Insert node at beginning of circular linked list in java
//C# Program 
//Insert circular linked list at beginning position

//Define class of linked list Node
using System;
public class Node {
  public int data;
  public Node next;
  public Node(int data, Node next) {
    this.data = data;
    this.next = next;
  }
}
public class LinkedList {
  public Node head;
  //Class constructors
  LinkedList() {
    head = null;
  }
  //Insert node at begining of linked list
  public void insert(int value) {
    //Create a node
    Node node = new Node(value, head);
    if (head == null) {
      //First node of linked list
      head = node;
      node.next = head;
    } else {
      Node temp = head;
      //Find last node
      while (temp.next != head) {
        temp = temp.next;
      }
      //Add node
      temp.next = node;
      //make new head node
      head = node;
    }
  }
  //Display node element of circular linked list
  public void display() {
    if (head == null) {
      Console.WriteLine("Empty Linked List");
    } else {
      Console.Write("Circular Linked List Element :");
      Node temp = head;
      while (temp != null) {
        Console.Write(" " + temp.data);
        temp = temp.next;
        if (temp == head) {
          break;;
        }
      }
    }
  }
  public static void Main(String[] args) {
    LinkedList obj = new LinkedList();
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);
    obj.display();
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
<?php
//Php Program 
//Insert circular linked list at beginning position
//Define class of linked list Node
class Node {
  public $data;
  public $next;

  function __construct($data, $next) {
    $this->data = $data;
    $this->next = $next;
  }
}
class LinkedList {
  public $head;
  //Class constructors

  function __construct() {
    $this->head = null;
  }
  //Insert node at begining of linked list
  public  function insert($value) {
    //Create a node
    $node = new Node($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) {
        $temp = $temp->next;
      }
      //Add node
      $temp->next = $node;
      //make new head node
      $this->head = $node;
    }
  }
  //Display node element of circular linked list
  public  function display() {
    if ($this->head == null) {
      echo("Empty Linked List");
    } else {
      echo("Circular Linked List Element :");
      $temp = $this->head;
      while ($temp != null) {
        echo(" ". $temp->data);
        $temp = $temp->next;
        if ($temp == $this->head) {
          break;
        }
      }
    }
  }
}

function main() {
  $obj = new LinkedList();
  //insert following linked list nodes
  $obj->insert(8);
  $obj->insert(7);
  $obj->insert(6);
  $obj->insert(5);
  $obj->insert(4);
  $obj->insert(3);
  $obj->insert(2);
  $obj->insert(1);
  $obj->display();

}
main();

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Node Js Program 
//Insert circular linked list at beginning position
//Define class of linked list Node
class Node {
  constructor(data, next) {
    this.data = data;
    this.next = next;
  }
}
class LinkedList {
  //Class constructors
  constructor() {
    this.head = null;
  }

  //Insert node at begining of linked list
  insert(value) {
    //Create a node
    var node = new Node(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) {
        temp = temp.next;
      }

      //Add node
      temp.next = node;
      //make new head node
      this.head = node;
    }
  }

  //Display node element of circular linked list
  display() {
    if (this.head == null) {
      process.stdout.write("Empty Linked List");
    } else {
      process.stdout.write("Circular Linked List Element :");
      var temp = this.head;
      while (temp != null) {
        process.stdout.write(" " + temp.data);
        temp = temp.next;
        if (temp == this.head) {
          break;
        }
      }
    }
  }
}

function main(args) {
  var obj = new LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
}

main();

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
# Python 3 Program 
# Insert circular linked list at beginning position

# Define class of linked list Node
class Node :
  
  def __init__(self, data, next) :
    self.data = data
    self.next = next
  

class LinkedList :
  
  # Class constructors
  def __init__(self) :
    self.head = None
  
  # Insert node at begining of linked list
  def insert(self, value) :
    # Create a node
    node = Node(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) :
        temp = temp.next
      
      # Add node
      temp.next = node
      # make new head node
      self.head = node
    
  
  # Display node element of circular linked list
  def display(self) :
    if (self.head == None) :
      print("Empty Linked List", end = "")
    else :
      print("Circular Linked List Element :", end = "")
      temp = self.head
      while (temp != None) :
        print(" ", temp.data, end = "")
        temp = temp.next
        if (temp == self.head) :
          break
        
  

def main() :
  obj = LinkedList()
  # insert following linked list nodes
  obj.insert(8)
  obj.insert(7)
  obj.insert(6)
  obj.insert(5)
  obj.insert(4)
  obj.insert(3)
  obj.insert(2)
  obj.insert(1)
  obj.display()


if __name__ == "__main__":
  main()

Output

Circular Linked List Element :  1  2  3  4  5  6  7  8
# Ruby Program 
# Insert circular linked list at beginning position

# Define class of linked list Node
class Node
    # Define the accessor and reader of class Node
    attr_reader :data, :next_node
    attr_accessor :data, :next_node 
  
  def initialize(data, next_node) 
    self.data = data
    self.next_node = next_node
  end
end
class LinkedList
    # Define the accessor and reader of class LinkedList
    attr_reader :head
    attr_accessor :head 
  
  # Class constructors
  def initialize() 
    @head = nil
  end
  # Insert node at begining of linked list
  def insert(value) 
    # Create a node
    node = Node.new(value, @head)
    if (@head == nil) 
      # First node of linked list
      @head = node
      node.next_node = @head
    else 
      temp = @head
      # Find last node
      while (temp.next_node != @head) 
        temp = temp.next_node
      end
      # Add node
      temp.next_node = node
      # make new head node
      @head = node
    end
  end
  # Display node element of circular linked list
  def display() 
    if (@head == nil) 
      print("Empty Linked List")
    else 
      print("Circular Linked List Element  :")
      temp = @head
      while (temp != nil) 
        print(" ", temp.data)
        temp = temp.next_node
        if (temp == @head) 
          break
        end
      end
    end
  end
end
def main() 
  obj = LinkedList.new()
  # insert following linked list nodes
  obj.insert(8)
  obj.insert(7)
  obj.insert(6)
  obj.insert(5)
  obj.insert(4)
  obj.insert(3)
  obj.insert(2)
  obj.insert(1)
  obj.display()
end
main()

Output

Circular Linked List Element  : 1 2 3 4 5 6 7 8
//Scala Program 
//Insert circular linked list at beginning position

import scala.util.control.Breaks._

//Define class of linked list Node
class Node(var data: Int,
  var next: Node) {
}
class LinkedList(var head: Node) {

  //Class constructors
  def this() {
    this(null);
  }
  //Insert node at begining of linked list
  def insert(value: Int): Unit = {
    //Create a node
    var node: Node = new Node(value, head);

    if (head == null) {
      //First node of linked list
      head = node;
      node.next = head;
    } else {
      var temp: Node = head;

      //Find last node
      while (temp.next != head) {
        temp = temp.next;
      }
      //Add node
      temp.next = node;

      //make new head node
      head = node;
    }
  }
  //Display node element of circular linked list
  def display(): Unit = {
    if (head == null) {
      print("Empty Linked List");
    } else {
      print("Circular Linked List Element :");
      var temp: Node = head;
             breakable  
             { 
              while (temp != null) {
                  print(" " + temp.data);
                  temp = temp.next;

                  if (temp == head) {
                      break;
                  }
              }
        }
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: LinkedList = new LinkedList();

    //insert following linked list nodes
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);
    obj.display();
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Swift Program 
//Insert circular linked list at beginning position

//Define class of linked list Node
class Node {
  var data: Int;
  var next: Node? ;
  init(_ data: Int, _ next: Node? ) {
    self.data = data;
    self.next = next;
  }
}
class LinkedList {
  var head: Node? ;
  //Class constructors

  init() {
    self.head = nil;
  }
  //Insert node at begining of linked list
  func insert(_ value: Int) {
    //Create a node
    let node: Node? = Node(value, self.head);
    if (self.head == nil) {
      //First node of linked list
      self.head = node;
      node!.next = self.head;
    } else {
      var temp: Node? = self.head;
      //Find last node
      while ((temp!.next === self.head)==false) {
        temp = temp!.next;
      }
      //Add node
      temp!.next = node;
      //make new head node
      self.head = node;
    }
  }
  //Display node element of circular linked list
  func display() {
    if (self.head == nil) {
      print("Empty Linked List", terminator: "");
    } else {
      print("Circular Linked List Element :", terminator: "");
      var temp: Node? = self.head;
      while (temp != nil) {
        print(" ", temp!.data, terminator: "");
        temp = temp!.next;
        if (temp === self.head) {
          break;
        }
      }
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
}
main();

Output

Circular Linked List Element :  1  2  3  4  5  6  7  8

Second approach: Using of two pointers head and tail to hold the references of first and last linked list node. this process are no need to find last node so time complexity of this approach is O(1).

Insert Circular linked list Node
//C Program 
//Insert linked listnode at beginning of circular single linked list
//using head and tail pointer

#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
  int data;
  struct Node*next;
};
//Use head and tail pointer
struct Node*tail=NULL,*head=NULL;

//insert Node element
void insert(int value)
{
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL)
  {
    printf("Memory overflow\n");
  }
  else
  {
    node->data=value;
    node->next=head;
    if(tail==NULL) 
    {
      tail=node;
    }
    head=node;
    //point last node next reference pointer to first node
    tail->next=head;
  }
}
//display element of linked list
void display()
{

  if(head==NULL)
  {
    printf("Empty linked list");
  }
  else
  {
    struct Node*temp=head;
    while(temp)
    {
      printf("%d  ",temp->data);
      temp=temp->next;
      if(temp==head)
      {
        break; //terminate loop
      }
    }

  }
}
int main()
{

  //insert element of linked list
  insert(8);
  insert(7);
  insert(6);
  insert(5);
  insert(4);
  insert(3);
  insert(2);
  insert(1);
  //display all node
  display();
  return 0;
}

Output

1  2  3  4  5  6  7  8
//C++ Program 
//Insert circular linked list at beginning position
//Using head and tail pointer

#include<iostream>
using namespace std;

//Define class of linked list Node
class Node {
  public:
    int data;
  Node *next;
  Node(int data, Node *next) {
    this->data = data;
    this->next = next;
  }
};
class LinkedList {
  public:
  Node *head;
  Node *tail;
  //Class constructors
  LinkedList() {
    this->head = NULL;
    this->tail = NULL;
  }
  //insert node at beginning of linke list
  void insert(int value) {
    //Create a node
    Node *node = new Node(value, this->head);
    this->head = node;
    if (this->tail == NULL) {
      this->tail = node;
    }
    this->tail->next = this->head;
  }
  //Display node element of circular linked list
  void display() {
    if (this->head == NULL) {
      cout << "Empty Linked List";
    } else {
      cout << "Circular Linked List Element :";
      Node *temp = this->head;
      while (temp != NULL) {
        cout << " " << temp->data;
        temp = temp->next;
        if (temp == this->head) {
          break;
        }
      }
    }
  }
};
int main() {
  LinkedList obj =  LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
  return 0;
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Java Program 
//Insert circular linked list at beginning position
//Using head and tail pointer

//Define class of linked list Node
class Node
{
  public int data;
  public Node next;
  public Node(int data, Node next)
  {
    this.data = data;
    this.next = next;
  }
}
public class LinkedList
{
 
  public Node head;
  public Node tail;

  //Class constructors
  LinkedList()
  {
    head = null;
    tail = null;
  } 
  //insert node at beginning of linke list
  public void insert(int value)
  {
    //Create a node
    Node node = new Node(value,head);

    head = node;

    if(tail==null) 
    {
      tail=node;
    }

    tail.next=head;
    
  }
  //Display node element of circular linked list
  public void display()
  {
    if(head==null)
    {
      System.out.println("Empty Linked List");
    }
    else
    {
      System.out.print("Circular Linked List Element :");

      Node temp = head;

      while(temp!=null)
      {
        System.out.print("  "+temp.data);

        temp=temp.next;

        if(temp==head) 
        {
          break;
        }
      }
    }
  }


  
  public static void main(String[] args) 
  {
    LinkedList obj=new LinkedList();
    //insert following linked list nodes
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);

    obj.display();
    
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
inserting node using head and tail pointer
//C# Program 
//Insert circular linked list at beginning position
//Using head and tail pointer

using System;
//Define class of linked list Node
public class Node {
  public int data;
  public Node next;
  public Node(int data, Node next) {
    this.data = data;
    this.next = next;
  }
}
public class LinkedList {
  public Node head;
  public Node tail;
  //Class constructors
  LinkedList() {
    head = null;
    tail = null;
  }
  //insert node at beginning of linke list
  public void insert(int value) {
    //Create a node
    Node node = new Node(value, head);
    head = node;
    if (tail == null) {
      tail = node;
    }
    tail.next = head;
  }
  //Display node element of circular linked list
  public void display() {
    if (head == null) {
      Console.WriteLine("Empty Linked List");
    } else {
      Console.Write("Circular Linked List Element :");
      Node temp = head;
      while (temp != null) {
        Console.Write(" " + temp.data);
        temp = temp.next;
        if (temp == head) {
          break;;
        }
      }
    }
  }
  public static void Main(String[] args) {
    LinkedList obj = new LinkedList();
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);
    obj.display();
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
<?php
//Php Program 
//Insert circular linked list at beginning position
//Using head and tail pointer

//Define class of linked list Node
class Node {
  public $data;
  public $next;

  function __construct($data, $next) {
    $this->data = $data;
    $this->next = $next;
  }
}
class LinkedList {
  public $head;
  public $tail;
  //Class constructors

  function __construct() {
    $this->head = null;
    $this->tail = null;
  }
  //insert node at beginning of linke list

  public  function insert($value) {
    //Create a node
    $node = new Node($value, $this->head);
    $this->head = $node;
    if ($this->tail == null) {
      $this->tail = $node;
    }
    $this->tail->next = $this->head;
  }
  //Display node element of circular linked list

  public  function display() {
    if ($this->head == null) {
      echo("Empty Linked List");
    } else {
      echo("Circular Linked List Element :");
      $temp = $this->head;
      while ($temp != null) {
        echo(" ". $temp->data);
        $temp = $temp->next;
        if ($temp == $this->head) {
          break;
        }
      }
    }
  }
}

function main() {
  $obj = new LinkedList();
  //insert following linked list nodes
  $obj->insert(8);
  $obj->insert(7);
  $obj->insert(6);
  $obj->insert(5);
  $obj->insert(4);
  $obj->insert(3);
  $obj->insert(2);
  $obj->insert(1);
  $obj->display();

}
main();

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Node Js Program 
//Insert circular linked list at beginning position
//Using head and tail pointer

//Define class of linked list Node
class Node {
  constructor(data, next) {
    this.data = data;
    this.next = next;
  }
}
class LinkedList {
  //Class constructors

  constructor() {
    this.head = null;
    this.tail = null;
  }

  //insert node at beginning of linke list
  insert(value) {
    //Create a node
    var node = new Node(value, this.head);
    this.head = node;
    if (this.tail == null) {
      this.tail = node;
    }
    this.tail.next = this.head;
  }

  //Display node element of circular linked list
  display() {
    if (this.head == null) {
      process.stdout.write("Empty Linked List");
    } else {
      process.stdout.write("Circular Linked List Element :");
      var temp = this.head;
      while (temp != null) {
        process.stdout.write(" " + temp.data);
        temp = temp.next;
        if (temp == this.head) {
          break;
        }
      }
    }
  }
}

function main(args) {
  var obj = new LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
}

main();

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
# Python 3 Program 
# Insert circular linked list at beginning position
# Using head and tail pointer

# Define class of linked list Node
class Node :
  
  def __init__(self, data, next) :
    self.data = data
    self.next = next
  

class LinkedList :
  
  # Class constructors
  def __init__(self) :
    self.head = None
    self.tail = None
  
  # insert node at beginning of linke list
  def insert(self, value) :
    # Create a node
    node = Node(value, self.head)
    self.head = node
    if (self.tail == None) :
      self.tail = node
    
    self.tail.next = self.head
  
  # Display node element of circular linked list
  def display(self) :
    if (self.head == None) :
      print("Empty Linked List", end = "")
    else :
      print("Circular Linked List Element :", end = "")
      temp = self.head
      while (temp != None) :
        print(" ", temp.data, end = "")
        temp = temp.next
        if (temp == self.head) :
          break
        
      
    
  

def main() :
  obj = LinkedList()
  # insert following linked list nodes
  obj.insert(8)
  obj.insert(7)
  obj.insert(6)
  obj.insert(5)
  obj.insert(4)
  obj.insert(3)
  obj.insert(2)
  obj.insert(1)
  obj.display()


if __name__ == "__main__":
  main()

Output

Circular Linked List Element :  1  2  3  4  5  6  7  8
# Ruby Program 
# Insert circular linked list at beginning position
# Using head and tail nodes

# Define class of linked list Node
class Node
    # Define the accessor and reader of class Node
    attr_reader :data, :next_node
    attr_accessor :data, :next_node 
  
  def initialize(data, next_node) 
    self.data = data
    self.next_node = next_node
  end
end
class LinkedList
    # Define the accessor and reader of class LinkedList
    attr_reader :head, :tail
    attr_accessor :head, :tail 
  
  # Class constructors

  def initialize() 
    @head = nil
    @tail = nil
  end
  # insert node at beginning of linke list
  def insert(value) 
    # Create a node
    node = Node.new(value, @head)
    @head = node
    if (@tail == nil) 
      @tail = node
    end
    @tail.next_node = @head
  end
  # Display node element of circular linked list
  def display() 
    if (@head == nil) 
      print("Empty Linked List")
    else 
      print("Circular Linked List Element  :")
      temp = @head
      while (temp != nil) 
        print(" ", temp.data)
        temp = temp.next_node
        if (temp == @head) 
          break
        end
      end
    end
  end
end
def main() 
  obj = LinkedList.new()
  # insert following linked list nodes
  obj.insert(8)
  obj.insert(7)
  obj.insert(6)
  obj.insert(5)
  obj.insert(4)
  obj.insert(3)
  obj.insert(2)
  obj.insert(1)
  obj.display()
end
main()

Output

Circular Linked List Element  : 1 2 3 4 5 6 7 8
//Scala Program 
//Insert circular linked list at beginning position
//Using head and tail nodes

import scala.util.control.Breaks._

//Define class of linked list Node
class Node(var data: Int,
  var next: Node) {
}
class LinkedList(var head: Node,
  var tail: Node) {


  //Class constructors
  def this() {
    this(null,null);
  }
  //insert node at beginning of linke list
  def insert(value: Int): Unit = {
    //Create a node
    var node: Node = new Node(value, head);
    head = node;
    if (tail == null) {
      tail = node;
    }
    tail.next = head;
  }
  //Display node element of circular linked list
  def display(): Unit = {
    if (head == null) {
      print("Empty Linked List");
    } else {
      print("Circular Linked List Element :");
      var temp: Node = head;
       breakable  
             { 
                while (temp != null) {
                    print(" " + temp.data);
                    temp = temp.next;

                    if (temp == head) {
                        break;
                    }
                }
             }
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: LinkedList = new LinkedList();

    //insert following linked list nodes
    obj.insert(8);
    obj.insert(7);
    obj.insert(6);
    obj.insert(5);
    obj.insert(4);
    obj.insert(3);
    obj.insert(2);
    obj.insert(1);
    obj.display();
  }
}

Output

Circular Linked List Element : 1 2 3 4 5 6 7 8
//Swift Program 
//Insert circular linked list at beginning position
//Using head and tail nodes

//Define class of linked list Node
class Node {
  var data: Int;
  var next: Node? ;
  init(_ data: Int, _ next: Node? ) {
    self.data = data;
    self.next = next;
  }
}
class LinkedList {
  var head: Node? ;
  var tail: Node? ;
  //Class constructors

  init() {
    self.head = nil;
    self.tail = nil;
  }
  //insert node at beginning of linke list
  func insert(_ value: Int) {
    //Create a node
    let node: Node? = Node(value, self.head);
    self.head = node;
    if (self.tail == nil) {
      self.tail = node;
    }
    self.tail!.next = self.head;
  }
  //Display node element of circular linked list
  func display() {
    if (self.head == nil) {
      print("Empty Linked List", terminator: "");
    } else {
      print("Circular Linked List Element :", terminator: "");
      var temp: Node? = self.head;
      while (temp != nil) {
        print(" ", temp!.data, terminator: "");
        temp = temp!.next;
        if (temp === self.head) {
          break;
        }
      }
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  //insert following linked list nodes
  obj.insert(8);
  obj.insert(7);
  obj.insert(6);
  obj.insert(5);
  obj.insert(4);
  obj.insert(3);
  obj.insert(2);
  obj.insert(1);
  obj.display();
}
main();

Output

Circular Linked List Element :  1  2  3  4  5  6  7  8

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