Sunday, 28 March 2021

Circular Queue

Queue is first in first out (FIFO) data structure.we will implement queue data structure with one front and one rear end and one queue of array.
Initially front and rear end value will be [-1].
FRONT :- when we delete data from queue then front will increment.
REAR :- when we add element in queue then rear will increment.




package com.java.cqds;

import java.util.Scanner;

public class CircularQeue {
public static int[] queue=new int[5];
public static int front=-1,rear=-1;

public static void main(String[] args) {
System.out.println("Jai shree ram !!!");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Create \t2. Display \t3. Delete");
int operation = sc.nextInt();
switch (operation) {

case 1:
System.out.println("Enter the queue value to create");
int data = sc.nextInt();
create(data);
break;
case 2:
display();
break;
case 3:
delete();
break;
default:
System.out.println("Operation not found !!!");
break;
}
}


}


Below method is used to create the queue.

private static void create(int data) {
if((front==0 && rear==queue.length-1)||(front==rear+1)){
System.out.println("Quee is full");
return;
}else{
if(front==-1 && rear==-1){
rear++;
front++;
}else{
rear++;
}
if(rear==queue.length){
rear=0;
}
queue[rear]=data;
}
}


Below method is used to display the queue.


private static void display() {
if(front==-1 && rear==-1){
System.out.println("Empty circular queue");
return;
}else{
int front1=front;
int rear1=rear;
if(front1>rear1){
while(front1<=queue.length-1){
System.out.println(queue[front1++]+" ");
}
front1=0;
while(front1<=rear1){
System.out.println(queue[front++]+" ");
}
}else{
while(front1<=rear1){
System.out.println(queue[front++]+" ");
}
}
}
}


Below method is used to delete the queue.


private static void delete() {
if(front==-1 && rear==-1){
System.out.println("Empty Circular linked list");
return;
}else{
System.out.println(queue[front]+"");
if(front==queue.length-1){
front=0;
}else{
front++;
}
if((front==rear+1)||(front==0 && rear==queue.length-1)){
rear=front=-1;
}
}
}



}

Regular Queue

 Queue is first in first out (FIFO) data structure.we will implement queue data structure with one front and one rear end and one queue of array.
Initially front and rear end value will be [-1].
FRONT :- when we delete data from queue then front will increment.
REAR :- when we add element in queue then rear will increment.



package com.java.qds;

import java.util.Scanner;

public class RegularQueue {

public static int front = -1, rear = -1;
public static int[] queue = new int[5];

public static void main(String[] args) {

System.out.println("Jai shree ram !!!");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Create \t2. Display \t3. Delete");
int operation = sc.nextInt();
switch (operation) {

case 1:
System.out.println("Enter the queue value to create");
int data = sc.nextInt();
create(data);
break;
case 2:
display();
break;
case 3:
delete();
break;
default:
System.out.println("Operation not found !!!");
break;
}
}

}

Below method is used to create the queue.


public static void create(int data) {
if(rear==queue.length-1){
System.out.println("Queue is overflow !!!");
return;
}
if(front==-1 && rear==-1){
rear++;
front++;
}else{
rear++;
}
queue[rear]=data;

}

Below method is used to display element in queue.


public static void display() {
if((front==-1 && rear==-1) || front==rear+1){
System.out.println("Empty Queue");
return;
}
for(int i=front;i<=rear;i++){
System.out.println(queue[i]);
}

/*///OR
int j=front;
while(j<=rear){
System.out.println(queue[j++]);
}*/
}

Below Method is uses to delete the element from queue.


public static void delete() {

if((front==-1 && rear==-1) || front==rear+1){
System.out.println("Empty queue");
return;
}
System.out.println("Deleted element is "+queue[front++]);
}

}


Saturday, 27 March 2021

Binary Search Tree using java

 Create a Node Using java for binary search tree.we will use this node in creating node in binary search tree .Traversing Inorder ,preorder and postorder traversal. It is also used in searching a node or deleting a node.

package com.java.bstds;
public class Node {
int data;
Node left,right;

public Node(int data){
this.data=data;
}
public String toString(){
return hashCode()+"";
}
}


To Implement the logic I am writing following code inside main method so that we can used all services.


package com.java.bstds;

