SIMPLE JAVA PROGRAMS
1.PROGRAM TO PRINT "HELLO WORLD"
import java.io.*;
import java.lang.*;
class Hello {
public static void main (String[ ] args){
System.out.println("Hello world");
}
}
2.PROGRAM TO PRINT A NUMBER
import java.io.*;
import java.lang.*;
class Number{
public static void main(String [ ] arg)throws IOException
{System.out.println("Enter the number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.println("i="+i);
}
}
3.REVERSE OF A NUMBER
import java.io.*;
import java.lang.*;
class Reverse{
public static void main(String [ ] arg)throws IOException{
System.out.println("Enter a number to get reversed ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.println("Number before reverse "+i);
int c=0,temp=0;
while(i>0)
{temp=i%10;
c=c*10+temp;
i=i/10;
}
System.out.println("Reverse is "+c);
}
}
4.BANK APPLICATION PROGRAM
import java.io.*;
import java.lang.*;
class Bank {
private int bal=0;
public Bank(int openingbal){
bal=openingbal; }
public void deposit(int a) {
bal=bal+a;}
public void withdraw(int b) {
bal=bal-b;}
public void display( ) {
System.out.println("Balance in Account is: "+bal); } }
class App {
public static void main(String[ ] arg)throws IOException {
Bank b=new Bank(0);
b.display( );
System.out.println("Enter the amount to be deposited ");
DataInputStream obj = new DataInputStream(System.in);
String s=obj.readLine( );
int am=Integer.parseInt(s);
b.deposit(am);
b.display( );
System.out.println("Enter withdraw amount: ");
String t=obj.readLine( );
int amo=Integer.parseInt(t);
b.withdraw(amo);
b.display( );} }
5.BINARY SEARCH
import java.io.*;
class Binary {
public static void main(String [] args)throws IOException
{
int mid=0;
int d[] = new int[100];
System.out.println("Enter the no: of elements in the array");
DataInputStream obj = new DataInputStream(System.in);
String num = obj.readLine();
int n = Integer.parseInt(num);
System.out.println("Enter the elements in to the array in sorted manner");
for(int i=0;i<n;i++)
{
String b = obj.readLine();
int k = Integer.parseInt(b);
d[i] = k;
}
System.out.println("Enter the element to be searched");
String c = obj.readLine();
int item= Integer.parseInt(c);
int beg=0,end=n-1;
mid = (beg+end)/2;
while(beg<=end && d[mid]!=item)
{
if(item<d[mid])
end = mid-1;
else
beg = mid+1;
mid = (beg+end)/2;
}
if(d[mid]==item)
{
int j = mid+1;
System.out.println("Element found at location: "+j);
}
else
{
System.out.println("Sorry no element found");
}
}
}
6.LINEAR SEARCH
import java.io.*;
import java.lang.*;
class Linear {
public static void main(String[ ] arg)throws IOException {
int [ ]d=new int[100];
int j=0;
System.out.println("Enter the number of elements in array");
DataInputStream obj = new DataInputStream(System.in);
String num=obj.readLine( );
int a=Integer.parseInt(num);
System.out.println("Enter the elements");
for(int i=0;i<a;i++) {
String b=obj.readLine();
int k=Integer.parseInt(b);
d[i]=k;
}
System.out.println("Enter an element to be searched ");
String e=obj.readLine();
int f=Integer.parseInt(e);
for(int i=0;i<a;i++)
{
if(d[i]==f)
{
j=i+1;
System.out.println("Element found at location: "+j);
}
}if (j==0)System.out.println("Element not found");
}
}
7.MENU DRIVEN BINARY & LINEAR SEARCH
import java.io.*;
class Linearbinary
{
public static void main(String s[])
{
int key=0,p=0,u=0,mid=0,l=0,i=0,k=0,j=0,ch=0,position=0,ele=0;
int flag=0;
int A[]=new int[10];
int B[]=new int[10];
int n=0;
String choice;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("\n\n\tEnter a limit : ");
n=Integer.parseInt(b.readLine());
System.out.println("\n\n\tEnter the array elements\n\n\t");
for(i=0;i<n;i++)
{
A[i]=Integer.parseInt(b.readLine());
}
do
{
System.out.println("\n\tMENU\n\n\t1. Linear search\n\n\t2. Binary search\n\n\t");
ch=Integer.parseInt(b.readLine());
switch(ch)
{
case 1:flag=0;
System.out.println("\n\n\tEnter the element to be searched for : ");
ele=Integer.parseInt(b.readLine());
for(i=0;i<n;i++)
{
if(ele==A[i])
{
flag=1;
position=i;
}
}
if(flag==1)
System.out.println("\n\n\tElement Found at position "+(position+1));
else
System.out.println("\n\n\tElement not found\n\n\t");
break;
case 2:
B[0]=A[0];
for(j=1;j<n;j++)
{
k=A[j];
flag=1;
i=j-1;
while(flag==1)
{
if(k<B[i])
{
i=i-1;
if(i==-1)
{
flag=0;
}
}
else
{
flag=0;
}
}
p=j;
while(p>i+1)
{
B[p]=B[p-1];
p=p-1;
}
B[i+1]=k;
}
System.out.println("\n\n\tInsertion Sort Over\n\n\tThe array is\n\n\t");
for(i=0;i<n;i++)
{
System.out.println("\n\t"+B[i]);
}
System.out.println("\n\n\tEnter an element to be searched for : ");
key=Integer.parseInt(b.readLine());
l=0; u=n-1;
flag=0;
for(;(flag!=1)&&(l<=u);)
{
mid=(l+u)/2;
if(key==B[mid])
{
flag=1;
position=mid;
}
if(key<B[mid])
u=mid-1;
else
l=mid+1;
}
if(flag==1)
System.out.println("\n\n\tElement fount at position "+(position+1));
else
System.out.println("\n\n\tElement not found");
break;
case 3: System.out.println("\n\n\tThe array is\n\n\t");
for(i=0;i<n;i++)
{
System.out.println("\n\t"+A[i]);
}
break;
}
System.out.println("\n\n\tDo you want to continue ? (Y/N)\n\n\t");
choice=b.readLine();
}while(choice.charAt(0)=='y'||choice.charAt(0)=='Y');
}
catch(Exception e)
{
}
}
}
8.BUBBLE SORT
import java.io.*;
import java.lang.*;
class Bubble {
public static void main(String[ ] arg)throws IOException {
int [ ]d=new int[100];
int j=0;
System.out.println("Enter the number of elements in array");
DataInputStream obj = new DataInputStream(System.in);
String num=obj.readLine( );
int a=Integer.parseInt(num);
System.out.println("Enter the elements");
for(int i=0;i<a;i++) {
String b=obj.readLine();
int k=Integer.parseInt(b);
d[i]=k;
}
System.out.println("array is ");
for(int i=0;i<a;i++)
System.out.println(d[i]);
int ptr=1;
for(int i=0;i<a+1;i++)
{
while (ptr<=(a-i))
{int temp;
if (d[ptr]>d[ptr+1])
{temp=d[ptr];
d[ptr]=d[ptr+1];
d[ptr+1]=temp;
}
}
ptr=ptr+1;
}
System.out.println("Sorted array is ");
for(int i=0;i<a;i++)
System.out.println(d[i]);
System.out.println("Enter an element to be add to the array ");
String e=obj.readLine();
int f=Integer.parseInt(e);
{
d[a-1]=f;
}
int pt=1;
for(int i=0;i<a;i++)
{
while (pt<=(a-i))
{int temp;
if (d[pt]>d[pt+1])
{temp=d[pt];
d[pt]=d[pt+1];
d[pt+1]=temp;
}
}
pt=pt+1;
}
System.out.println("Sorted array is ");
for(int i=0;i<a+1;i++)
System.out.println(d[i]);
}
}
9.INSERTION SORT
import java.io.*;
import java.lang.*;
class Insertion {
public static void main(String [ ]arg)throws IOException {
int [ ] b=new int [50];
DataInputStream in = new DataInputStream(System.in);
System.out.println("Enter no of elements :");
String a =in.readLine();
int n = Integer.parseInt(a);
System.out.println("Enter unsorted list :");
for(int i=0;i<n;i++) {
String c =in.readLine();
int d =Integer.parseInt(c);
b[i]=d;
}
for(int j=1;j<=n-1;j++) {
int temp = b[j];
int ptr =j-1;
while(ptr>=0 && temp<b[ptr] ) {
b[ptr+1]=b[ptr];
ptr=ptr-1;
}
b[ptr+1]=temp;
System.out.println("After pass"+j);
for(int i=0;i<n;i++) {
System.out.println(b[i]);
}
}
System.out.println("SORTED ARRAY");
for(int k=0;k<n;k++) {
System.out.println(b[k]);
}
}
}
10.SELECTION SORT
import java.io.*;
class SelSort {
public static void main(String args[])throws IOException {
int min=0;
int a[]=new int[10];
int temp=0;
int k=0;
int loc=0;
int i=0;
int j=0;
int m=1;
DataInputStream obj = new DataInputStream(System.in);
System.out.println("Enter the no: of elements:");
int l=Integer.parseInt(obj.readLine());
System.out.println("Enter the elements:");
for( i=0;i<l;i++)
a[i]=Integer.parseInt(obj.readLine());
for(i=0;i<l-1;i++){
min=a[i];
loc=i;
for(j=i+1;j<l;j++){
if(min>a[j]){
min=a[j];
loc=j;
}
}
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
System.out.println("Pass:"+m);
m++;
for(k=0;k<l;k++)
System.out.print(a[k]+" ");
System.out.println(" ");
}
System.out.println("Sorted array is :");
for(i=0;i<l;i++)
System.out.println(a[i]+" ");
}
}
import java.io.*;
class SelSort {
public static void main(String args[])throws IOException {
int min=0;
int a[]=new int[10];
int temp=0;
int k=0;
int loc=0;
int i=0;
int j=0;
int m=1;
DataInputStream obj = new DataInputStream(System.in);
System.out.println("Enter the no: of elements:");
int l=Integer.parseInt(obj.readLine());
System.out.println("Enter the elements:");
for( i=0;i<l;i++)
a[i]=Integer.parseInt(obj.readLine());
for(i=0;i<l-1;i++){
min=a[i];
loc=i;
for(j=i+1;j<l;j++){
if(min>a[j]){
min=a[j];
loc=j;
}
}
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
System.out.println("Pass:"+m);
m++;
for(k=0;k<l;k++)
System.out.print(a[k]+" ");
System.out.println(" ");
}
System.out.println("Sorted array is :");
for(i=0;i<l;i++)
System.out.println(a[i]+" ");
}
}
11.MERGE SORT
import java.io.*;
import java.lang.*;
class MergeSort{
public int a[]=new int[50];
public void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
public void merge(int low,int mid,int high)
{
int h,i,j,k;
int b[]=new int[50];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
public MergeSort()
{
int num,i;
System.out.println("MERGE SORT PROGRAM ");
System.out.println();
System.out.println();
System.out.println("Enter THE No. OF ELEMENTS ");
num=new Scanner(System.in).nextInt();
System.out.println();
System.out.println(" Enter the nos");
for(i=1;i<=num;i++)
{
a[i]=new Scanner(System.in).nextInt() ;
}
merge_sort(1,num);
System.out.println();
System.out.println("the sorted list ");
System.out.println();
System.out.println();
for(i=1;i<=num;i++)
System.out.print(a[i]+" ");
}
public static void main(String[] args) {
new MergeSort();
}
}
12.QUICKSORT
import java.io.*;
import java.lang.*;
class Quicksort
{
public static int j=0;
public static int part(int a[],int n,int left,int right) {
int m=(left+right)/2;
int t;
int piv;
t=a[left];
a[left]=a[m];
a[m]=t;
piv=a[left];
int le=left+1;
int ri=right;
while(le<=ri) {
while((le<=right)&&(a[le]<=piv)) {
le=le+1;
}
while((ri>left)&&(a[ri]>=piv))
{
ri=ri-1;
}
if(le>ri) {
break;
}
else {
t=a[le];
a[le]=a[ri];
a[ri]=t;
le=le+1;
ri=ri-1;
System.out.println("\nafter the pass number the array is");
for(int i=0;i<n;i++) {
System.out.print(" "+a[i]);
}
System.out.print("\n");
System.out.println("_________________________________________________________\n");
}
}
if(le>ri) {
t=a[ri];
a[ri]=a[left];
a[left]=t;
System.out.println("after the pass number the array is");
for(int i=0;i<n;i++) {
System.out.print(" "+a[i]); }
System.out.print("\n");
System.out.println("____________________________________________________\n");
}
return (ri);
}
public static void quicksort(int []a,int n,int left,int right) {
if(left<right) {
int ri= part(a,n,left,right);
quicksort(a,n,left,ri-1);
quicksort(a,n,ri+1,right);
}
}
public static void main( String [] arg)throws IOException {
int a[]=new int[30];
System.out.println("Enter the number of the elements");
String s1;
DataInputStream ob=new DataInputStream(System.in);
s1=ob.readLine();
int n=Integer.parseInt(s1);
System.out.println("Enter the elements");
for(int i=0;i<n;i++) {
s1=ob.readLine();
a[i]=Integer.parseInt(s1);
}
System.out.println("the array is");
for(int i=0;i<n;i++) {
System.out.print(" "+a[i]);
}
System.out.print("\n");
System.out.print("___________________________________________________________\n");
int left=0;
int right=(n-1);
quicksort(a,n,left,right);
System.out.println("\nQUICK SORT ");
System.out.println("_________________");
System.out.println("the sorted array is");
for(int i=0;i<n;i++) {
System.out.print(" "+a[i]);
}
System.out.print("\n");
System.out.print("****************************************************************");
}
}
13.QUEUE USING LINKED LIST
import java.io.*;
import java.lang.*;
class Node {
int data;
Node next;
}
class Q
{
DataInputStream ob=new DataInputStream(System.in);
String s;
//int st[];
int max;
int top;
Q(int n)
{
max=n;
//st=new int[n];
top=-1;
}
public static Node insertlast(int item,Node header)throws IOException
{
Node newnode=new Node();
Node ptr;
if(header==null)
{
header=newnode;
}
else
{
ptr=header;
while(ptr.next!=null)
{
ptr=ptr.next;
}
ptr.next=newnode;
}
newnode.data=item;
newnode.next=null;
return(header);
}
public static Node deletelast(Node header)throws IOException {
if(header!=null) {
header=header.next;
return(header);
}
else {
System.out.print("\nQUEUE IS EMPTY\n");
return(header);
}
}
public static void traverse(Node header)throws IOException {
System.out.println("THE QUEUE ELEMENTS ARE\n");
if(header!=null) {
while(header!=null)
{
System.out.print(" "+header.data);
header=header.next;
}
}
else {
System.out.print("NULL..........................!");
}
}
public void sea(Node header)throws IOException {
Node ptr=header;
System.out.print("ENTER THE ITEM TO BE SEARCHED\n");
s=ob.readLine();
int item=Integer.parseInt(s);
int f=0;
int i=0;
while(ptr!=null) {
i++;
if(ptr.data==item) {
f=1;
System.out.print(item+" "+"IS FOUND AT POSITION :"+(i));
}
ptr=ptr.next;
}
if(f==0) {
System.out.print("\nSORRY ITEM IS NOT FOUND\n");
}
}
}
class Qul
{
static int ch;
public static void main(String args[])throws IOException
{
Node header;
header=null;
Q obj=new Q(5);
do
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.SEARCH\n5.EXIT\n");
System.out.println("enter the choice");
ch=Integer.parseInt(b.readLine());
switch(ch)
{
case 1:System.out.println("Enter the element to be inserted:");
int c=Integer.parseInt(b.readLine());
header=obj.insertlast(c,header);
obj.traverse(header);break;
case 2:header=obj.deletelast(header);
obj.traverse(header);break;
case 3:obj.traverse(header);break;
case 4:obj.sea(header); break;
case 5:System.out.println("exit");break;
default:System.out.println("invalid choice\n");
}
}while(ch!=5);
}
}
14.SINGLY LINKED LIST
import java.io.*;
import java.lang.*;
class Node {
int data;
Node next;}
class Operations {
public Node insertfront(int item,Node header)throws IOException {
Node newnode=new Node();
newnode.next=null;
if(header!=null) {
newnode.next=header;}
header=newnode;
newnode.data=item;
return(header);}
public Node insertlast(int item,Node header)throws IOException {
Node newnode=new Node();
Node ptr;
if(header==null) {
header=newnode;}
else {
ptr=header;
while(ptr.next!=null) {
ptr=ptr.next;}
ptr.next=newnode;}
newnode.data=item;
newnode.next=null;
return(header);}
public Node insertbtw(int key,int item,Node header)throws IOException {
Node ptr;
ptr=header;
while(ptr.data!=key&&ptr.next!=null) {
ptr=ptr.next;}
if(ptr.data==key) {
Node newnode=new Node();
newnode.next=ptr.next;
ptr.next=newnode;
newnode.data=item;}
else
System.out.println("Key not found");
return(header);}
public Node deletefront(Node header)throws IOException {
if(header==null)
System.out.print("Its empty There is no element to delete");
else
header=header.next;
return(header);}
public Node deletelast(Node header)throws IOException {
Node ptr,ptr1;
ptr=header;
ptr1=null;
while(ptr.next!=null) {
ptr1=ptr;
ptr=ptr.next;}
ptr1.next=null;
return(header);}
public Node deletebtw(int key,Node header)throws IOException {
Node ptr,ptr1;
ptr=header;
ptr1=null;
while(ptr.data!=key&&ptr.next!=null) {
ptr1=ptr;
ptr=ptr.next;}
if(ptr.data==key)
ptr1.next=ptr.next;
else
System.out.println("Key not found");
return(header);}
public void traverse(Node header)throws IOException {
while(header!=null) {
System.out.print(" "+header.data);
header=header.next;}
System.out.print("\n"); }
public void search(int key,Node header)throws IOException {
Node ptr=header;
while(ptr.data!=key&&ptr.next!=null) {
ptr=ptr.next;}
if(ptr.data==key)
System.out.println("Search success key found");
else
System.out.println("Search failed key not found");}}
class SingleLinkList {
public static void main(String [] args)throws IOException {
Operations op=new Operations();
Node header;
header=null;
int ch,ins,del,item,key,n=0,q=0;
String s;
do {
System.out.println("Enter your choice-\n1 Insert\n2 Delete\n3 Search \n4 Traverse\n5 Exit");
DataInputStream ob=new DataInputStream(System.in);
s=ob.readLine();
ch=Integer.parseInt(s);
switch(ch) {
case 1:
System.out.println("Enter the item to be inserted");
s=ob.readLine();
item=Integer.parseInt(s);
System.out.println("Where do you wanna insert?");
System.out.println("Enter \n1 Insert at beginning\n2 Insert at end\n3 to Insert inbetween");
s=ob.readLine();
ins=Integer.parseInt(s);
switch(ins) {
case 1:
header=op.insertfront(item,header);
break;
case 2:
header=op.insertlast(item,header);
break;
case 3:
System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
header=op.insertbtw(key,item,header);
break;}break;
case 2:
System.out.println("From where do you wanna delete?");
System.out.println("Enter \n1 del from beginning \n2 del from end\n3 to del an item inbetween");
s=ob.readLine();
del=Integer.parseInt(s);
switch(del) {
case 1:
header=op.deletefront(header);
break;
case 2:
header=op.deletelast(header);break;
case 3:System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
header=op.deletebtw(key,header);
break;}break;
case 3:System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
op.search(key,header);break;
case 4:
op.traverse(header);break;
case 5:q=1;break;
default:System.out.print("Quiting");break; }
}
while(q!=1);} }
15.DOUBLY LINKED LIST
import java.io.*;
class Node
{
Object info;
Node next,pre;
Node(Object data)
{
info=data;
next=null;
pre=null;
}
}
class DLinkedList
{
Node start;
DLinkedList()
{
start=null;
}
void first(Node nnode)
{
nnode.next=start;
start.pre=nnode;
start=nnode;
}
void mid(Node nnode,int v)
{
Node p=start;
for(int i=1;i<v-1;i++)
{
p=p.next;
}
(p.next).pre=nnode;
nnode.next=p.next;
nnode.pre=p;
p.next=nnode;
System.out.println("value inserted at "+v+" position.");
}
void end(Node nnode)
{
Node p=start;
while(p.next!=null)
{
p=p.next;
}
p.next=nnode;
nnode.pre=p;
System.out.println("value inserted at end");
}
void display()
{
Node p=start;
if(p==null)
{
System.out.println("\n The LinkedList is empty.");
}
else
{
System.out.print("your values are : ");
while(p!=null)
{
System.out.print(p.info+" ");
p=p.next;
}
}
}
Node search(Object data)throws Exception
{
Node p;
int co=1,f=0;
p=start;
if(p==null)
{
System.out.println(" The LinkedList is empty.");
}
else
{
while((p!=null)&&(f==0))
{
if((p.info).equals(data)==true)
{
f=1;
}
else
{
p=p.next;
co++;
}
}
if((p.info).equals(data)==true)
{
System.out.println(p.info+" found at "+co+" place " );
return(p);
}
else
{
System.out.println(data+" does not found");
return(null);
}
}
return(p);
}
void del(Object data)throws Exception
{
Node p=null;
int f=0;
if(start==null)
{
System.out.println(" The linked list is empty.");
}
try
{
p=search(data);
}
catch(Exception e){}
if(p!=null)
{
if(p.pre!=null)
{
if(p.next==null)
{
p.pre.next=p.next;
p=null;
}
else
{
p.pre.next=p.next;
p.next.pre=p.pre;
p=null;
}
}
else
{
if(p.pre!=null && p.next==null)
start=null;
else
{
start=start.next;
start.pre=null;
}
}
}
}
public static void main(String args[ ])throws Exception
{
DLinkedList ob=new DLinkedList();
int c,a=1,x;
DataInputStream dis=new DataInputStream(System.in);
while(a==1)
{
System.out.println("\n Main Menu \n");
System.out.println("1-Insert\n2-Delete");
System.out.println("3-Traverse\n4-Search");
System.out.println("5-Exit");
System.out.print("\nEnter your choice:");
c=Integer.parseInt(dis.readLine());
switch(c)
{
case 1: System.out.println("\n....INSERTION.... \n");
System.out.print("Enter your value:");
x=Integer.parseInt(dis.readLine());
Node nnode=new Node(new Integer(x));
if(ob.start==null)
ob.start=nnode;
else
{
System.out.println("\n1:Insert value at first place");
System.out.println("2:Midle");
System.out.println("3:Insert value at end");
System.out.print("Enter your choice:");
int pos=Integer.parseInt(dis.readLine());
if(pos==1)
ob.first(nnode);
if(pos==2)
{
System.out.print("Enter position:");
int v=Integer.parseInt(dis.readLine());
ob.mid(nnode,v);
}
if(pos==3)
ob.end(nnode);
}
break;
case 2: try
{
System.out.print("Enter value to delete:");
x=Integer.parseInt(dis.readLine());
ob.del(new Integer(x));
System.out.print(" value is to deleted");
}
catch(Exception e)
{}
break;
case 3:
System.out.println();
ob.display();
break;
case 4: try
{
System.out.print("Enter value for search:");
x=Integer.parseInt(dis.readLine());
Node p=null;
p=ob.search(new Integer(x));
}
catch(Exception e)
{}
break;
case 5: System.out.println("\n....EXIT....");
System.exit(1);
break;
default: System.out.println("\n....Wrong Choice....");
}
}
}
}
import java.io.*;
class Node
{
Object info;
Node next,pre;
Node(Object data)
{
info=data;
next=null;
pre=null;
}
}
class DLinkedList
{
Node start;
DLinkedList()
{
start=null;
}
void first(Node nnode)
{
nnode.next=start;
start.pre=nnode;
start=nnode;
}
void mid(Node nnode,int v)
{
Node p=start;
for(int i=1;i<v-1;i++)
{
p=p.next;
}
(p.next).pre=nnode;
nnode.next=p.next;
nnode.pre=p;
p.next=nnode;
System.out.println("value inserted at "+v+" position.");
}
void end(Node nnode)
{
Node p=start;
while(p.next!=null)
{
p=p.next;
}
p.next=nnode;
nnode.pre=p;
System.out.println("value inserted at end");
}
void display()
{
Node p=start;
if(p==null)
{
System.out.println("\n The LinkedList is empty.");
}
else
{
System.out.print("your values are : ");
while(p!=null)
{
System.out.print(p.info+" ");
p=p.next;
}
}
}
Node search(Object data)throws Exception
{
Node p;
int co=1,f=0;
p=start;
if(p==null)
{
System.out.println(" The LinkedList is empty.");
}
else
{
while((p!=null)&&(f==0))
{
if((p.info).equals(data)==true)
{
f=1;
}
else
{
p=p.next;
co++;
}
}
if((p.info).equals(data)==true)
{
System.out.println(p.info+" found at "+co+" place " );
return(p);
}
else
{
System.out.println(data+" does not found");
return(null);
}
}
return(p);
}
void del(Object data)throws Exception
{
Node p=null;
int f=0;
if(start==null)
{
System.out.println(" The linked list is empty.");
}
try
{
p=search(data);
}
catch(Exception e){}
if(p!=null)
{
if(p.pre!=null)
{
if(p.next==null)
{
p.pre.next=p.next;
p=null;
}
else
{
p.pre.next=p.next;
p.next.pre=p.pre;
p=null;
}
}
else
{
if(p.pre!=null && p.next==null)
start=null;
else
{
start=start.next;
start.pre=null;
}
}
}
}
public static void main(String args[ ])throws Exception
{
DLinkedList ob=new DLinkedList();
int c,a=1,x;
DataInputStream dis=new DataInputStream(System.in);
while(a==1)
{
System.out.println("\n Main Menu \n");
System.out.println("1-Insert\n2-Delete");
System.out.println("3-Traverse\n4-Search");
System.out.println("5-Exit");
System.out.print("\nEnter your choice:");
c=Integer.parseInt(dis.readLine());
switch(c)
{
case 1: System.out.println("\n....INSERTION.... \n");
System.out.print("Enter your value:");
x=Integer.parseInt(dis.readLine());
Node nnode=new Node(new Integer(x));
if(ob.start==null)
ob.start=nnode;
else
{
System.out.println("\n1:Insert value at first place");
System.out.println("2:Midle");
System.out.println("3:Insert value at end");
System.out.print("Enter your choice:");
int pos=Integer.parseInt(dis.readLine());
if(pos==1)
ob.first(nnode);
if(pos==2)
{
System.out.print("Enter position:");
int v=Integer.parseInt(dis.readLine());
ob.mid(nnode,v);
}
if(pos==3)
ob.end(nnode);
}
break;
case 2: try
{
System.out.print("Enter value to delete:");
x=Integer.parseInt(dis.readLine());
ob.del(new Integer(x));
System.out.print(" value is to deleted");
}
catch(Exception e)
{}
break;
case 3:
System.out.println();
ob.display();
break;
case 4: try
{
System.out.print("Enter value for search:");
x=Integer.parseInt(dis.readLine());
Node p=null;
p=ob.search(new Integer(x));
}
catch(Exception e)
{}
break;
case 5: System.out.println("\n....EXIT....");
System.exit(1);
break;
default: System.out.println("\n....Wrong Choice....");
}
}
}
}
16.OPERATION ON STACK USING ARRAY
import java.io.*;
import
java.lang.*;
class stk{
int st[];
int max;
int top;
stk(int n){
max=n;
st=new int[n];
top=-1;}
void push(int a){
int b;
b=a;
if(top==max-1){
System.out.println("stack
is overflow\n");}
else{
top=top+1;
st[top]=b;}}
void pop(){
if(top==-1){
System.out.println("stack
is underflow\n");}
else{
int temp;
temp=st[top];
top=top-1;
System.out.println(""+temp+"is
deleted\n");}}
void display(){
System.out.println("\nElements
are:");
for(int
i=0;i<=top;i++){
System.out.println("+"+st[i]);}
System.out.println("\n");}}
class StackArr{
static int ch;
public static
void main(String args[])throws IOException{
stk obj=new
stk(5);
do{
try{
BufferedReader b
= new BufferedReader(new InputStreamReader(System.in));
System.out.println("1.push\n2.pop\n3.display\n4.exit\n");
System.out.println("enter
ur choice");
ch=Integer.parseInt(b.readLine());
switch(ch){
case
1:System.out.println("Enter the element to be inserted:");
int
c=Integer.parseInt(b.readLine());
obj.push(c);break;
case
2:obj.pop();break;
case
3:obj.display();break;
case
4:System.out.println("exit");break;
default:System.out.println("invalid
choice\n");}}
catch(Exception
e){
System.out.println(e);}}
while(ch!=4);}}
17.OPERATION ON STACK USING LINKED LIST
import java.io.*;
import
java.lang.*;
class Node {
int data;
Node next;}
class stk{
int st[];
int max;
int top;
stk(int n){
max=n;
st=new int[n];
top=-1;}
public static
Node insertlast(int item,Node header)throws IOException{
Node
newnode=new Node();
Node ptr;
if(header==null){
header=newnode;}
else{
ptr=header;
while(ptr.next!=null){
ptr=ptr.next;}
ptr.next=newnode;}
newnode.data=item;
newnode.next=null;
return(header);}
public static
Node deletelast(Node header)throws IOException {
Node ptr,ptr1;
ptr=header;
ptr1=null;
while(ptr.next!=null){
ptr1=ptr;
ptr=ptr.next;}
ptr1.next=null;
return(header);}
public static
void traverse(Node header)throws IOException {
while(header!=null){
System.out.print("
"+header.data);
header=header.next;}}}
class StackLink{
static int ch;
public static
void main(String args[])throws IOException{
Node header;
header=null;
stk obj=new
stk(5);
do
{try{
BufferedReader b
= new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n1.push\n2.pop\n3.display\n4.exit\n");
System.out.println("Enter
ur choice");
ch=Integer.parseInt(b.readLine());
switch(ch){
case
1:System.out.println("Enter the element to be inserted:");
int
c=Integer.parseInt(b.readLine());
header=stk.insertlast(c,header);break;
case
2:header=stk.deletelast(header);break;
case
3:stk.traverse(header);break;
case
4:System.out.println("exit");break;
default:System.out.println("invalid
choice\n");}}
catch(Exception
e){
System.out.println(e);}}
while(ch!=4);}}
18.MAINTAIN A SORTED ARRAY
import java.io.*;
class Maintainsort {
static int a[] = new int[10];
static int c=0;
public static void main(String args[]) throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int ch;
do {
System.out.println( "\nOption ?");
System.out.println( " 1.Insetion \n 2.Deletion \n 3.Traversal \n 4.Quit" ) ;
ch = Integer.parseInt(in.readLine());
switch(ch) {
case 1: {
insert();
traverse();
break;
}
case 2 : {
delete();
traverse();
break;
}
case 3 : {
traverse();
break;
}
case 4:{break;
}
default : System.out.println( " what? quiting...");
}
}
while(ch<=3);
}
public static void insert() throws IOException{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.println( "Enter a number") ;
int n = Integer.parseInt(in.readLine());
if(c==0) {
a[c] = n;
}
else {
for ( int i=c-1; i>=0; i--) {
if( n>a[i]) {
a[i+1] = n;
}
if(n<a[i]) {
a[i+1] = a[i];
a[i] = n;
}
}
}
c++;
}
public static void delete() throws IOException{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int f;
f=0;
System.out.println( "Enter number to be deleted");
int del = Integer.parseInt(in.readLine());
for( int i = 0; i<c; i++) {
if( a[i]==del ) {
f=1;
for( int j = i; j<c; j++) {
a[j] = a[j+1];
}
break;
}
}
if(f==1) {
System.out.println( "Deleted");
c--;
}
else {
System.out.println( "Not found ");
}
}
public static void traverse() {
System.out.println( " Numbers are-> ");
for( int i=0; i<c; i++) {
System.out.print ( a[i] + " ");
}
}
}
19.MAINTAIN A SORTED ARRAY USING LINKED LIST
import java.io.*;
import java.lang.*;
class Node
{
int data;
Node next;
}
class Operations
{
public Node insertfront(int item,Node header)throws IOException
{
Node newnode=new Node();
newnode.next=null;
if(header!=null)
{
newnode.next=header;
}
header=newnode;
newnode.data=item;
return(header);
}
public Node insertlast(int item,Node header)throws IOException
{
Node newnode=new Node();
Node ptr;
if(header==null)
{
header=newnode;
}
else
{
ptr=header;
while(ptr.next!=null)
{
ptr=ptr.next;
}
ptr.next=newnode;
}
newnode.data=item;
newnode.next=null;
return(header);
}
public Node insertbtw(int key,int item,Node header)throws IOException
{
Node ptr;
ptr=header;
while(ptr.data!=key&&ptr.next!=null)
{
ptr=ptr.next;
}
if(ptr.data==key)
{
Node newnode=new Node();
newnode.next=ptr.next;
ptr.next=newnode;
newnode.data=item;
}
else
{System.out.println("Key not found");}
return(header);
}
public Node deletefront(Node header)throws IOException
{
if(header==null)
System.out.print("Its empty There is no element to delete");
else
header=header.next;
return(header);
}
public Node deletelast(Node header)throws IOException
{
Node ptr,ptr1;
ptr=header;
ptr1=null;
while(ptr.next!=null)
{ ptr1=ptr;
ptr=ptr.next;
}
ptr1.next=null;
return(header);
}
public Node deletebtw(int key,Node header)throws IOException
{
Node ptr,ptr1;
ptr=header;
ptr1=null;
while(ptr.data!=key&&ptr.next!=null)
{ptr1=ptr;
ptr=ptr.next;
}
if(ptr.data==key)
ptr1.next=ptr.next;
else
System.out.println("Key not found");
return(header);
}
public void traverse(Node header)throws IOException
{
while(header!=null)
{
System.out.print(" "+header.data);
header=header.next;
}
}
public void search(int key,Node header)throws IOException
{
Node ptr=header;
while(ptr.data!=key&&ptr.next!=null)
{
ptr=ptr.next;
}
if(ptr.data==key)
System.out.println("Search success key found");
else
System.out.println("Search failed key not found");
}
}
class Maintainlink
{
public static void main(String [] args)throws IOException
{
Operations op=new Operations();
Node header;
header=null;
int ch,ins,del,item,key,n=0;
String s;
do
{
System.out.println("Enter your choice\n1 - Insert\n2 - Delete\n3 - Search \n4 - Traverse");
DataInputStream ob=new DataInputStream(System.in);
s=ob.readLine();
ch=Integer.parseInt(s);
switch(ch)
{
case 1:System.out.println("Enter the item to be inserted");
s=ob.readLine();
item=Integer.parseInt(s);
System.out.println("Where do you wanna insert?");
System.out.println("1 Insert at beginning\n2 Insert at end\n3 to Insert inbetween");
s=ob.readLine();
ins=Integer.parseInt(s);
switch(ins)
{
case 1:header=op.insertfront(item,header);break;
case 2:header=op.insertlast(item,header);break;
case 3:System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
header=op.insertbtw(key,item,header);
break;
}break;
case 2:System.out.println("From where do you wanna delete?");
System.out.println("\n1 from beginning \n2 del from end \n3 del an item inbetween");
s=ob.readLine();
del=Integer.parseInt(s);
switch(del)
{
case 1:header=op.deletefront(header);break;
case 2:header=op.deletelast(header);break;
case 3:System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
header=op.deletebtw(key,header);
break;
}break;
case 3:System.out.println("Enter the key");
s=ob.readLine();
key=Integer.parseInt(s);
op.search(key,header);break;
case 4:op.traverse(header);break;
}
System.out.println();
System.out.println("Enter 1 to continue");
s=ob.readLine();
n=Integer.parseInt(s);
}
while(n==1);
}
}
No comments:
Post a Comment