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



}

No comments:

Post a Comment