import java.util.Scanner;

public class BST {

public static Node root;

// For Searching element we used below reference
public static Node avail, parent;

public static void main(String[] args) {
System.out.println("Jai shree Ganesh !!!");

Scanner sc = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println(
"1. Create \t2. Inorder Traversal \t3. Preorder Travelsal \t4. Postorder Traversal \t5. Search Element \t6. Delete");
int operation = sc.nextInt();
switch (operation) {
case 1:
System.out.println("How many number of node you want to create");
int noOfNode = sc.nextInt();
for (int i = 0; i < noOfNode; i++) {
System.out.println("Enter the node value to create");
int data = sc.nextInt();
create(data);
}
break;
case 2:
inOrderTraversal();
break;
case 3:
preOrderTraversal();
break;
case 4:
postOrderTraversal();
break;
case 5:
System.out.println("Enter element which you want to search");
int ele = sc.nextInt();
Node avl = search(ele);
if (avl != null) {
System.out.println("Element is " + avail.data);
if (parent != null) {
System.out.println("which parent is " + parent.data);
}
} else {
System.out.println("Element Not Found");
}
break;
case 6:
System.out.println("Enter node value which you want to delete");
int value = sc.nextInt();
delete(value);
break;
default:
System.out.println("Not found any index");
break;

}
}

}
}


To create a node and add element greater than root node is right side of the root and element less than the root will store inside left side of the root.To implement that logic we have gone through below case.


public static void create(int data) {
Node temp = new Node(data);
if (root == null) {
root = temp;
return;
} else {
Node copyRoot = root;
Node newNode = null;
while (copyRoot != null) {
newNode = copyRoot;
if (copyRoot.data > data) {
copyRoot = copyRoot.left;
} else if (copyRoot.data < data) {
copyRoot = copyRoot.right;
} else {
copyRoot = null;
}
}

if (newNode.data > data) {
newNode.left = temp;
} else if (newNode.data < data) {
newNode.right = temp;
} else if (newNode.data == data) {
temp = null;
}

}
}


To implement the Traversal in tree we have three ways to traverse the tree .
1. Inorder Traversal(LNR)
2. Preorder Traversal(NLR)
3. Postorder Traversal(LRN)
So let us see one by one.
1. Inorder traversal : First it will traverse left node then root and then right node.This rule is applicable for all node.


