Thursday, 11 February 2016

Data Structure Circular Doubly Linked List Example

                                Data Structure Circular Doubly Linked List

For Creating A Node in Java write folloing program

package amritesh.singh.chauhan;

public class Test {
Test next,previous;
int data;
public Test(int data){
this.data=data;
}

public String toString(){
return hashCode()+" ";
}

}


  • for perform all operation we write following code

package amritesh.singh.CDLL;

import java.util.Scanner;
//create class CDLL to perform all operation of circular Doubly Linked List
public class CDLL {
//Scanner is used to take input from user
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("program start");
System.out.println("1.Create"+"\t"+"2. Traverse"+"\t"+"3.Reverse  "+"\t"+"4. AddAtPosition"+"\t"+"5. Delete");
//It is used to iterate every time after complete an operation.
while(true){
System.out.println("Enter value to perform operation");

int operation = sc.nextInt();
switch(operation){
case 1:
System.out.println("How many Node you want to create");
int num=sc.nextInt();
for(int i=0;i<num;i++){
create();
}
break;
case 2:
traverse();
break;
case 3:
reverse();
break;
case 4:
System.out.println("Enter Position to insert a node");
int position=sc.nextInt();
addAtposition(position);
break;
case 5:
System.out.println("Enter Element data You want to Delete");
int data=sc.nextInt();
delete(data);
break;
}
}
}
// end works as a reference of node
static Test end=null;
//for creating  node we call this method
public static void create(){
//we have taken reference temp of type test
Test temp=null;
System.out.println("Enter Node value to create");
int nodevalue =sc.nextInt();
//At the time of creating first node if condition will execute other wise else condition will execute
if(end==null){
                 //for create a node
temp=new Test(nodevalue);
                   //assign temp into next of temp
temp.next=temp;
                   //assign temp into temp previous
temp.privious=temp;
                   // display value into console
System.out.println(temp.privious+">>>>"+nodevalue+">>>>"+temp.next);
                    //assign temp into end 
end=temp;
}else{
               //for not disturbing end value we write reference copy and assign cursor to copy.
temp = new Test(nodevalue);
                   //assign next of end into copy
Test copy = end.next;
                //it will iterate until next of copy value not equal to next of end
while(copy.next != end.next){
copy=copy.next;
}
      //assign next of copy into q of type test
Test q=copy.next;
      //assign temp into previous of q 
q.privious=temp;
 //assign next of temp into  q 
temp.next=q;
             //assign copy into previous of temp
temp.privious=copy;
 //assign temp into next of copy
copy.next=temp;
         // following code use to display 
System.out.println(temp.privious+">>>>"+nodevalue+">>>>"+temp.next);
end=temp;
}
}
//for displaying  node we call this method
public static void traverse(){
//At the time of creating first node if condition will execute other wise else condition will execute
if(end==null){
System.out.println("CDLL is Empty");
return;
}else{
Test copy=end.next;
 //it will iterate until next of copy value not equal to next of end
while(copy.next != end.next){
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
                 // assign next of copy into copy
copy=copy.next;
}
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);


}
}
//for reverse node we call this method
public static void reverse(){
//At the time of creating first node if condition will execute other wise else condition will execute
if(end==null){
System.out.println("Empty Circular Doubly LinkedList");
return;
}
else{
Test copy=end.next,q=null,q1=null;
 //it will iterate until next of copy value not equal to next of end
while(copy.next != end.next){
//following code used for reverse whole node except last node
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
q= copy.next;
copy.next=copy.privious;
copy.privious=q;
copy=q;
}
//following code used for reverse  last node only
q1= copy.next;
copy.next=copy.privious;
copy.privious=q1;
end=q1;
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
}
}
//for add At specific position node we call this method
public static void addAtposition(int position){
Test temp=null;
System.out.println("Enter Node Value to Insert");
int value= sc.nextInt();
temp=new Test(value);
//At the time of creating first node if condition will execute other wise else condition will execute
if(end==null){
System.out.println("Empty Circular Doubly Linked List");
return;
}else{
Test copy=end.next;
//insert a node at first position
if(position==0){
temp.privious=copy.privious;
temp.next=copy;
copy.privious.next=temp;
copy.privious=temp;
copy=temp;
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
return;
}
//iterate until i value less than (postion-1)
for(int i=0;i<position-1;i++){
copy=copy.next;
}
//insert a node at last postion
if(copy.next==end.next){
copy.next.privious=temp;
temp.next=copy.next;
temp.privious=copy;
copy.next=temp;
end=temp;
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
return;
}
//following code written for inserting a node at middle
Test q=copy.next;
temp.next=q;
temp.privious=copy;
q.privious=temp;
copy.next=temp;
System.out.println(copy.privious+">>>>"+copy.data+">>>>"+copy.next);
return;

}
}
//for deleting At specific position node we call this method
public static void delete(int data){
//At the time of creating first node if condition will execute other wise else condition will execute
if(end==null){
System.out.println("Empty Circular Doubly Linked List");
return;
}else{
Test copy=end.next;
//if only one node is available then execute
if(copy==end){
System.out.println("successfully Deleted Element = "+end.data);
end=null;
return;
}
                //for delelte node at first position
if(copy.data==data){
Test q=copy;
q.privious.next=q.next;
q.next.privious=q.privious;
System.out.println("successfully Deleted Element = "+q.data);
q=null;
return;
}
else{
 //it will iterate until next of next of copy value not equal to next of end
while(copy.next.next!=end.next){
              //data of next of next of copy equal to data,means delete node at middle position 
if(copy.next.data==data){
Test q=copy.next;
q.next.privious=copy;
copy.next=q.next;
System.out.println("successfully Deleted Element = "+q.data);
q=null;
return;
}
                //assign next of copy into copy
copy=copy.next;
}
//data of next of next of copy equal to data,means delete node at last position 
if(copy.next.data==data){
Test q=copy.next;
copy.next=q.next;
q.next.privious=copy;
System.out.println("successfully Deleted Element = "+q.data);
q=null;
return;
}
}
System.out.println("Element does not exist");
}
}
}

No comments:

Post a Comment