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");
}
}
}

Data Structure Circular Singly Linked List Using Java

            Data Structure Circular Singly Linked List


  • For creating a Node in java ,I have written following code. 
//creating a node of user defined type Test.
public class Test {
//here link is reference of node which contains the address of next node.
Test link;
//data is the value of node.
int data;
public Test(int data){
this.data=data;
}
//we call toString method to return address in string format.
public String toString(){
return hashCode()+"";
}

}

  • for perform all operation we write following code
package amritesh.singh.CSLL; import java.util.Scanner; //Perform Circular Singly Linked List inside class CSLL public class CSLL { // end works as a reference of node public static Test end=null; //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("Enter How Many Node you want to Create"); int node=sc.nextInt(); for(int i=0;i<node;i++){ create(); } break; case 2: traverse(); break; case 3: reverse(); break; case 4: System.out.println("Enter the position to insert a Node"); int position=sc.nextInt(); addAtPosition(position); break; case 5: System.out.println("Enter Node value which you want to delete "); int nodeValue=sc.nextInt(); delete(nodeValue); break; } } } //for creating node we call this method public static void create(){ System.out.println("Enter Node Value "); int nodeValue=sc.nextInt(); //we have taken reference temp of type test Test temp=null; //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); //this is circular Linkedlist so only one node is created till now therefore assign temp value in temp link temp.link=temp; //now end pointing to temp end=temp; System.out.println(temp.data+">>>>>>"+temp.link); return; }else{ //for creating a node we write following temp=new Test(nodeValue); //for not disturbing end value we write reference copy and assign link of end to copy. Test copy=end.link; //it will iterate until copy link value not equal to link of end while(copy.link!=end.link){ //assign link of copy into copy copy=copy.link; } //assign reference link of copy into link of temp temp.link=copy.link; //assign reference temp into link of copy copy.link=temp; //for display result on console System.out.println(temp.data+">>>>>>"+temp.link); end=temp; } } //for visiting node we write this method public static void traverse(){ //if there is no node available then execute if condition otherwise execute else if(end==null){ System.out.println("Empty Circular Singly Linked List"); return; }else{ //for not disturbing end value we write reference copy and assign link of end to copy. Test copy=end.link; //it will iterate until copy link value not equal to link of end while(copy.link!=end.link){ System.out.println(copy.data+">>>>>>"+copy.link); copy=copy.link; } System.out.println(copy.data+">>>>>>"+copy.link); } } //for reverse operation public static void reverse(){ //if there is no node available then execute if condition otherwise execute else if(end==null){ System.out.println("Empty Circular Linked List"); return; }else{ //for not disturbing end value we write reference copy and assign link of end to copy. Test copy=end.link, q1=end; //it will iterate until copy link value not equal to link of end while(copy.link!=end.link){ System.out.println(copy.data+">>>>>>"+copy.link); Test q=copy.link; copy.link=q1; q1=copy; copy=q; } //following code workes for reverse last node Test q2=copy.link; copy.link=q1; q1=copy; end=q2; } } //for inserting a node at any position public static void addAtPosition(int position){ System.out.println("Enter Node Value to Insert"); int nodeValue=sc.nextInt(); Test temp= new Test(nodeValue); //if there is no node available then execute if condition otherwise execute else if(end==null){ System.out.println("Empty Circular Singly Linked List"); return; }else{ //for not disturbing end value we write reference copy and assign link of end to copy. Test copy=end.link; //insert a node at first position if(position==0){ temp.link=copy; end.link=temp; copy=temp; System.out.println(copy.data+">>>>>"+copy.link); return; } //iterate until i value less than (postion-1) for(int i=0;i<position-1;i++){ copy=copy.link; } //insert a node at last postion if(copy==end){ temp.link=copy.link; copy.link=temp; end=temp; System.out.println(copy.data+">>>>>"+copy.link); return; } //following code written for inserting a node at middle temp.link=copy.link; copy.link=temp; System.out.println(copy.data+">>>>>"+copy.link); return; } }
//for performing Delete operation public static void delete(int data){ //if there is no node available then execute if condition otherwise execute else if(end==null){ System.out.println("Empty Circular Singly Linked List"); return; }else{ //for not disturbing end value we write reference copy and assign link of end to copy. Test copy=end.link; //if only one node is available then execute if(copy==end){ end=null; System.out.println("Element Deleted Successfully"); return; } //for delelte node at first position if(copy.data==data){ Test q=copy; end.link=copy.link; copy=copy.link; System.out.println("Element Deleted Successfully for first position"); q=null; return; }else{ //it will iterate until link of link of copy value equal to data while(copy.link.link!=end.link){ //data of link of copy equal to value,means delete node at middle if(copy.link.data==data){ Test q=copy.link; copy.link=q.link; System.out.println("Element Deleted Successfully for middle position"); q=null; return; } copy=copy.link; } //data of link of copy equal to value,means delete node at last position if(copy.link.data==data){ Test q1=copy.link; copy.link=q1.link; end=copy; System.out.println("Element Deleted Successfully for last position"); q1=null; return; } } System.out.println("Element does not Exist"); } } }