public static void inOrderTraversal() {
if (root == null) {
System.out.println("Empty BST ");
return;
} else {
Node copyRoot = root;
Node[] stack = new Node[20];
int top = -1;

try {
while (true) {
while (copyRoot != null) {
stack[++top] = copyRoot;
copyRoot = copyRoot.left;
}
copyRoot = stack[top--];
if (copyRoot.right != null) {
System.out.print("  " + copyRoot.data + " ");
copyRoot = copyRoot.right;
} else {
System.out.print(" " + copyRoot.data + " ");
copyRoot = null;
}
}

} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}


2. Preorder Traversal(NLR) : first it will traverse root then left and then right and it is applicable of all node.


public static void preOrderTraversal() {
if (root == null) {
System.out.println("Empty BST");
return;
} else {
Node copyRoot = root;
Node[] stack = new Node[20];
int top = -1;
try {
while (true) {
while (copyRoot != null) {
System.out.print(copyRoot.data + " ");
stack[++top] = copyRoot;
copyRoot = copyRoot.left;
}
copyRoot = stack[top--];
if (copyRoot.right != null) {
copyRoot = copyRoot.right;
} else {
copyRoot = null;
}
}

} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}

3.Postorder Traversal (LRN):- In this traversal we have first gone through left node the right node and then root node. and it is applicable for all node. Root node will be printed only when its is traverse at 3rd times.

public static void postOrderTraversal() {

if (root == null) {
System.out.println("Empty BST");
return;
} else {
Node copyRoot = root;
Node[] stack = new Node[20];
int top = -1;

try {
while (true) {
while (copyRoot != null) {
if (copyRoot.right != null) {
stack[++top] = copyRoot.right;
}
stack[++top] = copyRoot;
copyRoot = copyRoot.left;
}

copyRoot = stack[top--];
if (top != -1 && copyRoot.right != null && stack[top] == copyRoot.right) {
stack[top] = copyRoot;
copyRoot = copyRoot.right;
} else {
System.out.print(copyRoot.data + " ");
copyRoot = null;
}

}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}

}
}


To search a particular element from Binary search tree we are using below method.to search a element basically we want to find the node of element and its parent of the element. this search will also used in deleting a node from BST.


public static Node search(int data) {

avail = null;
if (root == null) {
System.out.println("Empty BST");
return null;
} else {
if (root.data == data) {
avail = root;
parent = null;
return avail;
}

Node copyRoot = root, findTree = null;
if (root.data > data) {
findTree = root.left;
} else {
findTree = root.right;
}

while (findTree != null) {
if (findTree.data == data) {
avail = findTree;
parent = copyRoot;
break;
}
copyRoot = findTree;
if (findTree.data > data) {
findTree = findTree.left;
} else {
findTree = findTree.right;
}
}

}

return avail;

}


To delete a node from BST , we have three way to delete a node.
1. If node have not left and right sub tree.(nought method)
2. If node have only left or right sub tree .(crooked method)
3. if node have both left and right sub tree.(hedge method)

public static void delete(int data) {
if (root == null) {
System.out.println("Empty BST");
return;
}
Node avail = search(data);
if (root == avail && root.left == null && root.right == null) {
root = null;
}

if (avail.left == null && avail.right == null) {
nought(avail);
}

if (avail.left != null && avail.right == null) {
crooked(avail);
}
if (avail.right != null && avail.left == null) {
crooked(avail);
}
if(avail.left!=null && avail.right!=null){
hedge(avail);
}
}


1.) To delete a node which haven't left and right node we called nought method.


public static void nought(Node avail) {
// if(parent==null) or if(root==avail)
if (root == avail) {
root = null;
return;
}
if (parent.left == avail) {
parent.left = null;
} else {
parent.right = null;
}
}



2.) If node have only left or right subtree then we will call crooked method.


public static void crooked(Node avail) {
Node copy = null;

if (avail.left != null) {
copy = avail.left;
} else {
copy = avail.right;
}

if (root == avail) {
root=copy;
return;
}
if(parent.left==avail){
parent.left=copy;
}else{
parent.right=copy;
}

}

3.) If node have both left and right tree then we will call hedge method.


public static void hedge(Node avail){
Node copy=avail.right;
Node q=null;
while(copy!=null){
q=copy;
copy=copy.left;
}
Node avail1=avail,parent1=parent;
Node avail2=search(q.data);
if(avail2.right!=null && avail2.left==null){
crooked(avail2);
}else{
nought(avail2);
}
if(parent1==null){
avail2.right=avail1.right;
avail2.left=avail1.left;
root=avail2;
}else{
if(parent1.right==avail1){
parent1.right=avail2;
}else{
parent1.left=avail2;
}
avail2.right=avail1.right;
avail2.left=avail1.left;
}
}








Wednesday, 15 June 2016

Pinch To Zoom In Android

we are going to create pinch to zoom IN/OUT in Android.

Step 1. For this we have created xml File as Follow.


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.gc.myapplication.MainActivity">
<TextView android:id="@+id/pinchZoom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pinch_title" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/pinchZoom" android:scaleType="matrix" android:src="@mipmap/amrit" android:maxHeight="100dp" android:id="@+id/pinchimage" android:maxWidth="50dp" android:contentDescription="@string/content_description" /></RelativeLayout>





Step 2. we have created Activity for Pinch Zoom IN/OUT.

package com.example.gc.myapplication;


import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

    

    // ImageView imageView;
    ImageView imageView;
    Matrix matrix= new Matrix();
    Float scale=1f;
    ScaleGestureDetector sgd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.pinchimage);
        sgd = new ScaleGestureDetector(this,new scaleListener());
    }

    private class scaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{

        @Override        public boolean onScale(ScaleGestureDetector detector) {
            scale =scale*detector.getScaleFactor();
            scale=Math.max(0.1f,Math.min(scale,5f));
            matrix.setScale(scale,scale);
            imageView.setImageMatrix(matrix);
            return true;
        }
    }

    public boolean onTouchEvent(MotionEvent event){
        sgd.onTouchEvent(event);
        return true;
    }


}




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