circular linked list insertion at begining

  singly circular linked list insertion at first position

import java.util.Scanner;

public class cll {
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}

}
Node head=null;
Node tail=null;
public void toadd(int data){
Node newnode=new Node(data);
if (head==null){
head=newnode;
tail=newnode;
tail.next=head;
}
else{
tail.next=newnode;
tail=newnode;
tail.next=head;
}
}
public void dis(){
Node temp=head;
if (head==null){
System.out.println("empty");
}
else{
do {
System.out.println(temp.data);
temp=temp.next;
}while (temp!=head);
}
}
    public void addfirst(int data){
Node newNode=new Node(data);
if (head==null){
head=newNode;
tail=newNode;
tail.next=head;
}
else{
newNode.next=head;
head=newNode;
tail.next=head;
}
    }
public static void main(String[] args) {
cll cc=new cll();
cc.toadd(10);
cc.toadd(20);
cc.toadd(30);
cc.toadd(40);
cc.dis();
    System.out.println("*******");
    cc.addfirst(55);
    cc.dis();
}
}

Comments

Popular posts from this blog

Back / Flip Cover Universal List

Super-D, 11D, Super X tempered glass Universal list