Thursday, 11 February 2016

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


No comments:

Post a Comment