Tuesday 15 March 2022

DSA Lecture 1



MAIN PROGRAM:


import java.util.*;
class Test{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
ARRAY a=new ARRAY();
do{
System.out.println("1-->Insert Data");
System.out.println("2-->Delete Data");
System.out.println("3-->Update Data");
System.out.println("4-->Delete At Any Index");
System.out.println("5-->Display ");
System.out.print("Chose 1 Option : ");
switch(input.nextInt()){
case 1:{
System.out.print("Enter number : ");
a.Insertdata(input.nextInt());
break;
}
case 2:{
a.Delete();
break;
}
case 3:{
a.Update(input.nextInt(), input.nextInt());
break;
}
case 4:{
a.DeleteAtLocation(input.nextInt());
break;
}
case 5:{
a.Display();
break;
}
}
}while(true);
}
}
 
 

Code of Array:



class ARRAY{
private int []Array;
private int Size;

//Constructor same name of class.
public ARRAY(){
Array=new int[5];
Size=0;
}

// used to Display the data in Array \t is used to to give tab between the number
public void Display(){
// means if size =1 then it display data
if(Size>0){
for(int i=0 ; i<Size ; i++){
System.out.print(Array[i]+"\t");
}
System.out.println("");
}
// if size=0 means it is empty
else{
System.out.println("Array is Empty :");
}
}

public void Insertdata(int data){
// if size=6 then means Array is full because Array size 5 .
if(Size>5){
System.out.println("Array is Full");
}
// if size is less then 5 it,s means space is present for insetion
else{
Array[Size++]=data;
}
}

// this is getter but i,m printing data here this is display the size of our Array means tell us about occupied space.
public void GetSize(){
// if size=0 means it,s Empty
if(Size==0){
System.out.println("The Array is Empty");
}
// if size is greter then 0 means some data is present in Array
else{
System.out.println("Occupied Space is : "+Size);
}
}

//this is used to delete the data from Array
//Note We can,t delete the data it,s not gonna be done
public void Delete(){
// if Size is greater then 0 then means data is present in Array so delete.
if(Size>0){
Size--;
}
//means Array is Empty size==0 so it is Empty.
else{
System.out.println("Array is Empty");
}
}

//it is used to delete the data at any index
public void DeleteAtLocation(int Location){
// it will check the given lovation is present
if(Location>Size){
System.out.println("No Address found");
}
//first i,m shfiting then element from location to 5. then delete last index
else{
for(int i=0 ; i<Size-1; i++){
Array[i]=Array[i+1];
}
Size--;
}
}

//this is used to Replace the data user enter data and location where replace the data.
public void Update(int data, int Location){
//checking index is present in a Array.
if(Location>Size){
System.out.println("No Address Found");
}
// if Address is found then replace data
else{
Array[Location]=data;
}
}
}

1 comment:

How to install and setup java in Windows

 Step 1:          first, you want to ensure that the JDK is installed in your system                        Then press Windows +  R  key  as...