Download Core Java Record

Document related concepts
no text concepts found
Transcript
Data Structures Through Java
Aim : To Write a Java program that prints all real solutions to the Quadratic
equation ax2+bx+c=0. Read in a,b,c and use the quadratic formula. If the
discriminant b2-4ac is negative,display a message stating that there are no
real solutions.
Program :
import java.io.*;
class quad
{
public static void main(String args[]) throws IOException
{
int a,b,c;
double d,r1,r2;
DataInputStream n=new DataInputStream(System.in);
System.out.println("enter a,b and c values");
a=Integer.parseInt(n.readLine());
b=Integer.parseInt(n.readLine());
c=Integer.parseInt(n.readLine());
System.out.println("the given equation is"+a+"x2+"+b+"x+"+c);
d=(b*b)-(4*a*c);
if(d<0)
System.out.println("the root are not real & imagenary");
else if(d==0)
System.out.println("the root are real& equal");
else if(d>0)
System.out.println("root are real"+d);
r1=(-b+Math.sqrt(d))/(2*a);
r2=(-b-Math.sqrt(d))/(2*a);
System.out.println("root are"+r1);
System.out.println(r2);
}
}
Output:
enter a,b and c values
1
2
1
the given equation is1x2+2x+1
the root are real& equal
roots are -1.0
-1.0
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 1
Data Structures Through Java
Aim : The Fibonacci sequence is defined by the following rule.The first two
values in the sequence are 0 and 1.Every subsequent value is the sm of the
values preceding it.Write a Java program that uses recursive function to print
the n th value in the Fibonacci sequence.
Program :
import java.io.*;
class Fibn1
{
public static void main(String args[]) throws IOException
{
int i,a=0,b=1;
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the limit");
i=Integer.parseInt(d.readLine());
System.out.print(a+" "+b);
for(int j=0;j<=i;j++)
{
int c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}
Output:
enter the limit
8
0 1 1 2 3 5 8 13 21 34 55
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 2
Data Structures Through Java
Aim : Write a Java program that prompts the user for an integer and then
prints out all the prime numbers up to that Integer.
Program:
import java.io.*;
class Prime
{
public static void main(String args[]) throws IOException
{
int a,b;
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the starting limit");
a=Integer.parseInt(d.readLine());
System.out.println("enter the ending limit");
b=Integer.parseInt(d.readLine());
for(int i=a;i<=b;i++)
{
int count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
System.out.print(" "+i);
}
}
}
Output:
enter the starting limit
2
enter the ending limit
20
2 3 5 7 11 13 17 19
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 3
Data Structures Through Java
Aim: Write a Java program that checks whether a given String is a
Palindrome or not.Ex: MADAM is a palindrome.
Program :
import java.io.*;
class Pali
{
DataInputStream d=new DataInputStream(System.in);
String s,t;
StringBuffer sb;
Pali()
{
try
{
System.out.println("enter any string");
s=d.readLine();
sb=new StringBuffer(s).reverse();
t=new String(sb);
if(s.equals(t))
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
catch (IOException io)
{
System.out.println("io error");
}
}
public static void main(String[] args)
{
Pali p=new Pali();
}
}
Output:
enter any string
pavan
not palindrome
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 4
Data Structures Through Java
Aim : Write a java program for sorting a given list of names in ascending
order.
Program :
import java.io.*;
class Sort_names
{
DataInputStream dis=new DataInputStream(System.in);
String s[],t;
int i,j,n;
Sort_names()
{
try
{
System.out.println("How many Names u want to sort");
n=Integer.parseInt(dis.readLine());
s=new String[n];
System.out.println("Enter names");
for(i=0;i<n;i++)
s[i]=dis.readLine();
}
catch(IOException x)
{
System.out.println("I/O Error");
}
}
void print()
{
System.out.println("The sorted Names");
for(i=0;i<n;i++)
System.out.println(s[i]);
}
void sort()
{
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(s[i].compareTo(s[j])>0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
public static void main(String args[])
{
Sort_names s1=new Sort_names();
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 5
Data Structures Through Java
s1.sort();
s1.print();
}
}
Output:
How many Names u want to sort
4
How many Names u want to sort
4
Enter names
raju
shekhar
pavan
meena
The sorted Names
meena
pavan
raju
shekhar
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 6
Data Structures Through Java
Aim : Write a java program to multiply two given matrices
Program :
class Mat
{
public static void main(String[] args)
{
int a[][]=new int[][]{{1,1},{1,1}};
int b[][]=new int[][]{{1,1},{1,1}};
int c[][]=new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=0;
for(int k=0;k<2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
System.out.println(“your resultant matrix is : “ );
for(int i=0;i<2;i++)
{
System.out.print("\n");
for(int j=0;j<2;j++)
{
System.out.print(" "+c[i][j]);
}
}
}
}
Output:
your resultant matrix is :
2 2
2 2
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 7
Data Structures Through Java
Aim: To write a java program that reads a line of integers, and then
displays each integer, and sum of all integers using String Tokenizer class.
Program :
Import java.util.*;
Class Sample
{
Public static void main(String args[])
{
Int sum=0, a;
String s=”1 2 3 4 5”;
StringTokenizer t=new StringTokenizer(s);
While(t.hasMoreTokens())
{
a=Integer.parseInt(t.nextToken());
sum=sum+a;
System.out.println(a);
}
System.out.println(“the sum is : “+sum);
}
}
Output:
1
2
3
4
5
The sum is : 15
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 8
Data Structures Through Java
Aim: To Write a Java program that reads on a file from the user then
displays information about whether the file exists , whether the file is
readable whether the file is writable,the type of file and the length of the file
in bytes.
Program :
import java.io.*;
class io1
{
public static void main(String args[])
{
File f1=new File("Fibn1.java");
if(f1.exists())
{
System.out.println(f1.getName());
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.isFile());
System.out.println(f1.isDirectory());
System.out.println(f1.canRead());
System.out.println(f1.canWrite());
f1.setReadOnly();
System.out.println(f1.canWrite());
System.out.println(f1.length());
}
else
System.out.println("file not found");
}
}
Output:
Fibn1.java
Fibn1.java
null
true
false
true
false
false
499
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 9
Data Structures Through Java
Aim: Write a Java program that reads a file and displays the file on the
screen ,with a line number before each line.
Program :
import java.io.*;
class io6
{
public static void main(String[] args)
{
try{
int c;
FileInputStream f1=new FileInputStream("io1.java");
while((c=f1.read())!= -1)
System.out.print((char)c);
}
catch(Exception io)
{
System.out.println("file found");
}
}
}
Output:
//sample program on files
import java.io.*;
class io1
{
public static void main(String args[])
{
File f1=new File("Fibn1.java");
if(f1.exists())
{
System.out.println(f1.getName());
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.isFile());
System.out.println(f1.isDirectory());
System.out.println(f1.canRead());
System.out.println(f1.canWrite());
f1.setReadOnly();
System.out.println(f1.canWrite());
System.out.println(f1.length());
}
else
System.out.println("file not found");
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 10
Data Structures Through Java
AIM: Write a Java program that displays the number of characters,lines and
words in a text file.
Program:
import java.io.*;
import java.lang.*;
class Str
{
public static void main(String[] args) throws IOException
{
int i=0,k=0,j;
int m=80,n=160,count=0,cout=1;
DataInputStream d=new DataInputStream(System.in);
String st=new String();
System.out.println("enter your string");
st=d.readLine();
for(j=0;j<st.length();j++)
{
if(st.charAt(j)==' ')
{
k++;
}
}
System.out.println("no. of spaces are "+k);
System.out.println("no. of words are "+(k+1));
System.out.println("no. of characters are "+(j-k));
System.out.println("no. of letters are "+j);
while(j<=80)
{
count++;
System.out.println("no. of lines are "+count);
break;
}
while(j>m||j<=n)
{
cout++;
if(j>m&&j<=n)
{
System.out.println("no. of lines are "+cout);
break;
}
else
{
m=m+80;
n=n+80;
}
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 11
Data Structures Through Java
}
Output:
enter your string
enter your string
we are studying mca in BVRIT college
Narsapur
no. of spaces are 55
no. of words are 56
no. of characters are 38
no. of letters are 93
no. of lines are 2
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 12
Data Structures Through Java
Aim: To write a program for creating multiple threads
a) Using Thread class
b) Using Runnable Interface
Program: a) Using Thread class
public class Sample
{
public static void main(String args[])
throws InterruptedException
{
Thread t1=Thread.currentThread();
System.out.println(t1);
System.out.println(t1.getName());
t1.setName("DemoThread");
t1.setPriority(6);
System.out.println(t1);
for(int i=0;i<5;i++)
{
System.out.println("Welcome to Multithreading");
Thread.sleep(1000);
}
}
}
Output :
Thread[main,5,main]
main
Thread[DemoThread,6,main]
Welcome to Multithreading
Welcome to Multithreading
Welcome to Multithreading
Welcome to Multithreading
Welcome to Multithreading
b) Using Runnable Interface
Program :
public class Sample
{
public static void main(String args[])
{
System.out.println("Main Thread is Started");
Thread t=Thread.currentThread();
t.setName("Hello");
Outer o=new Outer();
for(int i=1;i<=10;i++)
{
System.out.println(t.getName() +" " +i);
}
System.out.println("MainThread is over");
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 13
Data Structures Through Java
}
}
class Outer extends Thread
{
public Outer()
{
super("java");
start();
}
public void run()
{
System.out.println("Outer Thread is Started");
for(int j=1;j<=10;j++)
{
System.out.println(getName() +" "+j);
}
System.out.println("Outer Thread is over");
}
}
Output:
Main Thread is Started
Outer Thread is Started
Hello 1
java 1
Hello 2
java 2
Hello 3
java 3
Hello 4
java 4
Hello 5
java 5
Hello 6
java 6
Hello 7
java 7
Hello 8
java 8
Hello 9
java 9
Hello 10
java 10
MainThread is over
Outer Thread is over
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 14
Data Structures Through Java
Aim : To write a java program that illustrate how to runtime polymorphism
is achieved
Program :
class base
{
void show()
{
System.out.println("show in base");
}
}
class Derived1 extends base
{
void show()
{
System.out.println("show in derived1");
}
}
class Derived2 extends base
{
void show()
{
System.out.println("show in derived2");
}
}
class Sample
{
public static void main(String args[])
{
base b=new base();
b.show();
base c=new Derived1();
c.show();
base d=new Derived2();
d.show();
}
}
Output:
show in base
show in derived1
show in derived2
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 15
Data Structures Through Java
Aim : To write a java program that illustrate the following.
a) Creation of simple package.
b) Accessing a package.
c) Implementing Interface.
Program :
package p1;
public class c1
{
Int no;
String name;
Public c1(int a, String x)
{
no=a;
name=x;
}
public void show()
{
System.out.println(“this is java”);
}
}
import p1,c1;
class testp1
{
Public static void main(String args[])
{
c1 c=new c1(100,’abc’);
C.show();
}
}
Output:
100, abc
This is java
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 16
Data Structures Through Java
Aim : To write a java program that illustrate the following.
a) Handling pre-defined exceptions.
b) Handling user defined exceptions.
Program : a) Handling pre-defined exceptions.
class Except
{
public static void main(String args[])
throws InterruptedException
{
int a=10,b=5;
for(int i=1;i<=b;i++)
{
System.out.println("\n"+a+" * " +i+"=="+(a*i));
Thread.sleep(1000);
}
}
}
Output :
10 * 1==10
10 * 2==20
10 * 3==30
10 * 4==40
10 * 5==50
b) Handling Userdefined Exceptions
Program :
class Excep
{
public static void main(String args[])
{
System.out.println("Inside main");
int a,b,c;
a=10;
b=5;
try
{
System.out.println("Inside try");
c=a/b;
System.out.println("result of a/b is =="+c);
System.out.println(" "+args[2]);
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 17
Data Structures Through Java
System.out.println(Integer.parseInt(args[2])+Integer.parseInt(args[2]));
System.out.println("End of try");
}
catch(ArithmeticException ae)
{
System.out.println("Divide by zero Exception");
}
catch(ArrayIndexOutOfBoundsException aoe)
{
System.out.println("Wrong Array Index Exception");
}
catch(NumberFormatException ne)
{
System.out.println("Wrong conversion Exception");
}
System.out.println("End of main");
}
}
Output:
Inside main
Inside try
result of a/b is ==2
Wrong Array Index Exception
End of main
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 18
Data Structures Through Java
Aim: To write java programs for implementing the following searching
methods.
a) Linear Search
b) Binary Search
Program: a) Linear Search
import java.io.*;
class Linear
{
public static void main(String args[])throws IOException
{
int a[]=new int[9];
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter your array values");
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(d.readLine());
System.out.println("enter the element to find");
int b=Integer.parseInt(d.readLine());
boolean flag=false;
for(int j=0;j<9;j++)
{
if(a[j]==b)
{
flag=true;
break;
}
}
if(flag)
System.out.println("value found");
else
System.out.println("value not found");
}
}
Output :
enter your array values
2
3
5
9
4
enter the element to find
9
value found
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 19
Data Structures Through Java
b) Binary Search
Program :
import java.io.*;
class Bsearch
{
public static void main(String args[]) throws IOException
{
int a[]=new int[5];
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter your array values");
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(d.readLine());
System.out.println("enter the element to find");
int b=Integer.parseInt(d.readLine());
int low=0,mid,high=4;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==b)
{
System.out.println("value found");
break;
}
if(a[mid]<b)
low=mid+1;
else
high=mid-1;
}
if(low>high)
System.out.println("value not found");
}
}
Output :
enter your array values
2
5
8
4
2
enter the element to find
8
value found
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 20
Data Structures Through Java
Aim: To write java programs to implement the following using an array
a) Stack
b) Queue
Program :
a) Program for stack
import java.io.*;
class Stak
{
int top;
int a[]=new int[5];
int max=5;
Stak()
{
top=-1;
}
void push() throws IOException
{
System.out.println("enter stack element");
DataInputStream d=new DataInputStream(System.in);
int n=Integer.parseInt(d.readLine());
if(top==(max-1))
System.out.println("Stack is full");
else
a[++top]=n;
}
int pop()
{
if(top==-1)
return -1;
//System.out.println("stack is empty");
else
return a[top--];
}
void display()
{
System.out.println("your stack elements are");
for(int i=top;i>=0;i--)
System.out.print(" "+a[i]);
}
public static void main(String args[]) throws IOException
{
Stak s=new Stak();
s.push();
s.push();
s.push();
System.out.println("poped element is "+s.pop());
s.display();
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 21
Data Structures Through Java
}
Output :
enter stack element
2
enter stack element
5
enter stack element
4
poped element is 4
your stack elements are
5 2
b) Program for Queue :
import java.io.*;
class queue
{
int front,rear;
int a[]=new int[5];
int max=5;
queue()
{
front=rear=-1;
}
void insert() throws IOException
{
System.out.println("Enter the queue element");
DataInputStream d=new DataInputStream(System.in);
int n=Integer.parseInt(d.readLine());
if(rear==max-1)
{
System.out.println("Queue is full");
}
else
{
a[++rear]=n;
if(front==-1)
front=0;
}
}
int extract()
{
if(front==-1)
return -1;
//System.out.println("queue is empty");
else
{
if(front==rear)
{
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 22
Data Structures Through Java
front=rear=-1;
}
return a[front++];
}
}
void display()
{
System.out.println("queue elements are");
for(int i=front;i<=rear;i++)
System.out.print(" "+a[i]);
}
public static void main(String[] args) throws IOException
{
queue q=new queue();
for(int i=0;i<5;i++)
q.insert();
q.display();
System.out.println("extracted element is"+q.extract());
}
}
Output :
Enter the queue element
5
Enter the queue element
6
Enter the queue element
4
Enter the queue element
7
Enter the queue element
8
queue elements are
5 6 4 7 8extracted element is5
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 23
Data Structures Through Java
Aim : To write a java program that reads an infix expression, converts the
expression to postfix form and then evaluates the postfix expression .
Program :
import java.io.*;
import java.lang.Math;
import java.lang.String;
class inposteval
{
int top=-1;
double temp;
char s[]=new char[50];
double c[]=new double[50];
void push(char s1)
{
s[++top]=s1;
}
char pop()
{
return(s[top--]);
}
char tops()
{
return(s[top]);
}
int t()
{
return(top);
}
void push1(double f)
{
c[++top]=f;
}
double pop1()
{
return(c[top--]);
}
public static void main(String s[])throws IOException
{
int i=0;
double op1,op2,c=0;
inposteval o=new inposteval();
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the infix expression");
String str=String.valueOf(in.readLine());
str=str+")";
int a=str.length(); // 4
o.push('(');
String p="";
while(o.t()>=0)
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 24
Data Structures Through Java
{
switch(str.charAt(i))
{
case'(':o.push('(');
break;
case'+':
while(o.tops()=='+'||o.tops()=='*'||o.tops()=='/'||o.tops()=='^')
p+=o.pop();
o.push(str.charAt(i));
break;
case'-':
while(o.tops()==''||o.tops()=='*'||o.tops()=='/'||o.tops()=='^')
p+=o.pop();
o.push(str.charAt(i));
break;
case'/':
while(o.tops()=='/'||o.tops()=='^')
p+=o.pop();
o.push(str.charAt(i));
break;
case'*':
while(o.tops()=='*'||o.tops()=='^')
p+=o.pop();
o.push(str.charAt(i));
break;
case'^':
while(o.tops()=='^')
p+=o.pop();
o.push(str.charAt(i));
break;
case')':
while(o.tops()!='(')
p+=o.pop();
char ch=o.pop();
break;
default:p+=str.charAt(i);
}
i++;
} // end of while
System.out.println("The given expression is:"+str);
System.out.println("The postfix expression is:"+p);
int l=p.length();
System.out.println("p's length is:"+l);
int x=0;
while(x<l)
{
if(p.charAt(x)>='A' && p.charAt(x)<='z')
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 25
Data Structures Through Java
{
System.out.println("Enter temp value:");
double temp=Double.parseDouble(in.readLine());
o.push1(temp);
}
else
{
op1=o.pop1();
op2=o.pop1();
switch(p.charAt(x))
{
case'+': c=op1+op2;
o.push1(c);
break;
case'-': c=op1-op2;
o.push1(c);
break;
case'*': c=op1*op2;
o.push1(c);
break;
case'/': c=op2/op1;
o.push1(c);
break;
case'^': c=Math.pow((double)op1,(double)op2);
o.push1(c);
break;
}
}
x++;
}
System.out.println("\n THE EXPRESSION VALUE IS:"+o.pop1());
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 26
Data Structures Through Java
Output :
Enter the infix expression
a+b-c/(d+e/f)
The given expression is:a+b-c/(d+e/f))
The postfix expression is:abcdef/+/-+
p's length is:11
Enter temp value:
1
Enter temp value:
2
Enter temp value:
2
Enter temp value:
5
Enter temp value:
6
Enter temp value:
2
THE EXPRESSION VALUE IS:-0.75
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 27
Data Structures Through Java
Aim : To write a program on linked list
Program :
import java.io.*;
import java.lang.*;
class node
{
int info;
node next;
}
public class linklist
{
public static void main(String args[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
linklist n=new linklist();
node first=null;
first=n.create(first);
n.display(first);
}
node create(node start) throws IOException
{
int data;
DataInputStream nas=new DataInputStream(System.in);
node temp,last=null;
do
{
temp=new node();
System.out.println("enter the element");
DataInputStream in=new
DataInputStream(System.in);
data=Integer.parseInt(nas.readLine());
temp.info=data;
temp.next=null;
if(start==null)
{
start=last=temp;
}
else
{
last.next=temp;
last=temp;
}
}
while (data!=99);
return(start);
}
void display(node dislist)
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 28
Data Structures Through Java
{
while(dislist.next!=null)
{
System.out.print(" "+dislist.info+"-->");
dislist=dislist.next;
}
System.out.println("null");
}
}
Output :
enter the element
2
enter the element
5
enter the element
6
enter the element
4
enter the element
8
enter the element
9
enter the element
10
enter the element
23
enter the element
21
enter the element
99
2--> 5--> 6--> 4--> 8--> 9--> 10--> 23--> 21-->null
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 29
Data Structures Through Java
Aim : To write a program to create, insert and deleting the elements in
linked list
Program :
import java.io.*;
class node
{
int info;
node next;
}
//node class ending
public class linkedlist
{
static int count,data;
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
linkedlist n=new linkedlist();
node list=null;
int ch;
while(true)
{
System.out.println
("1.create\n2.insert\n3.delete\n4.display\n5.exit");
System.out.println("enter your choice");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
list=n.create(list);
break;
case 2:
list=n.insert(list);
break;
case 3:
list=n.delete(list);
break;
case 4:
n.display(list);
break;
case 5:
System.exit(1);
}
//switch ending
}
//while ending
}
//main ending
static node create(node start)throws IOException
{
DataInputStream in = new DataInputStream(System.in);
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 30
Data Structures Through Java
node temp,last=null;
do
{
temp=new node();
System.out.println("enter the item");
data=Integer.parseInt(in.readLine());
temp.info=data;
temp.next=null;
if(start==null)
start=last=temp;
else
{
last.next=last=temp;
count++;
}
}while(data!=100);
return(start);
}
//create methode ending
static void display(node dislist)
{
while(dislist!=null)
{
System.out.println(" "+dislist.info);
dislist=dislist.next;
}
}//diplay method ending
static node insert(node first) throws IOException
{
DataInputStream in
= new DataInputStream(System.in);
node l;
l=first;
int ch2;
System.out.println("enter the item to be inserted");
int item=Integer.parseInt(in.readLine());
node m=new node();
m.info=item;
if(first==null)
{
m.next=null;
first=m;
}
else
{
System.out.println("1.start \n 2.middle \n 3.end");
System.out.println("enter your choice");
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 31
Data Structures Through Java
ch2=Integer.parseInt(in.readLine());
if(ch2==1) // to insert at begining
{
m.next=first;
first=m;
}
if(ch2==2) // to insert at specified position
{
System.out.println("enter the position to place the item");
int p=Integer.parseInt(in.readLine());
for(int i=1;i<p-1;i++)
l=l.next;
m.next=l.next;
l.next=m;
}
if(ch2==3) // to insert at end of list
{
while(l.next!=null) // traversing upto last node
l=l.next;
m.next=null;
l.next=m;
}
}
//else ending
return first;
} // end of insert method
static node delete(node first)throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int ch3,item;
node l=first;
if(first==null)
System.out.println("no items in the list");
else
{
System.out.println("1.start \n 2.middle \n 3.end");
System.out.println("enter your choice");
ch3=Integer.parseInt(in.readLine());
if(ch3==1) // to delete first node
{
item=first.info;
System.out.println("deleted item:"+item);
first=first.next;
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 32
Data Structures Through Java
if(ch3==2) // to delete at specified position
{
System.out.println("enter the position to delete the item");
int p1=Integer.parseInt(in.readLine());
if(l.next==null)
{
item=l.info;
System.out.println("deldeted item is:"+item);
l=first=null;
}
else
{
for(int i=1;i<p1-1;i++)
l=l.next;
item=l.next.info;
System.out.println("deleted item:"+item);
l.next=l.next.next;
}
}
if(ch3==3)// to delete last node
{
if(l.next==null)
{
item=l.info;
System.out.println("deleted item is:"+item);
l=first=null;
}
else
{
while(l.next.next!=null)
l=l.next;
item=l.next.info;
System.out.println("deleted item is:"+item);
l.next=null;
l=l.next;
}
//else ending
}
}
return first;
}
}
//class linkedlist ending
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 33
Data Structures Through Java
Output:
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
1
enter the item
10
enter the item
20
enter the item
30
enter the item
100
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
4
10
20
30
100
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
2
enter the item to be inserte
35
1.start
2.middle
3.end
enter your choice
1
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
4
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 34
Data Structures Through Java
35
10
20
30
100
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
3
1.start
2.middle
3.end
enter your choice
1
deleted item:35
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
4
10
20
30
100
1.create
2.insert
3.delete
4.display
5.exit
enter your choice
5
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 35
Data Structures Through Java
Aim : To write a java program to implement circular queue using an array
Program :
import java.io.*;
class CQueue
{
int q[]=new int[5];
int max=5,f=-1,r=-1;
void insert()throws IOException
{
System.out.print("Enter Element:");
DataInputStream in= new DataInputStream(System.in);
int ele=Integer.parseInt(in.readLine());
if((f==0&&r==max-1) || r == f-1)
System.out.println("Queue is full");
else
if(f==-1)
{
q[++r]=ele;
++f;
}
else q[++r]=ele;
}
void delete()
{
if(f>r ||r==-1)
System.out.println("Queue is empty");
else
System.out.println("Deleted item is: "+q[f++]);
}
void display()
{
if(r==-1)
System.out.println("Queue is empty");
else
{
System.out.print("Elements in queue: ");
for(int i=f;i<=r;i++)
System.out.print(" "+q[i]);
}
}
public static void main(String[] args) throws IOException
{
DataInputStream in= new DataInputStream(System.in);
int x;
CQueue q=new CQueue();
do
{
System.out.println("\n\nEnter your
choise\n1.Insert 2.delete
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 36
Data Structures Through Java
3.display 4.exit");
x=Integer.parseInt(in.readLine());
switch(x)
{
case 1:
q.insert();
break;
case 2:
q.delete();
break;
case 3:
q.display();
break;
}
}while (x!=4);
}
}
Output :
Enter your choise
1.Insert 2.delete 3.display 4.exit
1
Enter Element:10
Enter your choise
1.Insert 2.delete 3.display 4.exit
1
Enter Element:20
Enter your choise
1.Insert 2.delete 3.display 4.exit
1
Enter Element:30
Enter your choise
1.Insert 2.delete 3.display 4.exit
2
Deleted item is: 10
Enter your choise
1.Insert 2.delete 3.display 4.exit
3
Elements in queue: 20 30
Enter your choise
1.Insert 2.delete 3.display 4.exit
1
Enter Element:35
Enter your choise
1.Insert 2.delete 3.display 4.exit
3
Elements in queue: 20 30 35
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 37
Data Structures Through Java
Aim : To write java programs for implementing the following sorting
methods.
a) Bubble sort
b) Radix sort
c) Selection sort
d) Merge sort
e) Insertion sort
Program :
a) Program for Bubble sort :
import java.io.*;
class Bubble
{
public static void main(String args[])throws IOException
{
int a[]=new int[5];
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter your array values");
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(d.readLine());
for(int j=0;j<a.length;j++)
{
for(int i=1,k=0;k<4&&i<5;i++,k++)
{
if(a[k]>a[i])
{
int l=a[k];
a[k]=a[i];
a[i]=l;
}
}
}
for(int m=0;m<5;m++)
System.out.println(“ “ +a[m]);
}
}
Output :
enter your array values
9
8
6
5
4
4 5 6 8 9
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 38
Data Structures Through Java
b) Radix Sort :
import java.io.*;
class radix
{
public static void main(String args[])throws IOException
{
int i, n, j, k, p, d;
double x[] = new double[20];
double a[][] = new double[10][10];
double front[] = new double[10];
double rear[] = new double[10];
DataInputStream in = new DataInputStream(System.in);
System.out.print("Enter number of elements: ");
n = Integer.parseInt(in.readLine());
System.out.println("Enter " + n + " elements: ");
for (i = 0; i < n; i++)
x[i] = Double.parseDouble(in.readLine());
for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 9; i++)
{
front[i] = 0;
rear[i] = -1;
}
for (k = 0; k < n; k++)
{
d = (int)(x[k] / Math.pow(10, j - 1));
d = d % 10;
rear[d]++;
a[d][(int)rear[d]] = x[k];
}
p = 0;
for (k = 0; k <= 9; k++)
{
if (rear[k] != -1)
{
for (i = (int)front[k]; i <= rear[k]; i++)
x[p++] = a[k][i];
}
}
}
System.out.println("Sorted elements are:\n ");
for (i = 0; i < n; i++)
System.out.print("\t" + x[i]);
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 39
Data Structures Through Java
Output:
Enter number of elements: 5
Enter 5 elements:
2
6
8
4
3
Sorted elements are:
2.0
3.0
4.0
6.0
8.0
c)Selection Sort :
import java.io.*;
class Select
{
public static void main(String args[]) throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter your array size");
int m=Integer.parseInt(d.readLine());
int a[]=new int[m];
System.out.println("enter your array values to be sorted");
for(int j=0;j<m;j++)
a[j]=Integer.parseInt(d.readLine());
for(int i=0;i<m;i++)
{
int min=a[i];
int p=i;
for(int j=i+1;j<m;j++)
{
if(a[j]<min)
{
min=a[j];
p=j;
}
}
int k=a[i];
a[i]=a[p];
a[p]=k;
}
for(int i=0;i<a.length;i++)
System.out.print(" "+a[i]);
}
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 40
Data Structures Through Java
Output:
enter your array size
10
enter your array values to be sorted
9
8
6
5
4
7
3
2
1
98
1 2 3 4 5 6 7 8 9 98
d) MergeSort :
import java.io.*;
class merge
{
public static void main(String args[])throws IOException
{
int x[]=new int[5];
int y[]=new int[5];
int z[]=new int[10];
int i,j,k;
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter your first array values");
for(i=0;i<5;i++)
x[i]=Integer.parseInt(d.readLine());
System.out.println("enter your second array values");
for(i=0;i<5;i++)
y[i]=Integer.parseInt(d.readLine());
for(i=0;i<y.length;i++)
{
for(j=i+1;j<y.length;j++)
{
if(y[i]>y[j])
{
k=y[i];
y[i]=y[j];
y[j]=k;
}
}
}
for(i=0;i<x.length;i++)
{
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 41
Data Structures Through Java
for(j=i+1;j<x.length;j++)
{
if(x[i]>x[j])
{
k=x[i];
x[i]=x[j];
x[j]=k;
}
}
}
for(i=0,j=0,k=0;i<5&&j<5; )
{
if(x[i]>y[j])
{
z[k]=y[j];
j++;
k++;
}
else
{
z[k]=x[i];
k++;
i++;
}
}
for( ;i<5;i++)
{
z[k]=x[i];
k++;
}
for( ;j<5;j++)
{
z[k]=y[j];
k++;
}
for(i=0;i<10;i++)
System.out.print(" "+z[i]);
}
}
Output :
enter your first array values
2
6
8
4
7
enter your second array values
11
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 42
Data Structures Through Java
36
24
52
89
2 4 6 7 8 11 24 36 52 89
Insertion sort:
import java.io.*;
class Ins
{
public static void main(String[] args) throws IOException
{
int a[]=new int[5];
System.out.println("enter the elements to sort");
DataInputStream d=new DataInputStream(System.in);
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(d.readLine());
for(int i=1;i<a.length;i++)
{
int x=a[i];
int j=i-1;
while(j>=0&&a[j]>x)
{
a[j+1]=a[j];
j--;
}
a[j+1]=x;
}
System.out.println("after sorting elments are");
for(int i=0;i<a.length;i++)
System.out.print(" "+a[i]);
}
}
Output:
enter the elements to sort
42
6
8
5
1
after sorting elments are
12568
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 43
Data Structures Through Java
Aim: To write a java program to implement the deque using array.
Program :
Class dequeary
{
Public static int q[]=new int[10];
Public static int lf,lr,rf,rr,size,ch,l;
Public static void main(String args[]) throws IOException
{
Int ch,l;
DataInputStream in=new DataInputStream(System.in);
System.out.println(“enter the size”);
Size=Integer.parseInt(in.readLine());
lf=r;
lr=-1;
rr=size;
rf=size-1;
while(true)
{
System.out.println(“1.insert 2.delete 3.display 4.exit “);
System.out.println(“enter your choice”);
ch=Integer.parseInt(in.readLine());
switch(ch)
{
Case 1: insert();
break;
case 2 : delete();
break;
case 3: display();
break;
case 4: System.exit(l);
}
}
}
Static void insert() throws IOException
{
int ele,chl;
DataInputStream in=new DataInputStream(System.in);
if(lr+l=rr)
System.out.orintln(“queue Is overflow”);
else
{
System.out.println(“1.left insertion 2.right insertion”);
System.out.println(“enter your choice”);
chl=Integer.parseInt(in.readLine());
System.out.println(“enter the element”);
ele=Integer.parseInt(in.readLine());
switch(chl)
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 44
Data Structures Through Java
{
case 1: lr++;
q[lr]=ele;
break;
case 2 : rr--;
q[rr]=ele;
break;
}
}
}
Static void delete()throws IOException
{
int ele,ch2;
DataInputStream in=new DataInputStream(System.in);
System.out.println(“1.left deletion 2.right deletion”);
System.out.println(“enter your choice”);
Ch2=Integer.parseInt(in.readLine());
switch(ch2)
{
Case 1: if(lf>lr)
{
System.out.println(“left underflow”);
return;
}
else
{
ele=q[lf];
System.out.println(“element deleted is :” +ele);
lf++;
}
break;
case 2 : if(rf<rr)
{
System.out.println(“right underflow”);
return;
}
else
{
ele=q[rf];
System.out.println(“element deleted is :” +ele);
rf--;
}
break;
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 45
Data Structures Through Java
Static void display() throws IOException
{
int I;
if(lf>lr && rf<rr)
System.out.println(“***LIST IS EMPTY***\n”);
else
{
System.out.println(“the elements in the deque are : “);
for(i=lf;i<=lr;i++)
System.out.println(q[i]);
for(i=rr;i<=rf;i++)
System.out.println(q[i]);
}
}
}
Output:
The items in the deque array are
45
50
78
20
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 46
Data Structures Through Java
Aim: To write a java program that uses both stack and queue to test
whether the given string is palindrome.
Program :
import java.io.*;
class sample
{
Char q[]=new char[20];
Char st[]=new char[20];
int rear=-1,top=-1,front=0;
void queue(char q)
{
Qu[++rear]=q;
}
Void stack(char s)
{
St[++top]=s;
}
Public static void main(String args[]) throws IOException
{
Sample s=new Sample();
DataInputStream in=new DataInputStream(System.in);
String str;
Int i=0,p=0;
System.out.println(“to find given string is palindrome or
not”);
System.out.println(“enter a String”);
str=String.valueOf(in.readLine());
int l=str.length();
for(i=0;i<l;i++)
{
S.queue(str.charAt(i));
S.stack(str.charAt(i));
}
While(s.front<=s.rear && s.top>=-1)
{
if(s.q[s.front]==s.st[s.top])
{
s.front++;
s.top--;
}
else
{
System.out.println(“given string is not a
palindrome”);
break;
}
}
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 47
Data Structures Through Java
if(s.top==-1)
System.out.println(“given string is palindrome”);
}
}
Output :
To find the given string is palindrome or not
Enter a string
Madam
Given string is palindrome
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 48
Data Structures Through Java
Aim : To write a java program to implement stack using singly linked list.
Program :
import java.io.*;
import java.lang.*;
Class node
{
Int info;
Node next;
}
Class stacklink
{
Public static void main(String args[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
Stacklink a=new stacklink();
Node stack=null;
Int ch;
System.out.println(“\nt1.\tpush\n\t2\tpop\n\t3\tdisplay\n\t4texit\n”);
While(true)
{
System.out.println(“enter your choice”);
Ch=Integer.parseInt(in.readLine());
Switch(ch)
{
Case 1:
System.out.println(“enter the element inserted”);
Int i=Integer.parseInt(in.readLine());
Stack=a.push(stack,i);
Break;
Case 2:
Stack=a.pop(stack);
Break;
Case 3:
a.disply(stack)
break;
case 4:
System.exit();
}
}
}
Static node push(node stack, int info)
{
Node temp=new node();
System.out.println(“ “+info);
Temp.info=info;
Temp.next=stack;
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 49
Data Structures Through Java
Stack=temp;
Return(stack);
}
Static void display(node stack)
{
System.out.println(“top”);
While(stack!=null)
{
System.out.println(“ “ +stack.info);
Stack=stack.next;
}
}
Static node pop(node stack)
{
Node temp;
If(stack==null)
System.out.println(“pop operation is not possible”);
else
{
Temp=stack;
System.out.println(“poppedelement is” + temp.info);
Stack=stack.next;
}
Return(stack);
}
}
Output:
The elements in the stack are
20
30
50
80
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 50
Data Structures Through Java
Aim: To write a java program to implement queues using linked list.
Program :
import java.io.*;
import java.lang.*;
Class qlist
{
Int data;
Qlist next;
Qlist front,temp,rear,l;
Void insert(int item)
{
System.out.println(“enter the item to be inserted “+item);
Qlist temp=new qlist();
Temp.data=item;
If(front==null)
{
Temp.next=null;
Rear.next=temp;
Rear=temp;
}
}
Void transerve()
{
If(front==null)
System.out.println(“list is empty”);
Else
{
For(l=front;l!=null;l=l.next)
System.out.println(“ “ +l.data);
}
}
Void delete()
{
Int item;
If(front==null)
System.out.println(“list is empty”);
Else
{
Item=front.data;
System.out.println(“deleted item from queue is “ +item);
Front=front.next;
}
}
Public static void main(String args[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 51
Data Structures Through Java
Qlist q=new qlist();
Int item;
System.out.println(“queue operations 1.enque\n 2.delete\n
3.tr4.exit”);
While(true)
{
System.out.println(“enter queue operation”);
Int choice=Integer.parseInt(in.readLine());
Switch(choice)
{
Case 1:
System.out.println(“enter the item”);
Item=Integer.parseInt(in.readLine());
q.insert(item);
break;
case 2:
q.delete();
break;
case 3:
q.transerve();
break;
case 4:
System.exit(l);
}
}
}
}
Output :
The items in the queue are
10
15
12
56
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 52
Data Structures Through Java
Aim: to write a java program to implement quick sort technique
Program:
import java.io.*;
class Quick
{
static int a[]=new int[10];
static int low,high,n;
static void quick_srt(int low,int high)
{
boolean flag=true;
if(low<high)
{
int i=low+1,j=high,piv=a[low];
while(flag)
{
while(a[i]<piv&&i<high)
i++;
while(a[j]>piv&&j>low)
j--;
if(i<j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
else
break;
}
a[low]=a[j];
a[j]=piv;
quick_srt(low,j-1);
quick_srt(j+1,high);
}
}
public static void main(String args[])throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter array size: ");
int n=Integer.parseInt(d.readLine());
System.out.println("Enter array elements: ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(d.readLine());
System.out.println("Before sorting array values are ");
for(int i=0;i<n;i++)
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 53
Data Structures Through Java
System.out.println("\t" +a[i]);
low=0;
high=n-1;
quick_srt(low,high);
System.out.println("\n After sorting the values are ");
for(int i=0;i<n;i++)
System.out.println("\t"+a[i]);
}
}
Output:
Enter array size:
5
Enter array elements:
85
96
12
48
99
Before sorting array values are
85
96
12
48
99
After sorting the values are
12
48
85
96
99
Padmasri Dr.B.V.Raju Institute of Technology
M.C.A.
Page 54
Related documents