ARRAY OPERATIONS
SOURCE CODE:
import java.io.*;
class Arrayf
{
int loc,item;
int a[]=new int[20];
int i,j,n;
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
void create()throws IOException
{
System.out.print("enter array size:");
n=Integer.parseInt(br.readLine());
System.out.print("enter elements in array:");
for(i=0;i<n;i++)
{
System.out.print("enter"+i+"element");
a[i]=Integer.parseInt(br.readLine());
}
}
void insert()throws IOException
{
System.out.println("Enter
location:");
loc=Integer.parseInt(br.readLine());
if(loc>=0&&loc<=n)
{
n=n+1;
System.out.println("enter
data:");
item=Integer.parseInt(br.readLine());
for(i=n;i>loc-1;i--)
{
a[i]=a[i-1];
}
a[loc-1]=item;
System.out.print("......insertion
is success");
}
else
System.out.println("given
location is out of arraybounadary");
}
void delete()throws IOException
{
System.out.println("enter location to remove:");
loc=Integer.parseInt(br.readLine());
if((loc>=0)&&(loc<=n))
{
for(i=loc-1;i<n;i++)
{
a[i]=a[i+1];
}
n--;
System.out.println("element is deleted");
}
else
System.out.println("given location is outof arrayboundary");
}
void display()
{
System.out.println("elements in array are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]+" ");
}
System.out.print(" ");
}
}
class ArrOperation
{
public static void
main(String args[])throws IOException
{
int ch;
Arrayf ar=new
Arrayf();
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
System
case 1:ar.
.out.println("prog to perform an array operations");
do
{
System.out.println("1.create array");
System.out.println("2.insert array");
System.out.println("3.delete array");
System.out.println("4.display
array");
System.out.println("5.exit");
System.out.println("enter ur
choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
create();
break;
case
2:ar.insert();
break;
case
3:ar.delete();
break;
case
4:ar.display();
break;
case 5:break;
default:
System.out.print("invalid choice");
}
}while(ch!=5);
}
}
OUT PUT:
prog to perform an array operations
1.create array
2.insert array
3.delete array
4.display array
5.exit
enter ur
choice:1
enter array size:3
enter elements in array
enter0element:2
enter1element:4
enter2element:6
1.create array
2.insert array
3.delete array
4.display array
5.exit
enter ur
choice:2
Enter location:3
enter data:7
......insertion is successfull
1.create array
2.insert array
3.delete array
4.display array
5.exit
enter ur
choice:4
elements in array are
2
4
7
6
1.create array
2.insert array
3.delete array
4.display array
5.exit
enter ur
choice:5
//ADDITION,MULTIPLICATION
OF POLYNOMIALS//
import java.io.*;
import java.lang.*;
import java.util.Scanner;
class polyad
{
public static
void main(String args[])throws Exception
{
Scanner ob=new
Scanner(System.in);
int m,n;
System.out.print("\nEnter
heighest degree of 1st polynomial :");
m=ob.nextInt();
System.out.print("\nEnter heighest degree of 2nd polynomial
:");
n=ob.nextInt();
int
hd=(m>n?m:n);
int eq1[]=new
int[hd+1];
int eq2[]=new
int[hd+1];
int i,j;
for(i=hd;i>=0;i--)
eq1[i] = eq2[i]
=0;
if(m<n)
{ for(i=m;i>0;i--)
eq1[i]=0;
}
else
{ for(i=n;i>0;i--)
eq2[i]=0; }
System.out.print("\n Enter coefficient value of 1st polynomial
\n");
for(i=m;i>0;i--)
{
System.out.print("\n Enter coeffvalue of "+i+" degree
:");
eq1[i]=ob.nextInt();
}
System.out.print("\n Enter coefficient value of 2nd polynomial
\n");
for(i=n;i>0;i--)
{
System.out.print(" \n Enter coeffvalue of "+i+" degree
:");
eq2[i]=ob.nextInt();
}
int sum[]=new
int[hd+1];
for(i=hd;i>=0;i--)
{
sum[i]=eq1[i]+eq2[i];
}
System.out.print("\n given two equations are----\n");
System.out.print("\n \n 1st equation :");
for(i=m;i>0;i--)
{
if(i==1)
System.out.print(eq1[i] + "x+");
else if(i==0)
System.out.print(eq1[i]);
else
System.out.print(eq1[i]+"x^"+i+"+");
}
System.out.print("\n \n 2nd equation :");
for(i=n;i>0;i--)
{
if(i==1)
System.out.print(eq2[i]+"x+");
else if(i==0)
System.out.print(eq2[i]);
else
System.out.print(eq2[i]+"x^"+i+"+");
}
System.out.print("\n\nsum of two equations is---> ");
for(i=hd;i>=0;i--)
{
if(i==1)
System.out.print(sum[i]+"x+");
else if(i==0)
System.out.print(sum[i]);
else
System.out.print(sum[i]+"x^"+i+"+");
}
int mul[]=new
int[hd+1];
for(i=hd;i>=0;i--)
mul[i]=mul[i]+eq1[i]*eq2[i];
System.out.print("\n\nmultiplication of two polynomials
is----->");
for(i=hd;i>=0;i--)
{
if(i==1)
System.out.print(mul[i]+"x+");
else if(i==0)
System.out.print(mul[i]);
else
System.out.print(mul[i]+"x^"+i+"+");
}
}
}
OUTPUT:
C:\Program Files\Java\jdk1.6.0_13\bin>javac polyad.java
C:\Program Files\Java\jdk1.6.0_13\bin>java polyad
Enter heighest degree of 1st polynomial :3
Enter heighest degree of 2nd polynomial :3
Enter coefficient
value of 1st polynomial
Enter coeffvalue of 3
degree :4
Enter coeffvalue of 2
degree :2
Enter coeffvalue of 1
degree :4
Enter coefficient
value of 2nd polynomial
Enter coeffvalue of 3
degree :5
Enter coeffvalue of 2
degree :3
Enter coeffvalue of 1
degree :2
given two equations
are----
1st equation :4x^3+2x^2+4x+
2nd equation
:5x^3+3x^2+2x+
sum of two equations is---> 9x^3+5x^2+6x+0
multiplication of two polynomials is----->20x^3+6x^2+8x+0
// PROGARM IN JAVA
USING QUEUES TO IMPLEMENT LIBRARY BOOK REQUESTS
import java.io.*;
class Node
{
int sno;
int bookid;
String book
name;
Node next;
}
class QueueLink
{
Node
front=null;
Node
rear=null;
void
enQueue()throws IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
Node
first = new Node();
System.out.print("\nEnter
SNO: ");
first.sno=Integer.parseInt(br.readLine());
System.out.print("Enter
book ID: ");
first.bookid=Integer.parseInt(br.readLine());
System.out.print("Enter
Book Name: ");
first.bookname=br.readLine();
first.next=null;
if(front==null)
{front=rear=first;}
else
{
rear.next=first;
rear=rear.next;
}
}
void
delQueue()
{
if(front==null)
{System.out.println("Queue
is empty..");}
else
{
System.out.println("Removed
book details are.. ");
System.out.print(front.sno+"\t");
System.out.print(front.bookid+"\t");
System.out.print(front.bookname+"\t");
System.out.println("
");
front=front.next;
}
}
void
display()
{
if(front==null)
{System.out.println("Queue
is empty");}
else
{
Node
temp=front;
System.out.println("Issue
Library book lists");
System.out.println("S.no\t
Book_ID \t Book_Name ");
while(temp!=null)
{
System.out.print(temp.sno
+ "\t");
System.out.print(temp.bookid+
"\t\t");
System.out.print(temp.bookname+"\t");
System.out.println(" ");
temp=temp.next;
}
}
}
}
class Library
{
public
static void main(String args[])throws IOException
{
int
sw;
QueueLink
QL=new QueueLink();
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\n\t
Queues to implement Library book request \n");
System.out.println("1.Enter
Book details");
System.out.println("2.Remove
Book Details");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.print("\nEnter
your choice(1-4) : ");
sw=Integer.parseInt(br.readLine());
switch(sw)
{
case
1: QL.enQueue();
break;
case
2: QL.delQueue();
break;
case
3: QL.display();
break;
case 4: break;
default:System.out.println("Invalid
option enter only (1-4)");
}
}while(sw!=4);
}
}
OUTPUT:
C:\Program Files\Java\jdk1.6.0_13\bin>javac Library.java
C:\Program Files\Java\jdk1.6.0_13\bin>java Library
Queues to
implement Library book request
1.Enter Book details
2.Remove Book Details
3.Display
4.Exit
Enter your choice(1-4) :
1
Enter SNO: 101
Enter book ID: 2217
Enter Book Name: DS structure
Queues to
implement Library book request
1.Enter Book details
2.Remove Book Details
3.Display
4.Exit
Enter your choice(1-4) :
3
Issue Library book lists
S.no Book_ID Book_Name
101 2217 DS
structure
Queues to
implement Library book request
1.Enter Book details
2.Remove Book Details
3.Display
4.Exit
Enter your choice(1-4) :
2
Removed book details are..
101 2217 DS structure
Queues to
implement Library book request
1.Enter Book details
2.Remove Book Details
3.Display
4.Exit
Enter your choice(1-4) :
4
/* Convert infix expression to
postfix,prefix format*/
import
java.util.*;
class obj
{
char
pst[] = new char[15];
char
inf[] = new char[15];
char
oprt[] = new char[15];
char
ods[] = new char[20];
int
i,n, top = 0,ptop=0, dsp = 0;
String s;
void
pushd(char x)
{
ods[dsp++]
= x;
}
char popd()
{
return(ods[--dsp]);
}
void prefix()
{
char a;
System.out.println( "Enter Expression in infix format : ");
Scanner in
= new Scanner(System.in);
s =
in.nextLine();
n =
s.length();
for(i=0;i<n;i++)
inf[i] =
s.charAt(i);
System.out.print("\n THE PREFIX FORM IS : ");
for(i=n;i>=0;i--)
{
a = inf[i];
if (a
!= '+' && a != '-' && a != '*' && a != '/'
&& a != '^' && a != '(' && a != ')')
pushd(a);
if (a
== '+' || a == '-' || a == '*' || a == '/')
System.out.print(a);
if(a
== '(')
continue;
if(a
== ')')
continue;
}
while(dsp
!= 0)
System.out.print(popd());
System.out.println("\n");
}
void push(char
x)
{
oprt[top++] = x;
}
char
pop()
{
return(oprt[--top]);
}
void insr(char
x)
{
ptop =
ptop + 1;
pst[ptop] = x;
}
void
display(int n)
{
for(i=0;i<n+1;i++)
System.out.print(pst[i]);
}
void postfix()
{
Scanner in = new Scanner(System.in);
System.out.print( "Enter Expression in infix format : ");
s =
in.nextLine();
n =
s.length();
for(i=0;i<n;i++)
inf[i]
= s.charAt(i);
i = 0;
while
(i < n)
{
if
(inf[i] != '+' && inf[i] != '-' && inf[i] != '*'
&& inf[i] != '/' && inf[i] != '^' && inf[i] !=
'('
&& inf[i] != ')')
insr(inf[i]);
else
{
if (inf[i] == '+' || inf[i] == '-' || inf[i] == '*'
|| inf[i] == '/')
push(inf[i]);
else
{
if(inf[i]=='(')
push(inf[i]);
else
{
if(inf[i]==')')
{
while(top!= 0 && oprt[top-1]!='(')
insr(pop());
pop();
}
}
}
}
i++;
}
while(top
!= 0)
insr(pop());
System.out.print("\n POSTFIX FORM IS : ");
display(n);
System.out.println("\n\n");
}
}
class infprepost
{
public static void
main(String args[])
{
Scanner in = new Scanner(System.in);
obj P = new obj();
int ch;
do
{
System.out.println("\n1. INFIX TO PREFIX");
System.out.println("2. INFIX TO POSTFIX");
System.out.println("3. EXIT");
System.out.print("Enter Your Choice : ");
ch =
in.nextInt();
switch(ch)
{
case 1 : P.prefix();
break;
case 2 : P.postfix();
break;
case 3 :
System.out.println("This is the End of the Program");
break;
default :
System.out.println("Invalid Choice ? ");
}
}
while(ch != 3);
}
}
Output:
C:\Program Files\Java\jdk1.6.0_13\bin>javac
infprepost.java
C:\Program Files\Java\jdk1.6.0_13\bin>java infprepost
1. INFIX TO PREFIX
2. INFIX TO POSTFIX
3. EXIT
Enter Your Choice : 1
Enter Expression in infix format :
a*b+c/d
THE PREFIX FORM IS :
/+*abcd
1. INFIX TO PREFIX
2. INFIX TO POSTFIX
3. EXIT
Enter Your Choice : 2
Enter Expression in infix format : a*b+c/d
POSTFIX FORM IS
: abcd/+*
1. INFIX TO PREFIX
2. INFIX TO POSTFIX
3. EXIT
Enter Your Choice : 3
This is the End of the Program
/* Heap tree traversal*/
#include <iostream.h>
#include <conio.h>
void cheap(int t[20],int n)
{
int i,j,key, q;
for(q=2;q<=n;++q)
{ i = q;
key = t[q];
j = (int) (i/2);
while((i>1) && (key > t[j]))
{
t[i] = t[j];
i = j;
j = (int) (i/2);
if (j < 1)
j = i;
} t[i] = key;
} }
void heap(int
t[20],int n)
{
int q, temp, i,j,
key;
cheap(t,n);
for(q=n;q >= 2;
--q)
{
temp = t[1];
t[1] = t[q];
t[q] = temp;
i = 1;
key = t[1];
j = 2;
if((j+1) < q)
if(t[j+1] >
t[j])
j++;
while(j <=
(q-1) && (t[j] > key))
{
t[i] = t[j];
i = j;
j = 2 * i;
if (j+1 < q)
if (t[j+1]
> t[j])
j++;
else
if (j > n)
j = n;
t[i] =
key;
} } }
void main(void)
{ int tree[20],i,n;
clrscr();
cout <<
"Enter the Number of Elements \n";
cin >>n;
cout <<
"Enter the Numbers \n";
for(i=1;i<=n;i++)
cin >>
tree[i];
heap(tree,n);
cout <<
"\n\n Heap Tree ";
cout<<"\n-------------------";
cout<<"\nAscending Order: ";
for(i=1;i<=n;i++)
{cout <<
"---> " << tree[i];}
cout<<"\n\nDecending Order: ";
for(i=n;i>=1;i--)
{cout <<
"---> " << tree[i];}
getch();
}
Output:
Enter the Number of Elements
5
Enter the Numbers
23
1
58
96
4
Heap Tree
-------------------
Ascending Order: ---> 1---> 4---> 23--->
58---> 96
Decending Order: ---> 96---> 58---> 23--->
4---> 1
DOUBLE STACK:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class ds
{
private:
int d[11],l,r,e;
public:
ds()
{ l=r=5; }
void PUSHA()
{
++r;
if(r>10)
{
cout<<"DS Full-right side";
return;
}
cout<<"Enter element(int) to insert";
cin>>e;
d[r]=e;
}
void PUSHB()
{
--l;
if(l<1)
{
cout<<"DS
Full left side";
return;
}
cout<<
"Enter element(int) to insert";
cin>>e;
d[l]=e;
}
void POPA()
{
if(r==5)
{
cout<<"DS Empty right side";
return;
}
e=d[r];--r;
cout<<"The deleted element is"<<e;
}
void POPB()
{
if(l==5)
{
cout<<"DS Empty left side";
return;
}
e=d[l];++l;
cout<<"The deleted element is"<<e;
}
void display()
{
cout<<"Elements in DS are\n";
for(int
i=l;i<=r;++i)
cout<<d[i]<<" ";
}
};
void main()
{
int o;
ds odq;
clrscr();
do
{
cout<<"\n1.PUSHA Right\n";
cout<<"2.PUSHB Left\n";
cout<<"3.POPA Right\n";
cout<<"4.POPB Left\n";
cout<<"5.Display\n";
cout<<"6.Exit\n";
cout<<"Enter your choice
";
cin>>o;
switch(o)
{
case 1:
odq.PUSHA();
break;
case 2:
odq.PUSHB();
break;
case 3:
odq.POPA();
break;
case 4:
odq.POPB();
break;
case 5:
odq.display();
break;
case 6:
exit(1);
break;
default:
cout<<"Illegal choice";
}
}while(1);
}
OUTPUT:
1.PUSHA Right
2.PUSHB Left
3.POPA Right
4.POPB Left
5.Display
6.Exit
Enter your choice 1
Enter element(int) to insert 10
1.PUSHA Right
2.PUSHB Left
3.POPA Right
4.POPB Left
5.Display
6.Exit
Enter your choice 2
Enter element(int) to insert 20
1.PUSHA Right
2.PUSHB Left
3.POPA Right
4.POPB Left
5.Display
6.Exit
Enter your choice 5
Elements in DS are
20 0 10
1.PUSHA Right
2.PUSHB Left
3.POPA Right
4.POPB Left
5.Display
6.Exit
Enter your choice 3
The deleted element is 10
1.PUSHA Right
2.PUSHB Left
3.POPA Right
4.POPB Left
5.Display
6.Exit
Enter your choice 5
Elements in DS are
20 0
// implementation of double queue using linked lists
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class dq
{ private:
int d[11],l,r,e;
public:
dq() { l=r=5; }
void insertright()
{ ++r;
if(r>10)
{cout<<"DQ Full-right side"; return;}
cout<<"Enter element(int) to insert";
cin>>e;
d[r]=e;
}
void insertleft()
{ --l;
if(l<1)
{cout<<"DQ Full left side"; return;}
cout<<
"Enter element(int) to insert";
cin>>e;
d[l]=e;
}
void delright()
{
if(r==5)
{cout<<"DQ Empty right side"; return;}
e=d[r];--r;
cout<<"The deleted element is"<<e;
}
void delleft()
{ if(l==5) {cout<<"DQ Empty left
side"; return;}
e=d[l];++l;
cout<<"The deleted element is"<<e;
}
void display()
{
cout<<"Elements in DQ are\n";
for(int
i=l;i<=r;++i)
cout<<d[i]<<" ";
}
};
void main()
{ int o;
dq odq;
clrscr();
do{
cout<<"\n1.Insert Right\n";
cout<<"2.Insert Left\n";
cout<<"3.Delete
Right\n";
cout<<"4.Delete Left\n";
cout<<"5.Display\n";
cout<<"6.Exit\n";
cout<<"Enter your choice
";
cin>>o;
switch(o)
{ case 1:
odq.insertright();break;
case 2:
odq.insertleft();break;
case 3:
odq.delright();break;
case 4:
odq.delleft();break;
case 5:
odq.display();break;
case 6:
exit(1);break;
default:
cout<<"Illegal choice";
}
}while(1);
}
Output :
1.Insert Right
2.Insert Left
3.Delete Right
4.Delete Left
5.Display
6.Exit
Enter your choice 1
Enter element(int) to insert 25
1.Insert Right
2.Insert Left
3.Delete Right
4.Delete Left
5.Display
6.Exit
Enter your choice 2
Enter element(int) to insert 45
1.Insert Right
2.Insert Left
3.Delete Right
4.Delete Left
5.Display
6.Exit
Enter your choice 5
Elements in DQ are
45 0 25
1.Insert Right
2.Insert Left
3.Delete Right
4.Delete Left
5.Display
6.Exit
Enter your choice 3
The deleted element is25
1.Insert Right
2.Insert Left
3.Delete Right
4.Delete Left
5.Display
6.Exit
Enter your choice 6
// Department binary tree
#include<iostream.h>
// Exno= 13
#include<stdlib.h>
#include <string.h>
#include<conio.h>
struct college
{
char dept[20];
int status;
int i;
}bt[10];
int n,i,c,o,l,r;
char ch,op;
char dept[20];
void createt()
{
i=1;ch='y' || 'Y';
cout<<"\nEnter Root Node : ";
cin >>
bt[i].dept;
bt[i].status = 0;
do
{
cout <<
"\nIs there Any Left Node (Y/N)? : ";
cin >> op;
if (op == 'y' || 'Y')
{
l = 2 * i;
cout <<
"\nEnter the " <<l<< " Left Node : ";
cin >>
bt[l].dept;
bt[l].status = 0;
}
cout <<
"\nIs there Any Right Node (Y/N)? : ";
cin >> op;
if (op == 'y' || 'Y')
{
r = 2 * i + 1;
cout <<
"\nEnter the " <<r<< " Right Node : ";
cin >>
bt[r].dept;
bt[r].status = 0;
}
c = i;
cout <<
"\nDo you Want to Continue (Y/N) ? : ";
cin >> ch;
i++;
}
while(ch == 'y' || ch
=='Y');
}
void insertt()
{
n = c * 2 + 1;
int x;
cout <<
"\nEnter the New Department to Add in the TREE / COLLEGE\n";
cin >>dept;
i = 1;
do
{
x
=strcmp((char*)bt[i].dept,(char *)dept);
if (x != 0)
{
strcpy(bt[n+1].dept,dept);
bt[n+1].status =
0;
}
else
{
cout <<
"\nThis Department is Already Exists\n";
break;
}
i++;
}while(i<=n);
n = i+1;
}
void deletet()
{
n = c * 2 +1;
i = 1;
int x;
cout <<
"\nEnter the Department Name to Delete in the TREE / COLLEGE\n";
cin >>dept;
do
{
x
=strcmp((char*)bt[i].dept,(char *)dept);
if (x == 0)
{
bt[i].status = 1;
break;
}
i++;
}while(i<=n);
}
void display()
{
n = c * 2 + 2;
i =0;
cout <<
"\nThe List of Department names in the College\n";
while(i<=n)
{
if (bt[i].status ==
0)
cout << "\t" << bt[i].dept <<endl;
i++;
}}
void main()
{ clrscr();
createt();
do{
cout<<"\n1.Insert the Department into the College tree";
cout<<"\n2.Delete the Department from College tree";
cout<<"\n3.Display all the Department in the College
tree";
cout<<"\n4.Exit";
cout<<"\nEnter your option (1-4) ?";
cin>>o;
switch(o)
{ case 1:insertt();break;
case
2:deletet();break;
case
3:display();break;
case
4:exit(0);
}
}while(1);
}
Output:
Enter Root Node :
jbict
Is there Any Left Node (Y/N)? : y
Enter the 2 Left Node : mba
Is there Any Right Node (Y/N)? : y
Enter the 3 Right Node : mca
Do you Want to Continue (Y/N) ? : n
1.Insert the Department into the College tree
2.Delete the Department from College tree
3.Display all the Department in the College tree
4.Exit
Enter your option (1-4) ? 1
Enter the New Department to Add in the TREE / COLLEGE
b.tech
1.Insert the Department into the College tree
2.Delete the Department from College tree
3.Display all the Department in the College tree
4.Exit
Enter your option (1-4) ? 3
The List of Department names in the College
jbict
mba
mca
b.tech
1.Insert the Department into the College tree
2.Delete the Department from College tree
3.Display all the Department in the College tree
4.Exit
Enter your option (1-4) ? 2
Enter the Department Name to Delete in the TREE / COLLEGE
mba
1.Insert the Department into the College tree
2.Delete the Department from College tree
3.Display all the Department in the College tree
4.Exit
// Priority queue implementation on airline reservation
#include <iostream.h>
#include <conio.h>
struct pass
{
char name[20];
long double p;
};
void main(void)
{
pass q[10], temp;
int i,j,n;
long mile;
int years,seq;
long double pri;
clrscr();
cout <<
"\nEnter How many Passengers? : ";
cin >> n;
for(i=1;i<=n;i++)
{
cout <<
"\nEnter the "<< i
<< " Passenger Name : ";
cin >>
q[i].name;
cout <<
"\nEnter the "<< i
<< " Mileage : ";
cin >> mile;
cout <<
"\nEnter the "<< i
<< " Years : ";
cin >> years;
cout <<
"\nEnter the "<< i
<< " Sequence : ";
cin >> seq;
pri = mile / 1000 +
years - seq;
q[i].p = pri;
}
for(i=1;i<=n;i++)
for(j=1;j<n;j++)
{
if (q[j].p >
q[j+1].p)
{
temp = q[j];
q[j] = q[j+1];
q[j+1] = temp;
}
}
cout << "\n\nPassenger Name ||" ;
cout
<<"Waiting Customer - Priority \n";
cout <<
"--------------------------";
cout<<"----------------------------------";
for(i=1;i<=n;i++)
cout <<
endl<<"\t"<<q[i].name <<"\t\t"<<
q[i].p<< endl;
getch();
}
Output :
Enter How many Passengers? : 3
Enter the 1 Passenger
Name : a
Enter the 1 Mileage :
2000
Enter the 1 Years : 5
Enter the 1 Sequence
: 1
Enter the 2 Passenger
Name : b
Enter the 2 Mileage :
300
Enter the 2 Years :
10
Enter the 2 Sequence
: 2
Enter the 3 Passenger
Name : c
Enter the 3 Mileage :
900
Enter the 3 Years : 3
Enter the 3 Sequence
: 3
Passenger Name ||Waiting Customer - Priority
--------------------- ---------------------------------------
c 0
a 6
b 8
// Library Database Using Linked Lists
#include <iostream.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
int i=0;
struct link
{
int acno;
char title[20];
int cop;
int status;
struct link *next;
};
struct link *item, *temp, *first;
struct link2
{
int bno;
int acno;
char bname[20];
char doi[20];
char dor[20];
int status;
struct link2 *next;
};
struct link2 *item2, *temp2, *first2;
int ch;
library();
issue();
libsearch();
borrow();
retun();
void main(void)
{
first = NULL;
first2 = NULL;
pos: clrscr();
gotoxy(15,4);cout<< "
1. CREATING LIBRARY BOOKS ";
gotoxy(15,5);cout<< "
2. ISSUING LIBRARY BOOKS ";
gotoxy(15,6);cout<<
" 3. RETURN LIBRARY BOOKS ";
gotoxy(15,7);cout<< "
4. BORROWER DETAILS ";
gotoxy(15,8);cout<< "
5. LIBRARY SEARCH ";
gotoxy(15,9);cout<< "
6. Exit ";
gotoxy(15,10);cout<< "Enter Your Choice: " ;
cin>> ch;
switch(ch)
{
case 1 : library();
break;
case 2 : issue();
break;
case 3 : retun();
break;
case 4 : borrow();
break;
case 5 :
libsearch();
break;
case 6 : exit(0);
break;
default : {
gotoxy(10,19);
cout<< "Your choice is out of Range ";
goto pos;
}
}
goto pos;
}
library()
{
if (first == NULL)
{
item = new link;
first = item;
}
else
{
temp = new link;
item->next =
temp;
item = temp;
}
cout<<endl<<"Enter the Book Accession Number:";
cin >>
item->acno;
cout<<endl<<"Enter the Book Title : ";
cin >>
item->title;
cout<<endl<<"Enter the No. of Copies of the
Book:";
cin >>
item->cop;
item->status = 0;
item->next =
NULL;
return(0);
}
libsearch()
{
int an;
item = first;
cout<<"Enter the Book Accession
Number to Search:";
cin >> an;
do
{
if(item->acno ==
an && item->status == 0)
{
cout<<"\n"<<"The Accession number of the Book
="<<item->acno<<"\n";
cout<<"\n"<<"The Title of the Book = "
<<item->title<<"\n";
cout<<"\n"<<"The Number of copies of the book
= "<<item->cop<<"\n";
break;
}
item =
item->next;
}while(item != NULL);
if (item == NULL)
cout <<
"Book Not Found ";
getch();
return(0);
}
issue()
{
int assn;
item = first;
cout<<"Enter the Book Accession
Number to Issue :";
cin >> assn;
do
{
if(item->acno ==
assn)
{
i++;
if (first2 == NULL)
{
item2 = new
link2;
first2 = item2;
}
else
{
temp2 = new link2;
item2->next =
temp2;
item2 = temp2;
}
item2->acno = assn;
item->cop =
item->cop - 1;
cout<<endl<<"Enter the Borrower A/C Number =
";
cin >>
item2->bno;
cout<<endl<<"Enter the Borrower Name =
";
cin >>
item2->bname;
cout<<endl<<"Enter the Date of Issue = ";
cin >>
item2->doi;
item2->status =
0;
if ( i > 5)
item->status = 1;
item2->next =
NULL;
break;
}
item =
item->next;
}while(item != NULL);
if (item == NULL)
cout <<
"Book Not Found ";
getch();
return(0);
}
retun()
{
int assn;
item = first;
item2 = first2;
cout<<endl<<"Enter
the Book Accession Number to Return :";
cin >> assn;
do
{
if(item->acno ==
assn)
{
do
{
if(item2->acno ==
assn)
{
i--;
cout<<endl<<"The Borrower Name =
" <<item2->bname;
cout<<endl<<"Enter Borrower A/C
Number="<<item2->bno;
cout<<endl<<"Enter
the Date of Return = ";
cin >>
item2->dor;
item2->status =
1;
item->cop =
item->cop + 1;
if ( i < 5)
item->status = 0;
break;
}
item2 =
item2->next;
}while(item2 !=
NULL);
if (item2 == NULL)
cout <<
"Book Not Found ";
}
item = item->next;
}while(item != NULL);
getch();
return(0);
}
borrow()
{
item2 = first2;
while(item2 != NULL)
{
if(item2->status
== 0)
{
cout<<"\n"<<"The Borrower Number for the Book
= "<<item2->bno<<"\n";
cout<<"\n"<<"The Accession number of the Book
= "<<item2->acno<<"\n";
cout<<"\n"<<"The Borrwer Name for the Book =
"<<item2->bname<<"\n";
cout<<"\n"<<"The Date of Issue =
"<<item2->doi<<"\n";
}
item2 =
item2->next;
}
cout<<endl<<"The Number of
Books in his/her A/c = "<<i<<"\n";
getch();
return(0);
}
Output:
1.
CREATING LIBRARY BOOKS
2.
ISSUING LIBRARY BOOKS
3.
RETURN LIBRARY BOOKS
4.
BORROWER DETAILS
5.
LIBRARY SEARCH
6.
Exit
Enter
Your Choice: 1
Enter the Book Accession Number: 105
Enter the Book Title:
FS
Enter the No. of Copies of the Book: 50
1.
CREATING LIBRARY BOOKS
2.
ISSUING LIBRARY BOOKS
3.
RETURN LIBRARY BOOKS
4.
BORROWER DETAILS
5.
LIBRARY SEARCH
6.
Exit
Enter
Your Choice: 2
Enter the Book Accession Number to Issue: 105
Enter the Borrower A/C Number = 101
Enter the Borrower Name = Harun
Enter the Date of Issue = 30-06-2011
1.
CREATING LIBRARY BOOKS
2.
ISSUING LIBRARY BOOKS
3.
RETURN LIBRARY BOOKS
4.
BORROWER DETAILS
5.
LIBRARY SEARCH
6. Exit
Enter
Your Choice: 3
Enter the Book Accession Number to Return: 105
The Borrower Name = Harun
Enter Borrower A/C Number =101
Enter the Date of Return = 1-07-2011
1.
CREATING LIBRARY BOOKS
2. ISSUING LIBRARY BOOKS
3.
RETURN LIBRARY BOOKS
4.
BORROWER DETAILS
5.
LIBRARY SEARCH
6.
Exit
Enter
Your Choice: 4
The Borrower Number for the Book = 101
The Accession number of the Book = 105
The Borrower Name for the Book = Harun
The Date of Issue = 30-06-2011
The Number of Books in his/her A/c = 1
1.
CREATING LIBRARY BOOKS
2.
ISSUING LIBRARY BOOKS
3.
RETURN LIBRARY BOOKS
4.
BORROWER DETAILS
5.
LIBRARY SEARCH
6.
Exit
Enter
Your Choice: 5
Enter the Book Accession Number to Search: 105
The Accession number of the Book =105
The Title of the Book = FS
The Number of copies of the book = 50
// print groceries shop information using linked list
#include<iostream.h>
#include<conio.h>
class node
{
public:
int itemcode,stock;
char itemname[20];
float price,vat;
node *next;
};
class grocery
{
node *x,*y,*start;
float amount;
public:
grocery()
{
x=NULL;
y=NULL;
start=NULL;
amount=0;
}
void create();
void display();
void search();
};
void grocery::create()
{
x=new node;
cout<<"\n";
cout<<"enter
itemcode"<<"\n";
cin>>x->itemcode;
cout<<"enter item
name"<<"\n";
cin>>x->itemname;
cout<<"enter stock"<<"\n";
cin>>x->stock;
cout<<"enter price"<<"\n";
cin>>x->price;
cout<<"enter vat"<<"\n";
cin>>x->vat;
amount=amount+((x->stock*x->price)+(x->stock*(x->price*x->vat/100)));
x->next=NULL;
start=x;
y=x;
cout<<"enter 1 to enter another node";
int ch;
cin>>ch;
while(ch==1)
{
x=new node;
cout<<"\n";
cout<<"enter
itemcode"<<"\n";
cin>>x->itemcode;
cout<<"enter item
name"<<"\n";
cin>>x->itemname;
cout<<"enter stock"<<"\n";
cin>>x->stock;
cout<<"enter price"<<"\n";
cin>>x->price;
cout<<"enter vat"<<"\n";
cin>>x->vat;
amount=amount+((x->stock*x->price)+(x->stock*(x->price*x->vat/100)));
x->next=NULL;
y->next=x;
y=x;
cout<<"enter 1 to enter another node";
cin>>ch;
}
}
void grocery::display()
{
node *temp;
temp=new node;
for(temp=start;temp!=NULL;temp=temp->next)
{
cout<<"\n"<<"\n";
cout<<"ITEM CODE
::"<<temp->itemcode<<"\n";
cout<<"ITEM
NAME::"<<temp->itemname<<"\n";
cout<<"STOCK::"<<temp->stock<<"\n";
cout<<"PRICE::"<<temp->price<<"\n";
cout<<"VAT::"<<temp->vat<<"\n";
}
cout<<"********************"<<"\n";
cout<<"total amount is::"<<amount<<”\n”;
cout<<"********************"<<"\n";
}
void grocery::search()
{
int code;
node *temp;
cout<<"enter item code to search";
cin>>code;
for(temp=start;temp!=NULL;temp=temp->next)
{
if(temp->itemcode==code)
{
cout<<"\n"<<"\n";
cout<<"ITEM CODE
::"<<temp->itemcode<<"\n";
cout<<"ITEM
NAME::"<<temp->itemname<<"\n";
cout<<"STOCK::"<<temp->stock<<"\n";
cout<<"PRICE::"<<temp->price<<"\n";
cout<<"VAT::"<<temp->vat<<"\n";
}
}
}
void main()
{
clrscr();
grocery g1;
int ch;
do
{
cout<<"\n"<<" MAHESH GROCERIS
SHOP"<<"\n"<<"\n";
cout<<"1.Create item
information"<<"\n";
cout<<"2.Display item
information"<<"\n";
cout<<"3.search item"<<"\n";
cout<<"4.Exit"<<"\n";
cout<<"enter your choice";
cin>>ch;
switch(ch)
{
case 1:g1.create();break;
case 2:g1.display();break;
case 3:g1.search();break;
case 4:break;
}
}while(ch!=4);
}
Output:
MAHESH GROCERIS SHOP
1.Create item information
2.Display item information
3.search item
4.Exit
enter your choice 1
enter itemcode
1001
enter item name
hamam
enter stock
100
enter price
20
enter vat
3.50
enter 1 to enter another node 1
enter itemcode
1002
enter item name
colgate
enter stock
1000
enter price
15
enter vat
2.05
enter 1 to enter another node 2
MAHESH GROCERIS SHOP
1.Create item information
2.Display item information
3.search item
4.Exit
enter your choice 2
ITEM CODE ::1001
ITEM NAME::hamam
STOCK::100
PRICE::20
VAT::3.5
ITEM CODE ::1002
ITEM NAME::colgate
STOCK::1000
PRICE::15
VAT::2.05
**********************
total amount
is::17377.5
**********************
MAHESH GROCERIS SHOP
1.Create item information
2.Display item information
3.search item
4.Exit
enter your choice 3
enter item code to search1002
ITEM CODE ::1002
ITEM NAME::colgate
STOCK::1000
PRICE::15
VAT::2.05
MAHESH GROCERIS SHOP
1.Create item information
2.Display item information
3.search item
4.Exit
enter your choice 4
//program for AVL tree operations using c++
#include<iostream.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
friend
class AVL;
private:
int
data;
AVLNODE
*left,*right;
int
bf;
};
class AVL
{
private:
AVLNODE
*root;
public:
AVLNODE
*loc,*par;
AVL()
{
root=NULL;
}
int
insert(int);
void
displayitem();
void
display(AVLNODE *);
void
removeitem(int);
void
remove1(AVLNODE *,AVLNODE *,int);
void
remove2(AVLNODE *,AVLNODE *,int);
void
search(int x);
void
search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
AVLNODE
*a,*b,*c,*f,*p,*q,*y,*clchild,*crchild;
int
found,unbalanced;
int d;
if(!root) //special case empty tree
{ y=new AVLNODE;
y->data=x;
root=y;
root->bf=0;
root->left=root->right=NULL;
return
TRUE; }
//phase
1:locate insertion point for x.a keeps track of the most
// recent
node with balance factor +/-1,and f is the parent of a
// q
follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{ //search for insertion point
for x
if(p->bf)
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else
if(x>p->data)
{
q=p;
p=p->right;
}
else
{
y=p;
found=TRUE;
}
} //end while
//phase
2:insert and rebalance.x is not in the tree and
// may be
inserted as the appropriate child of q.
if(!found)
{
y
= new AVLNODE;
y->data=x;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust
balance factors of nodes on path from a to q
//note
that by the definition of a,all nodes on this
//path
must have balance factors of 0 and so will change
//to
+/- d=+1 implies that x is inserted in the left
//
subtree of a d=-1 implies
//to
that x inserted in the right subtree of a.
if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;
d=1;
}
while(p!=y)
if(x>p->data) //height of right increases by 1
{
p->bf=-1;
p=p->right;
}
else //height of left increases by 1
{
p->bf=1;
p=p->left;
}
//is
tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{ //tree still balanced
a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation
type
{
if(d==1)
{ //left imbalance
if(b->bf==1) //rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;
c->left=b;
c->right=a;
switch(c->bf)
{
case
1: a->bf=-1; //LR(b)
b->bf=0;
break;
case
-1:b->bf=1; //LR(c)
a->bf=0;
break;
case
0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
} //end
of left imbalance
else
//right imbalance
{
if(b->bf==-1) //rotation type RR
{
a->right=b->left;
b->left=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case
1: a->bf=-1; //LR(b)
b->bf=0;
break;
case
-1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c;
//b is the new root
}
//end of LR
}
//subtree with root b has been rebalanced and is the new
subtree
if(!f)
root=b;
else
if(a==f->left)
f->left=b;
else
if(a==f->right)
f->right=b;
}
//end of if unbalanced
return
TRUE;
}
//end of if(!found)
return FALSE;
}
//end of AVL INSERTION
void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
{
if(temp==NULL)
return;
cout<<temp->data<<"
";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem
is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE
*ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
root=l;
suc->left=l->left;
suc->right=l->right;
return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
AVLNODE
*child;
if(s->left==NULL
&& s->right==NULL)
child=NULL;
else
if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;
}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE
*ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe
tree is empty";
return;
}
if(temp->data==x)
{
cout<<"\nthe
item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return; }
if( x <
temp->data)
{
ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;
save=temp;
}
while(ptr!=NULL)
{
if(x==ptr->data)
{ flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;
}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item
is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}
main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter
u r choice to perform on AVL tree";
cin>>c;
switch(c)
{
case
1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;
case
2:a.displayitem(); break;
case
3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);
break;
case
4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case
5:exit(0); break;
default :cout<<"\nInvalid
option try again";
}
cout<<"\ndo
u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}
Output:
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 1
Enter an element to insert into tree 20
do u want to continue
y
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 1
Enter an element to insert into tree 1
do u want to continue
y
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 2
20 1
do u want to continue
y
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 4
Enter an element to search
1
itemfound
do u want to continue
y
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 3
Enter an item to deletion
1
itemfound
do u want to continue
y
1.insert
2.display
3.delete
4.search
5.exit
Enter u r choice to perform on AVL tree 5
// double linked list
in c++
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
#define new(p) p=(struct node*) malloc(sizeof(struct node))
#define read(p) cin>>p
class dll
{ private:
struct node
{ int info;
struct node*
right,*left;
}*h,*p,*t,*k,*l;
public:
dll()
{ h=l=0;}
void create()
{
cout<<"Enter elements at end give 0
";
new(p);h=p;
read(p->info);
while(p->info!=0)
{ new(k);
read(k->info);
p->right=k;
k->left=p;
p=k;
}
l=p;
l->right=0;
deletelast();
}
void insertfirst()
{
cout<<"Enter value to insert";
new(t);
cin>>t->info;
t->right=h;
h->left=t;
h=t;
}
void insertlast()
{ t=h;
while(t->right!=0) t=t->right;
new(p);
cout<<"Enter value to insert";
cin>>p->info;
t->right=p;
p->left=t;
p->right=0;
}
void insertmiddle()
{ int e;
new(t);
cout<<"Enter
value to insert";
cin>>t->info;
cout<<"Enter after which node you want to insert";
cin>>e;
p=h;
while(p->info!=e)
{ if(p==0)
{cout<<"Element not found"; return;}
p=p->right;
}
t->right=p->right;
p->right->left=t;
p->right=t;
t->left=p;
}
void insert()
{ int o;
do
{
cout<<"1.insertfirst\n";
cout<<"2.insertlast\n";
cout<<"3.insertmiddle\n";
cout<<"4.Exit";
cout<<"Enter your option";
cin>>o;
switch(o)
{ case 1:
insertfirst();break;
case
2: insertlast();break;
case
3: insertmiddle();break;
case
4: return;
}
}while(1);
}
void deletefirst()
{ p=h->right;
free(h);
h=p;
}
void deletelast()
{ p=h; while(p->right!=0) p=p->right;
t=h; while(t->right!=p) t=t->right;
free(p);
t->right=0;
}
void deletemiddle()
{ int e;
cout<<"enter node to be deleted";
cin>>e;
p=h;
while(p->info!=e)
{ if(p==0)
cout<<"Element not found";
p=p->right;
}
t=h;
while(t->right!=p) t=t->right;
t->right=p->right;
p->right->left=t;
free(p);
}
void del()
{ int o;
do
{
cout<<"1.Delete First\n";
cout<<"2.Delete Last\n";
cout<<"3.Delete Middle\n";
cout<<"4.Exit";
cout<<"Enter your option";
cin>>o;
switch(o)
{ case 1:
deletefirst();break;
case
2: deletelast();break;
case
3: deletemiddle();break;
case
4: return;
}
}while(1);
}
void disp()
{
cout<<"Elements in the list are ";
p=h;
while(p!=0)
{
cout<<p->info<<" ";
p=p->right;
} } }
Void main()
{
dll l;
int o;
clrscr();
l.create();
do
{
cout<<"\n";
cout<<"
1.Insertion\n";
cout<<"
2.Deletion\n";
cout<<"
3.Display\n";
cout<<"
4.Exit\n";
cout<<" Enter your
option:";
cin>>o;
switch(o)
{
case 1:
l.insert();break;
case 2:
l.del();break;
case 3:
l.disp();break;
case 4: exit(1);
}
}while(1);
}
Output:
Enter elements at end give 0 10
23
56
89
0
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your option: 1
1.insertfirst
2.insertlast
3.insertmiddle
4.Exit
Enter your option 1
Enter value to insert 20
1.insertfirst
2.insertlast
3.insertmiddle
4.Exit
Enter your option 2
Enter value to insert 50
1.insertfirst
2.insertlast
3.insertmiddle
4.Exit
Enter your option 4
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your option: 3
Elements in the list are
20 10 23 56 89 50
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your option: 2
1.Delete First
2.Delete Last
3.Delete Middle
4.Exit
Enter your option 1
1.Delete First
2.Delete Last
3.Delete Middle
4.ExitEnter your option 4
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your option: 3
Elements in the list are
10 23 56 89 50
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your option: 4
//Binary search trees
using arrays
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int bst[50],e,r,p,pa;
void createt()
{
cout<<"Enter elements at end give
0\n";
up1:r=1;
cin>>e;
if(e==0) return;
up2:if(bst[r]==0)
{bst[r]=e; goto up1;}
if(bst[r]>e)
{r=2*r; goto up2;}
if(bst[r]<e)
{r=2*r+1; goto up2;}
}
int search(int e)
{
r=1;
up3: if(bst[r]==0) {return(0);}
if(bst[r]==e)
{return(1);}
if(bst[r]>e) {pa=r;r=2*r; goto up3;}
if(bst[r]<e)
{pa=r;r=2*r+1; goto up3;}
}
void searcht()
{cout<<"enter element to search";
cin>>e;
if(search(e))
cout<<"Element found";
else
cout<<"Element not found";
}
void insertt()
{cout<<"Enter element to insert";
cin>>e;
if(search(e))
{cout<<"Element already exists"; return;}
else
{if(e>bst[pa])
bst[2*pa+1]=e;
else bst[2*pa]=e;
}
cout<<"The
element is inserted successfully";
}
void swap(int a,int b)
{int t=a;a=b;b=t;}
void delet(int e)
{ if(search(e))
{if((bst[2*r]==0)&&(bst[2*r+1]==0)) {bst[r]=0;return;}
if(bst[2*r]==0)
{bst[2*pa+1]=bst[2*r+1];bst[2*r+1]=0;return;}
if(bst[2*r+1]==0){bst[2*pa]=bst[2*r];bst[2*r]=0;return;}
p=2*r+1;
while(bst[2*p]!=0)
p=2*p;
swap(bst[r],bst[p]);
delet(bst[p-1]);
}
else
{cout<<"Element not existing";return;}
}
void deletet()
{cout<<"Enter the element you want to
delete";
cin>>e;
delet(e);
}
void preorder(int r)
{if(bst[r]==0) return;
cout<<"
"<<bst[r];
preorder(2*r);
preorder(2*r+1);
}
void inorder(int r)
{ if(bst[r]==0) return;
inorder(2*r);
cout<<"
"<<bst[r];
inorder(2*r+1);
}
void postorder(int r)
{ if(bst[r]==0) return;
postorder(2*r);
postorder(2*r+1);
cout<<"
"<<bst[r];
}
void displayt()
{
int o;
do{ r=1;
cout<<"\n1.preorder";
cout<<"\n2.inorder";
cout<<"\n3.postorder";
cout<<"\n4.return";
cout<<"\n5.Enter your option";
cin>>o;
switch(o)
{ case 1:
preorder(r);break;
case 2:
inorder(r);break;
case 3:
postorder(r);break;
case 4: return;
}
}while(1);
}
void main()
{
int o;
clrscr();
createt();
do{
cout<<"\n1.Insert an element into tree";
cout<<"\n2.Delete an element from tree";
cout<<"\n3.Display elements in tree";
cout<<"\n4.Search for an element in tree";
cout<<"\n5.Exit";
cout<<"\nEnter your option";
cin>>o;
switch(o)
{ case
1:insertt();break;
case
2:deletet();break;
case
3:displayt();break;
case
4:searcht();break;
case 5:exit(1);
}
}while(1);
}
Output:
Enter elements at end give 0
1
2
3
4
0
1.Insert an element into tree
2.Delete an element from tree
3.Display elements in tree
4.Search for an element in tree
5.Exit
Enter your option 3
1.preorder
2.inorder
3.postorder
4.return
5.Enter your option 3
4 3 2 1
1.preorder
2.inorder
3.postorder
4.return
5.Enter your option 4
1.Insert an element into tree
2.Delete an element from tree
3.Display elements in tree
4.Search for an element in tree
5.Exit
Enter your option 4
enter element to search 4
Element found
1.Insert an element into tree
2.Delete an element from tree
3.Display elements in tree
4.Search for an element in tree
5.Exit
Enter your option
1
Enter element to insert 10
The element is inserted successfully
1.Insert an element into tree
2.Delete an element from tree
3.Display elements in tree
4.Search for an element in tree
5.Exit
Enter your option 2
Enter the element you want to delete 4
1.Insert an element into tree
2.Delete an element from tree
3.Display elements in tree
4.Search for an element in tree
5.Exit
Enter your option 5
// quick sort in java
import java.util.*;
class ss
{
int
temp,left,right,p,i,n;
int[] a = new
int[100];
Scanner in
= new Scanner(System.in);
public
void reading(int n)
{
for(i=0;i<n;i++)
{
System.out.print("Enter Element of at
a[" + i +
"] : ");
a[i] =
in.nextInt();
}
}
public void sort(int
beg, int end)
{
if( beg < end)
{
left = beg;
right = end;
p = a[beg];
do
{
while((a[left] <= p) && (left
< end))
left = left + 1;
while((a[right] >= p) && (right
> beg))
right = right - 1;
if(left < right)
{
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
else
break;
}while(left < right);
a[beg] = a[right];
a[right] = p;
sort(beg, right-1);
sort(right+1, end);
}
}
public void display(int n)
{
System.out.println("The Resultant Sorted Array is : ");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
class Quick
{
public static void main(String args[])
{
ss obj = new ss();
Scanner in = new
Scanner(System.in);
int n;
System.out.print("Enter the Array Size : ");
n = in.nextInt();
obj.reading(n);
obj.sort(0,n-1);
obj.display(n);
}
}
Output:
C:\Program Files\Java\jdk1.6.0_13\bin>javac Quick.java
C:\Program Files\Java\jdk1.6.0_13\bin>java Quick
Enter the Array Size
: 5
Enter Element of at a[0]
: 12
Enter Element of at a[1]
: 6
Enter Element of at a[2]
: 4
Enter Element of at a[3]
: 89
Enter Element of at a[4]
: 3
The Resultant Sorted Array is :
3
4
6
12
89
//BFS using c++
#define SIZE 20
#define TRUE 1
#define FALSE 0
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int g[SIZE][SIZE];
int visit[SIZE];
int q[SIZE],front,rear,n;
void main()
{
int v1,v2;
char ans='y';
void create();
void bfs(int);
clrscr();
create();
clrscr();
cout<<"\n the adjacency matrix for the
graph";
for(v1=0;v1<n;v1++)
{
cout<<"\n";
for(v2=0;v2<n;v2++)
cout<<"\t"<<g[v1][v2];
}
getch();
do
{
for(v1=0;v1<n;v1++)
visit[v1]=FALSE;
clrscr();
cout<<"\n enter the vertex from which you want to
traverse";
cin>>v1;
if(v1>=n)
cout<<"\n invalid vertex";
else
{
cout<<"\n Breadth first Search of a graph
is...........";
bfs(v1);
getch();
}
cout<<"\n do u want traverse from any other
node";
cin>>ans;
}while(ans=='y');
}
void create()
{
int v1,v2;
char ans='y';
cout<<"\n\t\t this program is to create a
graph";
cout<<"\n enter no of nodes";
cin>>n;
for(v1=0;v1<n;v1++)
for(v2=0;v2<n;v2++)
g[v1][v2]=FALSE;
cout<<"\n enter the vertex number starting from
0";
do
{
cout<<"\n enter the vertices v1 and v2";
cin>>v1>>v2;
if(v1>=n||v2>=n)
cout<<"\n invalid vertex value";
else
{
g[v1][v2]=TRUE;
g[v2][v1]=TRUE;
}
cout<<"\n add more edges(Y/N)";
cin>>ans;
}while(ans=='y');
}
void bfs(int v1)
{
int v2;
visit[v1]=TRUE;
front=rear=-1;
q[++rear]=v1;
while(front!=rear)
{
v1=q[++front];
cout<<"\n"<<v1;
for(v2=0;v2<n;v2++)
if(g[v1][v2]==TRUE && visit[v2]==FALSE)
{
q[++rear]=v2;
visit[v2]=TRUE;
}
}
}
Output:
this
program is to create a graph
enter no of nodes 4
enter the vertex
number starting from 0
enter the vertices v1
and v2 0 1
add more edges(Y/N) y
enter the vertices v1
and v2 0 3
add more edges(Y/N) y
enter the vertices v1
and v2 3 2
add more edges(Y/N) n
the adjacency matrix
for the graph
0 1
0 1
1 0
0 0
0 0
0 1
1 0
1 0
enter the vertex from
which you want to traverse 0
Breadth first Search
of a graph is...........
0
1
3
2
do u want traverse from any other node y
enter the vertex from
which you want to traverse 1
Breadth first Search
of a graph is...........
1
0
3
2
do u want traverse
from any other node n
//DFS using c++
#define SIZE 20
#define TRUE 1
#define FALSE 0
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int g[SIZE][SIZE];
int visit[SIZE];
int n;
void main()
{
int v1,v2;
char ans='y';
void create();
void dfs(int);
clrscr();
create();
clrscr();
cout<<"\n the adjacency matrix for the
graph";
for(v1=0;v1<n;v1++)
{
cout<<"\n";
for(v2=0;v2<n;v2++)
cout<<"\t"<<g[v1][v2];
}
getch();
do
{
for(v1=0;v1<n;v1++)
visit[v1]=FALSE;
clrscr();
cout<<"\n enter the vertex from which you want to
traverse";
cin>>v1;
if(v1>=SIZE)
cout<<"\n invalid vertex";
else
{
cout<<"\n Deapth first Search of a graph
is...........";
dfs(v1);
getch();
}
cout<<"\n do u want traverse from any other
node";
cin>>ans;
}while(ans=='y');
}
void create()
{
int v1,v2,ch,flag;
char ans='y';
cout<<"\n\t\t this program is to create a
graph";
flushall();
for(v1=0;v1<n;v1++)
for(v2=0;v2<n;v2++)
g[v1][v2]=FALSE;
cout<<"\n enter no of nodes";
cin>>n;
cout<<"\n
the vertex number starting from 0";
do
{
cout<<"\n enter the vertices v1 and v2";
cin>>v1>>v2;
if(v1>=n||v2>=n)
cout<<"\n invalid vertex value";
else
{
g[v1][v2]=TRUE;
g[v2][v1]=TRUE;
}
cout<<"\n add more edges(Y/N)";
cin>>ans;
}while(ans=='y');
}
void dfs(int v1)
{
int v2;
cout<<"\n"<<v1;
visit[v1]=TRUE;
for(v2=0;v2<SIZE;v2++)
if(g[v1][v2]==TRUE && visit[v2]==FALSE)
dfs(v2);
}
Output:
this
program is to create a graph
enter no of nodes 4
the vertex number
starting from 0
enter the vertices v1
and v2 0 1
add more edges(Y/N) y
enter the vertices v1
and v2 0 3
add more edges(Y/N) n
the adjacency matrix
for the graph
0 1
0 1
1 0
0 0
0 0
0 0
1 0
0 0
enter the vertex from
which you want to traverse 0
Deapth first Search
of a graph is...........
0
1
3
do u want traverse
from any other node y
enter the vertex from
which you want to traverse 1
Deapth first Search
of a graph is...........
1
0
3
do u want traverse
from any other node n
//PRIMS ALGORITHM
#include<iostream.h>
#include<conio.h>
#define SIZE 20
#define INF 32767
void prims(int g[][SIZE],int nodes)
{
int select[SIZE],i,j,k,min_dist,v1,v2,tot=0;
for(i=0;i<nodes;i++)
select[i]=0;
cout<<"\n\n minimum spanning tree
is...........";
select[0]=1;
cout<<"\n\n source vertex \t destination vertex
\t weight";
for(k=1;k<nodes;k++)
{
min_dist=INF;
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
if(g[i][j]&&((select[j])||(!select[j])))
{
if(g[i][j]<min_dist)
{
min_dist=g[i][j];
v1=i;v2=j;
}
}
}
}
cout<<"\t"<<v1<<"\t\t"<<v2<<"\t\t"<<min_dist<<"\n";
select[v1]=select[v2]=1;
tot=tot+min_dist;
cout<<"\n\n\t Total path length is ::
"<<tot;
}
}
void main()
{
int g[SIZE][SIZE],nodes,v1,v2,length,i,j,n;
clrscr();
cout<<"\n\tprims algorithm \n enter no of nodes
in the graph";
cin>>nodes;
cout<,"\n enter no of edges in the graph;
cin>>n;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
g[i][j]=0;
cout<<"\n enter edges and weights";
for(i=0;i<n;i++)
{
cout<<"\n enter edge by v1 and v2";
cin>>v1>>v2;
cout<<"\n enter weights";
cin>>length;
g[v1][v2]=g[v2][v1]=length;
}
getch();
cout<<"\n\t";
clrscr();
prims(g,nodes);
getch();
}
OUTPUT:
prims
algorithm
enter no of nodes in
the graph 5
enter no of edges in
the graph 6
enter edges and
weights
enter edge by v1 and
v2 0 1
enter weights 2
enter edge by v1 and
v2 0 4
enter weights 4
enter edge by v1 and
v2 1 4
enter weights 1
enter edge by v1 and
v2 1 2
enter weights 4
enter edge by v1 and v2 0 2
enter weights 3
enter edge by v1 and v2 0 4
enter weights 4
minimum spanning tree
is...........
source vertex destination vertex weight 1 4
1
Total path
length is :: 1 1 4 1
Total path length is :: 2 1 4 1
Total path
length is :: 3 1 4 1
Total path
length is :: 4
/* program
for towers
of Honai */
#include<iostream.h>
#include<conio.h>
int n;
char a,b,c;
void move(int n, char
ta,char tb,char tc)
{
if(n>=1)
{
move(n-1,ta,tc,tb);
cout<<ta<<"---->"<<tb;
cout<<endl;
move(n-1,tc,tb,ta);}
}
main()
{
clrscr();
cout<<"Enter n";
cin>>n;
move(n,'a','b','c');
getch();
}
Output:
Enter n 4
a---->c
a---->b
c---->b
a---->c
b---->a
b---->c
a---->c
a---->b
c---->b
c---->a
b---->a
c---->b
a---->c
a---->b
c---->b
/*
program for Newton’s
equation */
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#define f(x) (x*x-2)
#define d(x) (2*x)
void main()
{
int istep;
double a,b,x0,dx;
double dl=0.0000001;
clrscr();
dx=b-a;
x0=a-(f(a)/d(x0));
istep=0;
while(fabs(dx)>dl)
{
dx=f(x0)/d(x0);
cout<<"xo="<<x0<<"\n";
x0-=dx;
istep++;
}
cout<<the square root
algorithm"<<"\n";
cout<<"Newton's
method converged in
<<istep<<"steps"<<"\n";
cout<<"the square root is
"<<"\n";
printf("%16.241f\n",x0);
cout<<"\n";
getch();
}
Output:
xo=1.033333
xo=1.484409
xo=1.415873
xo=1.414215
xo=1.414214
the square root algorithm
Newton's
method converged in 5steps
the square root is
1.414214
/*
program for merge sort */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
int a[20],b[20],i,n,j,m,s=0;
class customer
{
public:
int cnum,accno,bal;
char cname[20],branch[20];
void getdata();
void input();
void sort();
void display();
};
void customer::getdata()
{
cout<<"\n enter customer number:";
cin>>cnum;
cout<<"\n enter customer name:";
cin>>cname;
cout<<"\n enter account number:";
cin>>accno;
cout<<"\n enter initial balance:";
cin>>bal;
cout<<"\n enter branch name:";
cin>>branch;
}
void customer::display()
{
cout<<"\n"<<cnum<<"\t"<<cname<<"\t"<<accno<<"\t"<<bal<<"\t"<<branch<<endl;
}
void customer::sort()
{
customer bk,s1;
fstream bank1,bsort;
bank1.open("bank.dat",ios::in);
for(i=0;i<s;i++)
{
bank1.read((char*)&bk,sizeof(bk));
a[i]=bk.cnum;
b[i]=i;
}
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>=a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
int t1=b[i];
b[i]=b[j];
b[j]=t1;
}
}
}
bank1.close();
bank1.open("bank.dat",ios::in);
bsort.open("bsort.dat",ios::out|ios::trunc);
cout<<"\n\n all customer details of the
bank";
cout<<"\n number\tname\tnumber\tbalance\tbranch";
cout<<"\n
----------------------------------------------";
for(i=0;i<s;i++)
{
bank1.seekg(b[i]*sizeof(s1),ios::beg);
bank1.read((char*)&bk,sizeof(bk));
bsort<<"\n"<<bk.cnum<<"\t"<<bk.cname<<"\t"<<bk.accno<<"\t"<<bk.bal<<"\t"<<bk.branch<<endl;
bk.display();
}
bank1.close();
bsort.close();
}
void customer::input()
{
customer b1,b2;
fstream b1out,b2out,bank;
cout<<"\n enter branch1 customer details";
cout<<"\n enter how many customer you want to
add:";
cin>>n;
b1out.open("branch1.dat",ios::out);
bank.open("bank.dat",ios::out);
for(i=0;i<n;i++)
{
b1.getdata();
bank.write((char*)&b1,sizeof(b1));
b1out<<b1.cnum<<"\t"<<b1.cname<<"\t"<<b1.accno<<"\t"<<b1.bal<<"\t"<<b1.branch<<endl;
}
bank.close();
bank.open("bank.dat",ios::app);
cout<<"\n enter branch2 customer details";
cout<<"\n enter how many customer you want to
add:";
cin>>m;
s=m+n;
b2out.open("branch2.dat",ios::out|ios::trunc);
for(i=0;i<m;i++)
{
b2.getdata();
bank.write((char*)&b2,sizeof(b2));
b2out<<b2.cnum<<"\t"<<b2.cname<<"\t"<<b2.accno<<"\t"<<b2.bal<<"\t"<<b2.branch<<endl;
}
b1out.close();
b2out.close();
bank.close();
}
void main()
{
clrscr();
customer c;
c.input();
c.sort();
getch();
}
Output:
enter branch1
customer details
enter how many
customer you want to add:1
enter customer
number: 101
enter customer
name:honey
enter account number:
1001
enter initial
balance: 5000
enter branch name:
tirupati
enter branch2
customer details
enter how many
customer you want to add: 1
enter customer
number: 201
enter customer name:
venki
enter account number:
501
enter initial
balance: 3000
enter branch name:
chittoor
all customer details
of the bank
number name number
balance branch
----------------------------------------------
201 venki 501
3000 chittoor
101 honey 1001
5000 irupati
/*
java package for sorting techniques */
import java.io.*;
public class sortings
{
public static void main(String args[])throws IOException
{
int n,i,j,temp,count;
int arr[]=new int[10];
do
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\n\n\t\t\t sorting
techniques");
System.out.println("\t\t\t_____________________________");
System.out.println("1.Bubble Sort");
System.out.println("2.Insertion Sort");
System.out.println("3.Selection Sort");
System.out.println("4.Shell Sort");
System.out.println("\n Enter your choice");
n=Integer.parseInt(br.readLine());
switch(n)
{
case 1: System.out.println("Enter the data of 5
Elements:");
for(i=0;i<5;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
count=0;j=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
count++;
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("\n Complexity of Bubble sort:"+count);
System.out.println("\n
Bubble sort order.........");
for(i=1;i<=5;i++)
{
System.out.println(arr[i]);
}
break;
case 2:System.out.println("\n Enter the data of 5
Elements:");
for(i=0;i<5;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
count=0;
for(i=1;i<5;i++)
{
for(j=0;j<i;j++)
{
count++;
if(arr[j]>arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("\n Complexity
of Insertion sort:"+count);
System.out.println("\n insert sort order......");
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
break;
case 3:System.out.println("Enter the data of 5
Elements:");
for(i=0;i<5;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
count=0;j=0;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
count++;
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("\n
Complexity of Selection sort:"+count);
System.out.print("\n select sort order....");
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
break;
case 4: System.out.println("\n Enter the data of 5
Elements");
for(i=0;i<5;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
int k,m,mid;
count=0;
for(m=5/2;m>0;m/=2)
{
for(j=m;j<5;j++)
{
for(i=j-m;i>=0;i-=m)
{
count++;
if(arr[i+m]>=arr[i])
break;
else
{
mid=arr[i];
arr[i]=arr[i+m];
arr[i+m]=mid;
}
}
}
}
System.out.println("\n Complexity of Shell sort:"+count);
System.out.println("\n Shell sort order.....");
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
break;
}
}while(n<=4);
}
}
Output:
C:\Program Files\Java\jdk1.6.0_13\bin>javac sortings.java
C:\Program Files\Java\jdk1.6.0_13\bin>java sortings
sorting techniques
_____________________________
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
Enter your choice
1
Enter the data of 5 Elements:
1
9
4
6
2
Complexity of Bubble
sort:25
Bubble sort
order.........
1
2
4
6
9
sorting techniques
_____________________________
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
Enter your choice
2
Enter the data of 5
Elements:
5
8
1
3
4
Complexity of Insertion sort:10
insert sort
order......
1
3
4
5
8
sorting techniques
_____________________________
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
Enter your choice
3
Enter the data of 5 Elements:
7
2
9
4
1
Complexity of
Selection sort:10
select sort
order....1
2
4
7
9
sorting techniques
____________________________
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
Enter your choice
4
Enter the data of 5
Elements
2
9
1
8
6
Complexity of Shell
sort:10
Shell sort order.....
1
2
6
8
9
sorting techniques
_____________________________
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
Enter your choice
5