Saturday 2 January 2016

Data Structure Doubly Linked List Example


                                  Data Structure 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.chauhan;

import java.util.Scanner;

public class DLLCreation {
// 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("program Start");

//It is used to iterate every time after complete an operation.
while(true){
System.out.println("Enter value to perform operation");
int perform = sc.nextInt();
switch(perform){
case 1:
System.out.println("Enter 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:
System.out.println("Enter specific position to add node");
int position = sc.nextInt();
addAtPosition(position);
break;
case 4:
reverse();
return;
case 5:
System.out.println("Enter Element data to delete");
int elementData = sc.nextInt();
delete(elementData);
break;

}
}

}
//for creating a node we call this method
public static void create(){
System.out.println("Enter Node value ");
//we have taken reference temp of type test
Test temp=null;
int node1=sc.nextInt();
//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(node1);
//assign temp value to cursor
cursor=temp;
System.out.println(temp.previous+">>>>>"+node1 +">>>>>>"+cursor.next+">>>"+cursor);
//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;
copy=cursor;
//for creating a node we write following 
temp = new Test(node1);
//it will iterate until copy next value not equal to null
while(copy.next!=null){
//assign next of copy into copy
copy =copy.next;
}

copy.next=temp;
temp.previous=copy;
//for display result on console
System.out.println(temp.previous+">>>>>"+node1 +">>>>>>"+copy.next+">>>"+copy);
}
}
//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 DLL");
return;
}
//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.previous+">>>>>"+copy.data +">>>>>>"+copy.next+">>>"+copy);
//assign next of copy into copy
copy = copy.next;

}
// System.out.println(copy.previous+">>>>>"+copy +">>>>>>"+copy.next+">>>"+copy);
}
//for inserting a node at any position
public static void addAtPosition(int position){
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
//iterate until j value less than (postion-1) and copy not equalto null
for(int i=0;i<position-1 && copy!=null ;i++){
System.out.println("inside for loop");
//assign next of copy into copy
copy = copy.next;
}
//if there is no node available then execute if condition otherwise execute else
if(copy==null){
System.out.println("Invalid pattern");
return;
}
System.out.println("Enter Node Value");
int node = sc.nextInt();
//for creating a node
Test temp = new Test(node);
//insert a node at first position
if(position==0){
cursor=temp;
temp.next=copy;
copy.previous=temp;
System.out.println(temp.previous+">>>>>"+temp.data+"<<<<<"+temp.next);
return;

}
//insert a node at last postion
if(copy.next==null)
{
copy.next=temp;
temp.previous=copy;
System.out.println(temp.previous+">>>>>"+temp.data+"<<<<<"+temp.next);
return;
}
//following code written for inserting a node at middle
Test copyNext = copy.next;
copy.next=temp;
temp.previous=copy;
temp.next=copyNext;
copyNext.previous=temp;
System.out.println(temp.previous+">>>>>"+temp.data+"<<<<<"+temp.next);


}
//for performing 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 DLL");
return;
}
//for not disturbing cursor value we write reference copy and assign cursor to copy.
Test copy=cursor;
Test copyAmritesh=null,copyAmritesh2=null;
//iterate until copy value not equal to null
while(copy!=null){
//following code used for reverse whole node except last node
 copyAmritesh=copy.next;
copy.next=copy.previous;
copy.previous=copyAmritesh;
copyAmritesh2=copy;
copy=copyAmritesh;

}
//following code used for reverse  last node only
cursor=copyAmritesh2;
copy=cursor;
while(copy!=null){
System.out.println(copy.previous+">>>>>"+copy.data+"<<<<<"+copy.next);
copy=copy.next;
}
return;
}
//for deleting a node
public static void delete(int data){
boolean delItem = false;
//if there is no node available then execute if condition otherwise execute else
if(cursor == null){
delItem=true;
System.out.println("Doubly LinkedList is Empty");
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.next==null && copy.previous==null && copy.data==data){
delItem=true;
cursor=null;
copy=null;
System.out.println("Element data deleted :"+data);
return;
}
//for delelte node at first position
if(copy.data==data){
delItem=true;
Test elementItem = copy.next;
elementItem.previous =null;
cursor=elementItem;
System.out.println("Element data deleted :"+data);
return;
}else{
//iterate until next of next of copy not equal to null
while(copy.next.next != null){
//data of next of copy equal to value,means delete node at middle
if(copy.next.data==data){
delItem=true;
Test elementItem=copy.next.next;
copy.next=elementItem;
elementItem.previous=copy;
Test deletedItem=copy.next;
deletedItem=null;
System.out.println("Element data deleted :"+data);
}
copy=copy.next;
}
//data of next of copy equal to value,means delete node at last position 
if(copy.next.data==data){
delItem=true;
Test elementItem=copy.next;
copy.next=null;
elementItem=null;
return;
}
}
if(delItem==false)
System.out.println("You have Enterd Envalid element");

}

}