Tuesday, October 20, 2009

Find how many prime no's are in given five numbers
#include
void main()
{
int a[5],i,j,div=0,tot=0;
printf("\n Enter Five no.");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
for(i=0;i<=4;i++)
{
div=0;
for(j=1;j<=a[i];j++)
{
if(a[i]%j==0)
{
div=div+1;
}
}
if(div==2)
{
tot=tot+1;
}
}
printf(" no of prime no are %d",tot);
}
Find how many prime no's are in given five numbers
#include
void main()
{
int a[5],i,j,div=0,tot=0;
printf("\n Enter Five no.");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
for(i=0;i<=4;i++)
{
div=0;
for(j=1;j<=a[i];j++)
{
if(a[i]%j==0)
{
div=div+1;
}
}
if(div==2)
{
tot=tot+1;
}
}
printf(" no of prime no are %d",tot);
}

Thursday, October 8, 2009

``````````
SMTP
=========
import java.io.*;
import java.net.*;
import java.util.*;
public class smtp
{

public static void main(String args[]) throws Exception
{
Socket s=null;
try{

s=new Socket("192.168.1.4",25);
System.out.println("Connected to smtp server");
}
catch(Exception e){}

PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
in.readLine();
out.println("helo");
in.readLine();
System.out.println("Enter ur mail id");
String sid=sin.readLine().trim();
out.println("mail from:"+sid);
System.out.println(in.readLine());
System.out.println("Enter receiver id");
String rid=sin.readLine().trim();
out.println("rcpt to:"+rid);
System.out.println(in.readLine());
System.out.println("Enter ur Message to send");
String msg=sin.readLine().trim();
out.println("data");
in.readLine();
out.println(msg);
out.println(".");
System.out.println(in.readLine());
System.out.println("mail sent");

}

}