Saturday 6 February 2016

Data Structure Singly Linked List Using Java

Data Structure Singly Linked List

  • For creating a Node in java ,I have written following code. 


//creating a node of user defined type Test.
public class Test {
//here link is reference of node which contains the address of next node.
Test link;
//data is the value of node.
int data;
public Test(int data){
this.data=data;
}
//we call toString method to return address in string format.
public String toString(){
return hashCode()+"";
}

}

  • for perform all operation we write following code


package amritesh.singh.singly;

import java.util.Scanner;

public class SinglyLinkedList {
// cursor works as a reference of node
static Test cursor=null;
//Scanner is used to take input from user
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
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 Number to Perform Operation");
int operation =sc.nextInt();
switch(operation){
case 1:
System.out.println("How many node you want to create");
int node=sc.nextInt();
for(int i=1;i<=node;i++){
create();
}
break;
case 2:
traverse();
break;
case 3:
reverse();
break;
case 4:
System.out.println("Enter Position to add Node");
int position=sc.nextInt();
addAtPosition(position);
break;
case 5:
System.out.println("Enter Value to delete");
int value =sc.nextInt();
delete(value);
break;
}
}
}
//for creating a node we call this method
public static void create(){
System.out.println("Enter Node value you want to create");
int value=sc.nextInt();
//we have taken reference temp of type test
Test temp=null;
//At the time of creating first node if condition will execute other wise else condition will execute
if(cursor==null){
//for create a node
temp = new Test(value);
//assign temp value to cursor
cursor = temp;
System.out.println(temp.data+">>>>"+temp.link);
//create method return type is void so we write only return
return;
}
else{
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy = cursor;
//for creating a node we write following 
temp = new Test(value);
//it will iterate until copy link value not equal to null
while(copy.link!=null){
//assign link of copy into copy
copy=copy.link;
}
//assign reference temp into link of copy
copy.link=temp;
//for display result on console
System.out.println(temp.data+">>>>"+copy.link);
}
}
//for visiting node we write this method
public static void traverse(){
//if there is no node available then execute if condition otherwise execute else
if(cursor==null){
System.out.println("Empty Singly linked List");
return;
}else{
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
//iterate until copy value not equal to null
while(copy!=null){
System.out.println(copy.data+">>>>>"+copy.link);
//assign link of copy into copy
copy=copy.link;
}
}
}
//for reverse operation 
public static void reverse(){
//if there is no node available then execute if condition otherwise execute else
if(cursor==null){
System.out.println("Empty Singly linked list");
return;
}else{
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
Test q=null,q1=null;
//iterate until copy value not equal to null
while(copy != null){
System.out.println(copy.data+">>>>>"+copy.link);
//assign link of copy into referece q 
q=copy.link;
//assign referece q1 into link of copy
copy.link=q1;
//assign copy into q1
q1=copy;
//assign q into copy
copy=q;
}
//assign q1 into cursor
cursor=q1;
}
}
//for inserting a node at any position
public static void addAtPosition(int position){
//if there is no node available then execute if condition otherwise execute else
if(cursor==null){
System.out.println("Empty Singly Linked List");
return;
}
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
System.out.println("Enter Node Value");
int value = sc.nextInt();
//for creating a node
Test temp = new Test(value);
//iterate until j value less than (postion-1) and copy not equalto null
for(int j=0;j<position-1 && copy!=null ;j++){
//assign link of copy into copy
copy=copy.link;
}
//insert a node at first position
if(position==0){
cursor=temp;
temp.link=copy;
return;
}
//insert a node at last postion
if(copy.link==null){
copy.link=temp;
return;
}
//following code written for inserting a node at middle
Test q1=copy.link;
copy.link=temp;
temp.link=q1;
System.out.println(temp.data+">>>"+temp.link);
}
//for deleting a node
public static void delete(int value){
//if there is no node available then execute if condition otherwise execute else
if(cursor==null){
System.out.println("Empty Singly Linked list");
return;
}
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
//if only one node is available then execute 
if(copy.link==null && copy.data==value ){
cursor=null;
copy=null;
return;
}
//for delelte node at first position
if(copy.data==value){
cursor=null;
copy=null;
System.out.println("Element Deleted Successfully at first position");
return;
}else{
//iterate until link of link of copy not equal to null
while(copy.link.link != null){
//data of link of copy equal to value,means delete node at middle
if(copy.link.data==value){
Test q1=copy.link;
copy.link=copy.link.link;
System.out.println("Element Deleted Successfully at middle position");
q1=null;
return;
}
copy=copy.link;
}
//data of link of copy equal to value,means delete node at last position 
if(copy.link.data==value){
Test q1=copy.link;
copy.link=q1.link;
System.out.println("Element Deleted Successfully at last position");
q1=null;
return;
}
}
System.out.println("Element does not Exist");
}



}