circular doubly linked list insertion at the end in java

 circular doubly linked list insertion at the end in java

public class cdll {
class Node {
int data;
Node prev;
Node next;

Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Node head=null;
Node tail=null;
public void toadd(int data){
Node newnode=new Node(data);
if (head==null){
head=newnode;
tail=newnode;
newnode.next=head;
newnode.prev=tail;
}
else{
tail.next=newnode;
newnode.prev=tail;
tail=newnode;
tail.next=head;
head.prev=tail;
}
}

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 addlast(int data){
Node newnode=new Node(data);
tail.next=newnode;
newnode.prev=tail;
newnode.next=head;
head.prev=newnode;
tail=newnode;
    }
public static void main(String[] args) {
cdll cc=new cdll();
cc.toadd(10);
cc.toadd(20);
cc.toadd(45);
cc.toadd(70);
cc.dis();
System.out.println("*************");
        cc.addlast(44);
        cc.dis();
}
}

Comments

Popular posts from this blog

Back / Flip Cover Universal List

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