```````````
POP3
=========
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
class pop
{

public static void main(String args[]) throws Exception
{
Socket s=null;
try{

s=new Socket("192.168.1.4",110);
System.out.println("Connected to pop server");
}
catch(Exception e){}

PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println(in.readLine());
System.out.println("Enter ur mail id");
String sid=sin.readLine().trim();
System.out.println(sid);
out.println("user "+sid);
System.out.println(in.readLine());
System.out.println("Enter ur Password");
String pwd=sin.readLine().trim();
out.println("pass "+pwd);
System.out.println(in.readLine());
System.out.println("Mail in INBOX");
out.println("list");
String str;
while((str=in.readLine())!=null)
{
if(!str.startsWith("."))
System.out.println(str);
else
break;
}

System.out.println("Enter mail No: ");
String mailno=sin.readLine();
out.println("retr "+mailno);
System.out.println(in.readLine());
while((str=in.readLine())!=null)
{
if(!str.startsWith("."))
System.out.println(str);
else
break;

}

System.out.println();
}

}

Sunday, September 13, 2009



how to run telnet program
find ip address by typing ipconfig

1. Run java mserver // server program in one to many chat
2. Run Telnet
type following and press enter
open 192.168.1.37 4000

telnet

====
Telnet program

===
import java.net.*;
import java.io.*;
import java.util.*;
public class telnet extends Thread
{
static Socket cs=null;
static PrintWriter pw=null;
static BufferedReader br=null,in=null;
public static void main(String args[]) throws Exception
{
System.out.println("TELNET");
in=new BufferedReader(new InputStreamReader(System.in));
String command=in.readLine();
StringTokenizer st=new StringTokenizer(command);
String service=st.nextToken();
if(service.equals("open"))
{
String address=st.nextToken();
String port=st.nextToken();
if(!port.equals("null"))
open(address,port);
}
else if(service.equals("quit"))System.exit(0);
else System.out.println("invalid,try help");
}
static void open(String address,String port) throws Exception
{
try
{
if(address.equals("null"))
{
System.out.println("Specify correct addresss");
return;
}
System.out.println("Waiting for a connection");
cs=new Socket(address,Integer.parseInt(port));
br=new BufferedReader(new InputStreamReader(cs.getInputStream()));
pw=new PrintWriter(cs.getOutputStream(),true);
System.out.println("Connection Established");
new SendData(pw,in).start();
new ReceiveData(br).start();
}
catch(Exception e)
{
System.out.println("Unable to connect");
}
}

static class SendData extends telnet
{
PrintWriter pw=null;
BufferedReader in=null;
SendData(PrintWriter p,BufferedReader b)
{
pw=p;
in=b;
}
public void run()
{
while(true)
{
try
{
pw.println(in.readLine());
}
catch(IOException e){}
}
}
}
static class ReceiveData extends telnet
{
BufferedReader br=null;
ReceiveData(BufferedReader b)
{ br=b;
}
public void run()
{
while(true)
{
try
{
System.out.println(br.readLine());
}
catch(IOException e){}
}
}
}
}

Monday, September 7, 2009

random number between 0 and 2
===============================
import java.math.*;
public class k
{

public static void main(String a[])
{
Double k;

k=Math.random()*1000;
int s=k.intValue();
s= s%3;
System.out.println(" s is "+s);
}

}
DB CLIENT
==========
import java.io.*;
import java.net.*;
public class DBClient
{
static Socket cs=null;
public static void main(String args[]) throws IOException
{
try
{
cs=new Socket("localhost",1090);
}
catch(Exception e){}
System.out.println("Enter the code");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter out=new PrintWriter(cs.getOutputStream(),true);
System.out.println(in.readLine());
String user=null,pass=null;
while(true)
{
System.out.println("commands:1:insert 2:update 3:select 4:delete 5:Exit");
int choice=Integer.parseInt(stdin.readLine());
switch(choice)
{
case 1:System.out.println("Enter username and password to be inserted");
user=stdin.readLine();
pass=stdin.readLine();
out.println("1");
out.println(user);
out.println(pass);
System.out.println(in.readLine());
break;
case 2:System.out.println("Enter username and new password to be updates");
user=stdin.readLine();
pass=stdin.readLine();
out.println("2");
out.println(user);
out.println(pass);
System.out.println(in.readLine());
break;
case 3:System.out.println("enter username whose password is needed");
user=stdin.readLine();
out.println("3");
out.println(user);
System.out.println(in.readLine());
break;
case 4:System.out.println("enter username of person whose details has to be deleted?");
user=stdin.readLine();
out.println("4");
out.println(user);
System.out.println(in.readLine());
break;
case 5: System.exit(1);
default:break;
}
}
}
}
DB SERVER
==========
import java.io.*;
import java.sql.*;
import java.net.*;
public class DBServer
{
static Connection con;
static Statement st1,st2;
static ResultSet rs1;

static ResultSetMetaData rsmd;
static String[] rec=new String[10];
static ServerSocket ss=null;
static Socket s=null;
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:irk","system","bvrice");

st1=con.createStatement();
st2=con.createStatement();
System.out.println("Done");
}
catch(Exception e)
{
System.out.println("Error establishing connection"+e);
}
String output=" ",qry=" ";
try
{
ss=new ServerSocket(1090);
System.out.println("Server");
s=ss.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println("welcome to DBServer");
while(true)
{
int choice=Integer.parseInt(in.readLine());
String usr=null,pass=null;
int c=0;
switch(choice)
{
case 1:usr=in.readLine();
pass=in.readLine();
c=st1.executeUpdate("insert into userinfo values('"+usr+"','"+pass+"')");
if(c==1) out.println("Date inserted succcessfully");
break;
case 2: usr=in.readLine();
pass=in.readLine();
System.out.println("username="+usr+"password="+pass);
rs1=st1.executeQuery("select * from userinfo where userid='"+usr+"'");
while(rs1.next())
{
c=st1.executeUpdate("Update userinfo set password='"+pass+"' where userid ='"+usr+"'");
}
if(c==1) out.println("password updated for the given username");
else
out.println("update not possible");
break;
case 3:usr=in.readLine();
rs1=st1.executeQuery("select * from userinfo where userid='"+usr+"'");
rs1.next();
System.out.println(rs1.getString(1)+" "+rs1.getString(2));
out.println(rs1.getString(2));
break;
case 4:usr=in.readLine();
c=st1.executeUpdate("delete userinfo where userid='"+usr+"'");
if(c==1) out.println("userinformation for the following userid"+usr+"is deleted");
break;
default:break;
}
}
}
catch(Exception ie)
{}
}
}

Monday, August 31, 2009

TO Get Any Table
=================
import java.sql.*;
import java.util.*;
class Anytable_Retrieval
{
public static void main(String[] args) throws SQLException
{
Connection con;
Statement st;
ResultSet rs;
ResultSetMetaData rsmd;
Scanner sc=new Scanner(System.in);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:sagar","system","bvrice");
while(true)
{

System.out.println("Enter which table details you want :");
String str=sc.next();

st=con.createStatement();
rs=st.executeQuery("select * from "+str);
rsmd=rs.getMetaData();

int cc=rsmd.getColumnCount();
for(int i=1;i<=cc;i++)
System.out.print(rsmd.getColumnName(i)+" ");
System.out.println();
while(rs.next())
{

for(int i=1;i<=cc;i++)
{
System.out.print(rs.getString(i)+" ");
}
System.out.println();
} // end inner while
System.out.println("Do you want another table details(false/true) :");
boolean choice=sc.nextBoolean();
if(!choice) // false
break;

}// end outter while
rs.close();
st.close();
con.close();
} // try
catch(Exception e)
{
System.out.println(e);
}
}
}

Monday, August 24, 2009

DB

=====DB1========
import java.io.*;
import java.sql.*;
import java.net.*;
public class DB1
{
static Connection con;
static Statement st1,st2;
static ResultSet rs1;
//static ResultSetMetaData rsmd;
static PreparedStatement pstmt;
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:irk");
pstmt = con.prepareStatement("update emp set name = ? where eno = ?");
pstmt.setString(1,"krishna");
pstmt.setString(2,"e1");
pstmt.executeUpdate();

st1=con.createStatement();
System.out.println("Done");
rs1=st1.executeQuery("select * from emp");
while(rs1.next())
{
System.out.println(" "+rs1.getString(1)+" "+rs1.getString(2));
}
st2=con.createStatement();

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

}
}

=========DB--------
import java.io.*;
import java.sql.*;
import java.net.*;
public class DB
{
static Connection con;
static Statement st1,st2;
static ResultSet rs1;
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:tri");
st1=con.createStatement();
System.out.println("Done");
rs1=st1.executeQuery("select * from emp");
while(rs1.next())
{
System.out.println(" "+rs1.getString(1)+" "+rs1.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}

}
}

Tuesday, July 28, 2009

NP6

TFTP Server

import java.io.*;
import java.net.*;
public class TFTPserver extends Thread
{


static DatagramSocket ds;
static DatagramPacket inPacket,outPacket;
static byte[] buffer;
static int id=0;
static int port=2556;
public static void main(String args[]) throws IOException
{
try{
ds=new DatagramSocket(69);
}
catch(SocketException se){System.out.println("Unable to connect");
}
while(true)
{
buffer=new byte[512];
inPacket=new DatagramPacket(buffer,buffer.length);
ds.receive(inPacket);
InetAddress clientAddress=inPacket.getAddress();
int clientPort=inPacket.getPort();
System.out.println("Clientport:"+clientPort);
String messageIn=new String(inPacket.getData(),0,inPacket.getLength());
id++;
new ClientThread(clientAddress,clientPort,messageIn,id).start();
}
}
}
class ClientThread extends TFTPserver
{
int port,clientid;
DatagramSocket dse;
DatagramPacket dpe,dpk;
InetAddress ca;
File f;
String filename;
ClientThread(InetAddress x,int y,String z,int w)
{
ca=x;
try{
dse=new DatagramSocket(++(TFTPserver.port));}
catch(SocketException see)
{}
filename=z;
port=y;
clientid=w;
}
public void run()
{
while(true)
{
System.out.println("filename received"+filename);
f=new File(filename);
try
{
if(f.exists())
{
BufferedReader br=new BufferedReader(new FileReader(filename));
String str;
while((str=br.readLine())!=null)
{
System.out.println("Sending file data"+str);
dpe=new DatagramPacket(str.getBytes(),str.length(),ca,port);
dse.send(dpe);
}
str="sent";
dpe=new DatagramPacket(str.getBytes(),str.length(),ca,port);
dse.send(dpe);
}
}
catch(Exception e){}
try
{
byte[] buffer=new byte[512];
dpk=new DatagramPacket(buffer,buffer.length);
dse.receive(dpk);
filename=new String(dpk.getData(),0,dpk.getLength());
}
catch(IOException me){}
}
}
}
=================
TFTP Client
import java.io.*;
import java.net.*;
import java.util.*;

public class TFTPClient
{
static InetAddress host;
static DatagramSocket dss;
static DatagramPacket outPacket,inPacket;
static byte[] buffer;
static int port=69;
public static void main(String args[])
{
try
{
host=InetAddress.getByName("localhost");
}
catch(UnknownHostException uhe)
{
System.out.println("Host Not found");
}
try
{
dss=new DatagramSocket();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name should be received from server");
String str1,finput;
BufferedWriter bw;
while(!(str1=br.readLine()).equals("null"))
{
System.out.println("haiiiiiiii");
outPacket=new DatagramPacket(str1.getBytes(),str1.length(),host,port);
System.out.println("haiiiiiiiii");
dss.send(outPacket);
bw=new BufferedWriter(new FileWriter("kk.txt"));
System.out.println("Created fileinput stream");
do
{
System.out.println("Receiving file data");
buffer=new byte[512];
inPacket=new DatagramPacket(buffer,buffer.length);
dss.receive(inPacket);
System.out.println("Server Port="+inPacket.getPort());
finput=new String(inPacket.getData(),0,inPacket.getLength());
if(!(finput.equals("sent")))bw.write(finput,0,finput.length());
bw.newLine();
bw.flush();
}
while(!finput.equals("sent"));
port=inPacket.getPort();
System.out.println(port);
}
}
catch(Exception ie)
{}
}
}

Friday, July 24, 2009

NP5

FTP SERVER

import java.io.*;
import java.net.*;
public class fs
{
public static void main(String args[]) throws Exception
{
try
{
ServerSocket ss=new ServerSocket(1334);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String str;
int x=0;
str=in.readUTF();
File f=new File(str);
if(f.exists())
{ out.write(x);
FileInputStream fin=new FileInputStream(str);
int i;
do
{
i=fin.read();
out.write(i);
}
while(i!=-1);
System.out.println("File transfered Successfully...");
}
else
System.out.println("File not found...");
}
catch(Exception e){}
}
}
FTP CLIENT

import java.io.*;
import java.net.*;
public class fc
{
public static void main(String args[])
{
int i=0,x=1; String str="null";
try
{
Socket s=new Socket("192.168.0.18",1334);


DataInputStream in=new DataInputStream(s.getInputStream());

DataOutputStream out=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("enter file");

str=br.readLine();

out.writeUTF(str);

x=in.read();

if(x==0)

{

FileOutputStream fout=new FileOutputStream(str);

i=0;
while(i!=-1)
{
i=in.read();
fout.write(i);

}

}
}

catch(Exception e){}


if(x!=0) System.out.println("File not found...");
else System.out.println("File File received successfully");
}





}

Monday, July 20, 2009

update

while(true)
{
String msg1;
msg1=stdin.readLine();
System.out.println("server:"+msg1);
if(msg1!=null)
{
for(int i=0;i<
v.size();i++)
((PrintWriter)v.elementAt(i)).println("server"+":"+msg1);
}
}
}

Thursday, July 16, 2009

NP4

one to many chat



mserver.java

import java.io.*;

import java.net.*;

import java.util.*;

class mserver extends Thread

{

static ServerSocket ss;

static Socket cs=null;

static Vector v=new Vector(100);

static PrintWriter pw;

static int id=1;

public static void main(String args[])

{

try

{

ss=new ServerSocket(4000);

InputScan1 is=new InputScan1();

is.start();

while(true)

{

cs=ss.accept();

new ClientThread(cs,id).start();

pw=new PrintWriter(cs.getOutputStream(),true);

v.addElement(pw);

System.out.println("connected to client"+id);

id++;

}

}

catch(Exception e)

{}

}

}

class ClientThread extends mserver

{

Socket cs;

int id;

ClientThread(Socket clientsocket,int i)

{

cs=clientsocket;

id=i;

}

public void run()

{

try

{

BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream()));

String msg=null;

while(true)

{

msg=in.readLine();

System.out.println("client"+id+":"+msg);

if(msg!=null)

{

for(int i=0;i
((PrintWriter)v.elementAt(i)).println("client"+id+":"+msg);

}

}

}

catch(Exception e)

{}

}

}

class InputScan1 extends mserver

{

DataInputStream stdin=new DataInputStream(System.in);

public void run()

{

try

{

while(true)

{

String msg1;

msg1=stdin.readLine();

System.out.println("server:"+msg1);

if(msg1!=null)

{

for(int i=0;i
((PrintWriter)v.elementAt(i)).println("server"+":"+msg1);

}

}

}

catch(Exception e)

{ }

}

}





mclient.java



import java.io.*;

import java.net.*;

public class mclient extends Thread

{

static Socket cs=null;

public static void main(String args[])

{

try

{

cs=new Socket("localhost",4000);

System.out.println("connected to server");

BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream()));

InputScan is=new InputScan();

is.start();

while(true)

{

String msg;

msg=in.readLine();

System.out.println(msg);

}

}

catch(Exception e) { }

}

}



class InputScan extends mclient

{

DataInputStream stdin=new DataInputStream(System.in);

public void run()

{

try

{

PrintWriter out=new PrintWriter(cs.getOutputStream(),true);

while(true)

{

String msg;

msg=stdin.readLine();

out.println(msg);

}

}

catch(Exception e){}

}

}

Friday, July 10, 2009

NP3

UPD
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPEchoClient
{
private static InetAddress host;
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
try{
host=InetAddress.getLocalHost();
}
catch(UnknownHostException u)
{
System.out.println("Host ID not found");
System.exit(1);
}
accessServer();
}
private static void accessServer()
{try{
datagramSocket=new DatagramSocket();
DataInputStream userEntry=new DataInputStream(System.in);
String message="",response="";
do{
System.out.println("Enter Message");
message=userEntry.readLine();
if(!message.equals("CLOSE"))
{
outPacket=new DatagramPacket(message.getBytes(),message.length(),host,PORT);
datagramSocket.send(outPacket);
buffer=new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
response=new String(inPacket.getData(),0,inPacket.getLength());
System.out.println("\n"+response);
}
}while(!message.equals("CLOSE"));
}
catch(IOException ex)
{
ex.printStackTrace();
}
finally{
System.out.println("\nClosing Connection");
datagramSocket.close();
}
}
}

=================
import java.io.*;
import java.net.*;
public class UDPEchoServer
{
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
System.out.println("Opening Port...\n");
try{
datagramSocket=new DatagramSocket(PORT);
}
catch(SocketException sockEx)
{
System.out.println("Unable to attach to port");
System.exit(1);
}
handleClient();
}
private static void handleClient()
{
try{
String messageIn,messageOut;
int numMessages=0;
do{
buffer =new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
InetAddress clientAddress=inPacket.getAddress();
int clientPort=inPacket.getPort();
messageIn=new String(inPacket.getData(),0,inPacket.getLength());
System.out.println("Message Receives");
numMessages++;
messageOut="Message"+numMessages+":"+messageIn;
outPacket=new DatagramPacket(messageOut.getBytes(),messageOut.length(),clientAddress,clientPort);
datagramSocket.send(outPacket);
}while(true);
}
catch(IOException e)
{e.printStackTrace();
}
finally
{
System.out.println("\nClosing Connection..\n");
datagramSocket.close();
}
}
}

Friday, July 3, 2009

NP2

One to One Server
import java.io.*;
import java.net.*;
public class server1
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(2020);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=in.readUTF();
System.out.println(str);
str=br.readLine();
out.writeUTF(str);
}
}
catch(Exception e){}
}
}

One to One Client
import java.io.*;
import java.net.*;
public class client1
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",2020);
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=br.readLine();
out.writeUTF(str);
str=in.readUTF();
System.out.println(str);
}
}
catch(Exception e){}
}
}

Monday, June 29, 2009

NP1

import java.net.*;
public class s
{

public static void main(String a[])
{
Socket s;
for(int i=1;i<1024;i++)
{
try
{
s= new Socket("localhost",i);
System.out.println(" Active Port No is "+i);
}
catch(Exception e){ }
}
}

}

Monday, April 20, 2009

HEAPSORT

/*PROGRAMME ON HEAPSORT*/
#include
#include
void heapsort(int x[],int n)
{
int i,elt,s,f,ivalue;
for(i=1; i < n ;i++)
{
elt=x[i];
s=i;f=(s-1)/2;
while(s>0&& x[f] < elt)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=elt;
}

for(i=n-1;i>0;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2&&x[2]>x[1])
s=2;
while(s>=0&& ivalue< x[s])
{
x[f]=x[s];
f=s;
s=2*f+1;
if(s+1 < = i-1&&x[s] < x[s+1])
s=s+1;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
printf("\n After sorting:");
for(i=0; i < n ;i++)
printf("\n%2d",x[i]);
}
void main()
{
int n,i;
int data[10];
clrscr();
printf("\n Enter size of array:");
scanf("%d",&n);
for(i=0; i < n ;i++)
scanf("%d",&data[i]);
heapsort(data,n);
printf("\n Elements After Sort ");
for(i=0;i < n;i++)
{
printf("\t %d",data[i]);
}

}

INSERTIONSORT ఇంసెరషన్ సొర్త్

// Insertion Sort

#include
#include
void insertsort(int x[], int n)
{
int i,k,y;
for(k=1;k {
y=x[k];
for(i=k-1;i>=0 && y< x[i] ;i--)
{
x[i+1]=x[i];
}
x[i+1]=y;
}
}

void main()
{
int a[10],i,n;
printf("\n Enter no of elements ");
scanf("%d",&n);
printf("\n Enter Elements ");
for(i=0;i< n;i++)
{
scanf("%d",&a[i]);
}
insertsort(a,n);
printf("\n Elements After Sort ");
for(i=0;i< n;i++)
{
printf("\t %d",a[i]);
}
}

mergesort

void merge(int a[], int low, int high, int mid);
void mergesort(int a[], int low, int high)
{
int mid;

if(low < mid)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
}
void merge(int a[], int low, int high, int mid)
{
int i, j, k, c[10];
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
{
if(a[i] < a[j] )
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
while(j<=high)
{
c[k]=a[j];
k++;
j++;
}
for(i=low; i < k ; i++)
{
a[i]=c[i];
}
}
void main()
{
int a[10],i,n;
printf("\n Enter no of elements ");
scanf("%d",&n);
printf("\n Enter Elements ");
for(i=0;i < n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("\n Elements After Sort ");
for(i=0;i < n;i++)
{
printf("\t %d",a[i]);
}
}

Tuesday, April 7, 2009

BINARY SEARCH TREE Deletion, Insertioan, Traversals

// BINARY SEARCH TREE Deletion, Insertioan, Traversals
struct node
{
int info;
struct node *left,*right;
};
struct node *root;
void insert(struct node *tree,int number)
{
struct node *p,*q,*temp;
p=q=tree;
if(tree==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=number;
temp->left=temp->right=NULL;
root=temp;
}
else
{
while(number !=p->info && q!=NULL)
{
p=q;
if(number <>info )
q=p->left;
else
q=p->right;
}
if(number==p->info)
printf("\n DUPLICATE ");
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=number;
temp->left=temp->right=NULL;
if(number <>info)
p->left=temp;
else
p->right=temp;
}
}
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t\t%d",p->info);
}
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\t\t%d",p->info);
inorder(p->right);
}
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("\t\t%d",p->info);
preorder(p->left);
preorder(p->right);
}
}
void delet(struct node *tree,int number)
{
struct node *p,*q,*rp,*f,*s;
p=tree;
q=NULL;
while(p!=NULL & p->info!=number)
{
q=p;
if(number <>info)
p=p->left;
else
p=p->right;
}
if(p==NULL)
{
printf(" Element Not Found ");
return 0;
}
if(p->left==NULL)
rp=p->right;
else if(p->right==NULL)
rp=p->left;
else
{
f=p;
rp=p->right;
s=rp->left;
while(s!=NULL)
{
f=rp;
rp=s;
s=rp->left;
}
if(f!=p)
{
f->left=rp->right;
rp->right=p->right;
}
rp->left=p->left;
}
if(q==NULL)
root=rp;
else
{
if(p==q->left)
q->left=rp;
else
q->right=rp;
}
}
void main()
{
int el,l,v;
int c;
root=NULL;
while(1)
{
printf("\n1.insert\n 2.preorder\n3.inorder");
printf("\n4.postorder\n5.delete 6.exit\n");
printf("\n Enter U r choice\n"); scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter Element to insert");
scanf("%d",&v);
insert(root,v);
break;
case 2:
preorder(root);break;
case 3:
inorder(root);break;
case 4:
postorder(root);break;
case 5:
printf("\n Enter Element to Delete");
scanf("%d",&v);
delet(root,v);
break;
case 6:
exit(0);
}
}
}

BINARY SEARCH TREE INSERTION, TRAVERSALS

// BINARY SEARCH TREE INSERTION, TRAVERSALS
struct node
{
int info;
struct node *left,*right;
};
struct node *root;
void insert(struct node *tree,int number)
{
struct node *p,*q,*temp;
p=q=tree;
if(tree==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=number;
temp->left=temp->right=NULL;
root=temp;
}
else
{
while(number !=p->info && q!=NULL)
{
p=q;
if(number <>info )
q=p->left;
else
q=p->right;
}
if(number==p->info)
printf("\n DUPLICATE ");
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=number;
temp->left=temp->right=NULL;
if(number <>info)
p->left=temp;
else
p->right=temp;
}
}
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t\t%d",p->info);
}
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\t\t%d",p->info);
inorder(p->right);
}
}

void preorder(struct node *p)
{
if(p!=NULL)
{
printf("\t\t%d",p->info);
preorder(p->left);
preorder(p->right);
}
}
void main()
{
int el,l,v;
int c;
root=NULL;
while(1)
{
printf("\n1.insert\n 2.preorder\n3.inorder");
printf("\n4.postorder\n5.exit\n");
printf("\n Enter U r choice\n"); scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter Element to insert");
scanf("%d",&v);
insert(root,v);
break;
case 2:
preorder(root);break;
case 3:
inorder(root);break;
case 4:
postorder(root);break;
case 5:
exit(0);
}
}
}

Wednesday, April 1, 2009

Circulur Double Linked List

/*CIRCULUR DOUBLE LINKEDLIST*/
#include
#include
#include
#include
#define newnode (struct node *)malloc(sizeof(struct node ))
struct node
{
struct node *llink;
int info;
struct node *rlink;
};
struct node *p,*head,*q,*temp;
void insert(int pos,int val)
{
int ind=1;
q=newnode;
q->info=val;
p=head;
if(pos==1)
{
if(head==NULL)
{
q->rlink=q;
q->llink=q;
head=q;
}
else
{
q->rlink=p;
p->llink=q;
temp=head;
while(temp->rlink!=head)
{
temp=temp->rlink;
}
temp->rlink=q;
q->llink=temp;
head=q;
}
}
else
{
while((ind!=pos-1)&&(p->rlink!=head))
{
p=p->rlink;
ind++;
}
if(p->rlink==head && ind!=pos-1)
{
printf(" \n The position is NOT Valid ");
}
else
{
q->rlink=p->rlink;
p->rlink->llink=q;
p->rlink=q;
q->llink=p;
}
}
}
void delet(int pos)
{
int ind=1;
struct node *dp;
p=head;
if(head==NULL)
printf(" List is empty ");
else
{
if(pos==1)
{
if(head->rlink==head)
{
free(p);
head=NULL;
}
else
{
dp=p;
while(temp->rlink!=head)
{
temp=temp->rlink;
}
p=p->rlink;
head=p;
p->llink=temp;
temp->rlink=p;
free(dp);
}
}
else
{
while(ind!=pos-1 && p->rlink!=head)
{
p=p->rlink;
ind++;
}
if(p->rlink!=head)
{
temp=p->rlink;
p->rlink=p->rlink->rlink;
p->rlink->llink=p;
free(temp);
}
else
{
printf(" \n The position is NOT Valid ");
}
}
}
}
void display()
{
p=head;
if(p==NULL)
printf(" list is empty");
else
{
printf("\n ");
while(p->rlink!=head)
{
printf("\t%d",p->info);
p=p->rlink;
}
printf("\t%d",p->info);
}
}
void main()
{
int op=99;
int post,v;
head=NULL;
while(1)
{
printf("\n Menu");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.exit");
printf("\n Enter U r choice..");
fflush(stdin);
scanf("%d",&op);
switch(op)
{
case 1:printf("\n Enter the pos&val to insert");
fflush(stdin);
scanf("%d%d",&post,&v);
insert(post,v);
break;
case 2:printf("\n Enter the pos delete");
fflush(stdin);
scanf("%d",&post);
delet(post);
break;
case 3:display();
break;
case 4:exit(0);
}
}
}

Double linked list

/* DOUBLE LINKEDLIST*/
#include
#include
#include
#include
#define newnode (struct node *)malloc(sizeof(struct node ))
struct node
{
struct node *llink;
int info;
struct node *rlink;
};
struct node *p,*head,*q;
void insert(int pos,int val)
{
int ind=1;
q=newnode;
q->info=val;
p=head;
if(pos==1)
{
if(head==NULL)
{
q->rlink=NULL;
q->llink=NULL;
head=q;
}
else
{
q->rlink=p;
p->llink=q;
head=q;
}
}
else
{
while((ind!=pos-1)&&(p->rlink!=NULL))
{
p=p->rlink;
ind++;
}
if(p->rlink==NULL && ind!=pos-1)
{
printf(" \n The position is NOT Valid ");
}
else
{
q->rlink=p->rlink;
p->rlink->llink=q;
p->rlink=q;
q->llink=p;
}
}
}
void delet(int pos)
{
int ind=1;
struct node *temp;
p=head;
if(head==NULL)
printf(" List is empty ");
else
{
if(pos==1)
{
if(p->rlink==NULL)
{
free(p);
head=NULL;
}
else
{
temp=p;
p=p->rlink;
head=p;
p->llink=NULL;
free(temp);
}
}
else
{
while(ind!=pos-1 && p->rlink!=NULL)
{
p=p->rlink;
ind++;
}
if(p->rlink!=NULL)
{
temp=p->rlink;
p->rlink=p->rlink->rlink;
p->rlink->llink=p;
free(temp);
}
else
{
printf(" \n The position is NOT Valid ");
}
}
}
}
void display()
{
p=head;
if(p==NULL)
printf(" list is empty");
else
{
printf("\n ");
while(p!=NULL)
{
printf("\t%d",p->info);
p=p->rlink;
}
}
}
void main()
{
int op=99;
int post,v;
head=NULL;
while(1)
{
printf("\n Menu");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.exit");
printf("\n Enter U r choice..");
fflush(stdin);
scanf("%d",&op);
switch(op)
{
case 1:printf("\n Enter the pos&val to insert");
fflush(stdin);
scanf("%d%d",&post,&v);
insert(post,v);
break;
case 2:printf("\n Enter the pos delete");
fflush(stdin);
scanf("%d",&post);
delet(post);
break;
case 3:display();
break;
case 4:exit(0);
}
}
}