Ec8381 FDS Lab Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

EC8381-FUNDAMENTALSOFDATASTRUCTURESI

NC LABORATORY

OBJCTI
VES
 Tounder standandimpl
ementbasicdatast r
uctur
esusingC
 Toappl yli
nearandnon-
li
neardat
astructuresinprobl
em sol
ving.
 To l
earnt oi mpl
ementfuncti
ons and recursivefuncti
on bymeans ofdat
a
st
ruct
ures
 Toimplementsearchi
ngandsorti
ngalgori
thms

LI
STOFEXPERI
MENTS

1.Basi
cCPr
ogr
ams–l
oopi
ng,
dat
amani
pul
ati
ons,
arr
ays

2.Pr
ogr
amsusi
ngst
ri
ngs–st
ri
ngf
unct
ioni
mpl
ement
ati
on

3.Pr
ogr
amsusi
ngst
ruct
uresandpoi
nter
s

4.Pr
ogr
amsi
nvol
vi
ngdy
nami
cmemor
yal
l
ocat
ions

5.Ar
rayi
mpl
ement
ati
onofst
acksandqueues

6.Li
nkedl
i
sti
mpl
ement
ati
onofst
acksandqueues

7.Appl
i
cat
ionofSt
acksandQueues

8.I
mpl
ement
ati
onofTr
ees,
TreeTr
aver
sal
s

9.I
mpl
ement
ati
onofBi
nar
ySear
cht
rees

10.I
mpl
ement
ati
onofLi
nearsear
chandbi
nar
ysear
ch

OUTCOMES
 Towr
it
ebasi
candadv
ancedpr
ogr
amsi
nc.
 Toi
mpl
ementf
unct
ionsandr
ecur
siv
efunct
ionsi
nc.
 Toi
mpl
ementdat
ast
ruct
uresusi
ngc.
 Tochooseappropr
iat
esor
ti
ngal
gor
it
hm f
oranappl
i
cat
ionandi
mpl
ementi
tina
modular
izedway.
EXNO:
1ai SUM OFNNUMBERS
AI
M:
Towr
it
eCpr
ogr
am t
ofi
ndsum ofnnumber
susi
ngf
orl
oop
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Readt
hev
alueofn.
3.I
nit
ial
i
zesum=0andi
=1.
4.Cal
cul
atesum=sum+i
5I
ncr
ementi
by1.
6.i
fi<=n,
thengot
ost
ep7el
segot
ost
ep4.
7.Pr
intsum
8.St
op
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
i
ntn,
i,sum =0;
cl
rscr
();
pr
int
f("
\nEnt
eranyVal
ue\
n")
;
scanf
("%d"
,&n)
;
f
or(
i=1;
i<=n;
i++)
{
sum =sum +i
;
}
pr
int
f("
Sum ofnNumber
s=%d"
,sum)
;
get
ch(
);
}
OUTPUT:
Ent
eranyv
alue3
Sum ofnnumber
s=6
Ent
eranyv
alue5
Sum ofnnumber
s=15
RESULT:
Thus,Cpr
ogr
am t
ofi
ndsum ofnnumber
sisi
mpl
ement
edandexecut
ed
successful
ly
EXNO:
1ai
i AMSTRONGNUMBERS
AI
M:
Towrit
eaCpr
ogr
am t
ofi
ndwhet
heragi
vennumberi
sAr
mst
rongnumberornot
usi
ngwhi
l
eloop.
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Readt
hev
alueofn.
3.Assi
gnt
hev
alueofnt
o't
emp'
ands=0.
4.Repeatunt
iln!=0
r
em=t
emp%10;
sum=sum +(
rem *r
em *r
em)
;
t
emp=t
emp/
10
5.i
f(t
emp==n)
,thenpr
intgi
vennumberi
sAr
mst
rong,
elsepr
intnotAr
mst
rong
6.St
opt
hepr
ogr
am
PROGRAM
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
i
ntn,
temp,
rem,
num=0;
cl
rscr
();
pr
int
f("
Ent
eraposi
ti
vei
nteger
:")
;
scanf
("%d"
,&n)
;
t
emp=n;
whi
l
e(n!
=0)
{
r
em=n%10;
num=num+r
em*
rem*
rem;
n=n/
10;
}
i
f(
num==t
emp)
pr
int
f("
%di
sanAr
mst
rongnumber
."
,num)
;
el
se
pr
int
f("
%di
snotanAr
mst
rongnumber
."
,t
emp)
;
get
ch(
);
}
OUTPUT
Ent
eraposi
ti
vei
nteger
:153
153i
sanAr
mst
rongnumber
.
Ent
eraposi
ti
vei
nteger
:101
101i
snotanAr
mst
rongnumber
.

RESULT:
Thus,Cprogram tof
indwhetheragivennumberisAr
mst
rongnumberornotusi
ng
whi
l
eloopisimplementedandexecutedsuccessf
ull
y.
EXNO:
1ai
i
i SUM OFDI
GITS

AI
M:
Towr
it
eaCpr
ogr
am t
ofi
ndsum ofdi
git
susi
ngdowhi
l
eloop
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Readt
hev
alueofnandi
nit
ial
i
zat
ionsum =0
3.Assi
gnt
hev
alueofnt
otemp
4.Cal
cul
atet
hef
oll
owi
ng:
r
em=n%10
sum=sum+r
em
n=n/
10
5.Repeatunt
iln!=0
6.Pr
intt
her
esul
tforsum.
7.St
opt
hepr
ogr
am
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
i
ntn,
rem,
sum=0,
temp;
cl
rscr
();
pr
int
f("
Ent
eraposi
ti
vei
nteger
:")
;
scanf
("%d"
,&n)
;
t
emp=n;
do
{
r
em=n%10;
sum=sum+r
em;
n=n/
10;
}
whi
l
e(n!
=0)
;
pr
int
f("Sum ofDi
git
sis%d.
",
sum)
;
get
ch(
);
}

OUTPUT:
Ent
eraposi
ti
vei
nteger
:12345
Sum ofDi
git
sis15

RESULT:
Thustheprogram t
ofi
ndsum ofdi
git
sfort
hegi
vennumberi
simpl
ement
edand
execut
edsuccessf
ull
y.
EXNO:
1b DATAMANI
PULATI
ON
AI
M:
Towr
it
eaCpr
ogr
am f
ordat
amani
pul
ati
onsusi
ngbi
twi
seoper
ator
.
ALGORI
THM:
1St
artt
hepr
ogr
am
2.Decl
aret
hev
ari
abl
es
3.Uset
her
equi
redbi
twi
seoper
ator
sfort
hei
nputgi
ven
4.Di
spl
ayt
her
esul
t
5.St
opt
hepr
ogr
am
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
unsi
gnedi
nta,
b;
i
ntc=0;
cl
rscr
();
pr
int
f("
Ent
ert
hev
auesofaandb"
);
scanf
("%d%d"
,&a,
&b)
;
c=a&b;
pr
int
f("
\nBI
TWI
SEAND=%d\
n",
c);
c=a|
b;
pr
int
f("
\nBI
TWI
SEOR=%d\
n",
c);
c=a^b;
pr
int
f("
\nBI
TWI
SEXOR=%d\
n",
c);
c=~a;
pr
int
f("
One'
scompl
enetofa=%d\
n",
c);
c=a<<2;
pr
int
f("
Lif
tshi
ftby2bi
ts=%d\
n",
c);
c=a>>2;
pr
int
f("
Rightshi
ftby2bi
ts=%d\
n",
c);
get
ch(
);
}

OUTPUT:
Ent
ert
hev
aluesofaandb6013
BI
TWI
SEAND=12
BI
TWI
SEOR=61
BI
TWI
.SEXOR=49
One’
scompl
ementofa=-
61
Li
ftshi
ftby2=240
Ri
ghtshi
ftby2bi
ts=15

RESULT:
Thus,Cprogram f
ordat
amani
pul
ati
onusi
ngbi
twi
seoper
atori
simpl
ement
edand
execut
edsuccessf
ully
.
EXNO:
2 FI
NDMI
NIMUM ANDMAXI
MUM I
NARRAY
AI
M:
Towr
it
eCpr
ogr
am t
ofi
ndmaxi
mum andmi
nimum el
ementi
nanar
ray
.
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Readt
hev
alueofn.
3.Repeatf
ori
=0t
on.
4.Readt
hei
nputv
aluest
oar
raya[
i]andi
ncr
ementi
by1
5.Assi
gnmax=a[
0],
min=a[
0]
6.Repeatf
ori
=1t
on.
7.Checkwhet
hert
hev
alueofa[
i]i
sgr
eat
ert
hanmax
8.I
fst
ep7i
str
ue,
assi
gnmax=a[
i]
9.I
fst
ep7i
sfal
se,
checkt
hev
alueofa[
i]i
slesst
hemi
n
10.
Ifst
ep9i
str
ue,
assi
gnmi
n=a[
i]
.
11.
Pri
ntt
hev
aluesofmaxandmi
n
12.
Stopt
hepr
ogr
am
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
i
nti
,
n;
i
nta[
50]
,
max
,mi
n;
cl
rscr
():
pr
int
f("
Ent
ert
heel
ement
sinar
ray
");
scanf
("%d"
,&n)
;
f
or(
i=0;
i
<n;
i
++)
scanf
("%d"
,a[
i]
);
max=a[
0];
mi
n=a[
0];
f
or(
i=1;
i
<n;
i
++)
{
i
f(a[
i]
>max)
max=a[
i]
;
el
se
i
f(
a[i
]<mi
n)
mi
n=a[
i]
;
}
pr
int
f("
Lar
gestel
ementi
nar
rayi
s%d\
n",
max)
;
pr
int
f("
Smal
l
estel
ementi
nar
rayi
s%d\
n",
min)
;
get
ch(
);
}

OUTPUT:
Ent
ert
heel
ement
sinar
ray102030-
8-1
Lar
gestel
ementi
nar
rayi
s20
Smal
l
estel
ementi
nar
rayi
s-8
RESULT:
Thus,Cprogram t
ofindmaxi
mum andmi
nimum el
ementi
nar
rayi
simpl
ement
ed
andexecutedsuccessf
ull
y.

EXNO:
3 STRI
NGHANDLI
NGFUNCTI
ON
AI
M:
Towr
it
eaCpr
ogr
am t
oil
l
ust
rat
etheconceptofst
ri
nghandl
i
ngf
unct
ions.
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Gett
wost
ri
ngsasi
nput
.
3.Dot
hef
oll
owi
ng
 Uset
hest
ri
ngcompar
efunct
ionf
orchecki
ngt
het
wost
ri
ngsar
eequal
.
 Uset
hest
ri
ngconcat
enat
ionf
unct
ionf
orj
oini
ngf
ir
stst
ri
ngwi
thsecondst
ri
ng.
 Fi
ndt
hel
engt
hoft
hest
ri
ngsusi
ngt
hest
rl
enf
unct
ion.
 Pr
intt
her
ever
seofast
ri
ngusi
ngst
rrevf
unct
ion.
 Showt
heuppercaseandl
owercaseofast
ri
ngusi
ngst
ruprandst
rl
wrf
unct
ions.
 Usest
ri
ngcopyf
unct
iont
ocopysour
cest
ri
ngi
ntodest
inat
ionst
ri
ng
4.St
opt
hepr
ogr
am.
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<st
ri
ng.
h>
v
oidmai
n()
{
chars1[
20]
,s2[
20]
,s3[
20]
;i
ntx,
l1,
l2,
l3;
cl
rscr
();
pr
int
f("
\n\
nEnt
ert
wost
ri
ngconst
ant
s:\
n")
;
scanf
("%s%s"
,s1,
s2)
;
x=st
rcmp(
s1,
s2)
;
i
f(
x!=0)
{
pr
int
f("
\n\
nSt
ri
ngsar
enotequal
\n"
);
}
el
se
pr
int
f("
\n\
nSt
ri
ngsar
eequal
\n"
);
st
rcat
(s1,
s2)
;
st
rcpy
(s3,
s1)
;
l
1=st
rl
en(
s1)
;
l
2=st
rl
en(
s2)
;
l
3=st
rl
en(
s3)
;
pr
int
f("
\ns1=%s\
tlengt
h=%dchar
act
ers\
n",
s1,
l1)
;
pr
int
f("
s2=%s\
tlengt
h=%dchar
act
ers\
n",
s2,
l2)
;
pr
int
f("
s3=%s\
tlengt
h=%dchar
act
ers\
n",
s3,
l
3);
pr
int
f("
Rev
erseofS1i
s%s\
n",
str
rev
(s1)
);
pr
int
f("
Upper
caseofS2i
s%s\
n",
str
upr
(s2)
);
pr
int
f("
Lower
caseofS2i
s%s\
n",
str
lwr
(s2)
);
get
ch(
);
}

OUTPUT:
Ent
ert
wost
ri
ngconst
ant
s:
hai
wel
come
St
ri
ngsar
enotequal
s1=hai
wel
comel
engt
h=10char
act
ers
s2=wel
comel
engt
h=7char
act
ers
s3=hai
wel
comel
engt
h=10char
act
ers
Rev
erseofS1i
semocl
ewi
ah
Upper
caseofS2i
sWELCOME
Lower
caseofS2i
swel
come
RESULT:
Thus,
Cpr
ogr
am f
orst
ri
nghandl
i
ngf
unct
ionsi
simpl
ement
edandexecut
edsuccessf
ull
y

EXNO:
4 SALARYSLI
POFEMPLOYEES
AI
M
Towr
iteCpr
ogram t
ogener
atesal
arysl
i
pofempl
oyeesusi
ngst
ruct
ureandpoi
nter
.
Al
gor
it
hm:
1.Star
t
2.Reademploy
eesdet
ail
se1,
empi
d,name,
basi
c,hr
a,da,
ma,
pf,
insur
ance,
gross,
net
3.e1=(
str
uctempl
oyee*
)mal
l
oc(
sizeof
(st
ructempl
oyee)*num)
;
4.Pr
inti
nputf
orev
eryempl
oyee
5.FOR(
i=0;
i<num;
i++)
6.Cal
cul
ategr
ossamount
7.Cal
cul
atenetamount
t
8.ENDFOR
9.St
op
Pr
ogr
am:
#include<stdio.
h>
#include<stdli
b.h>
#include<stri
ng.h>
#include<conio.h>
structempl oyee
{
i
ntempi d;
charname[ 32];
i
ntbasic,hra, da,ma;
i
ntpf,i
nsur ance;
fl
oatgross, net;
};
voidpr i
ntSalary
(st ructempl oy
eee1)
{
pri
ntf(
"Salar yslipof%s: \n"
,e1.name) ;
pri
ntf(
"Empl oyeeI D:%d\n",
e1.empi d);
pri
ntf(
"Basi csal ary:%d\n",
e1.basi c);
pri
ntf(
"Houser entall
owance: %d\ n",
e1.hra)
;
pri
ntf(
"Dear nessal l
owance: %d\ n",e1.
da);
pri
ntf(
"Medi cal al
l
owance: %d\ n",e1.ma) ;
pri
ntf(
"Grosssalary:%.2fRupees\n",
e1.gr
oss)
;
pri
ntf(
"Deducti
ons: \
n");
pri
ntf(
"Provi
dedf und:%d\n"
,e1.
pf);
pri
ntf(
"I
nsurance: %d\n",
e1.
insur
ance);
pri
ntf(
"netsal
ary:%.2fRupees\n\n",
e1.net
);
ret
urn;
}
voi
dmai n()
{
i
nti ,
ch,num, fl
ag,empi d;
structempl oyee* e1;
printf
("Ent ert henumberofempl oy ees: ")
;
scanf("%d" ,
&num) ;
e1=(structempl oy ee* )mal loc( sizeof (structempl oyee)*num);
printf
("entery ourinputf orev er yempl oy ee:\n");
for(i
=0;i<num; i++)
{
pr i
nt f (
"empl oy eei d:");
scanf (
"%d" ,
&(e1[ i
].empi d));
get char ()
;
pr i
nt f (
"empl oy eename: ");
f get s(e1[i]
.name, 32, st din) ;
e1[ i].name[ strlen( e1[ i].
name) -
1] ='\0';
pr i
nt f (
"basicsal ary ,
HRA: ");
scanf (
"%d%d" ,
&( e1[ i].
basi c) ,&(e1[ i
].hra));
pr i
nt f (
"DA, medi cal allowance: ");
scanf (
"%d%d" ,
&( e1[ i].
da) ,&( e1[i].
ma) );
pr i
nt f (
"PFandi nsur ance: "
) ;
scanf (
"%d%d" ,
&( e1[ i].
pf ),
&( e1[ i
].insur ance));
}
for(i
=0;i<num; i++)
{
e1[ i].gross=e1[ i
].basi c+( e1[ i]
.hra*e1[ i
].
basic)/
100+(e1[
i]
.da*
e1[
i]
.basic)/100+( e1[ i
].
ma*e1[ i
].basi c)/ 100;
e1[ i].net=e1[ i
].
gr oss- ( e1[ i]
.pf +e1[ i]
.insurance);
}
whi l
e(1)
{
pr i
nt f (
"enterempl oy eeI Dt ogetpay sli
p:"
);
scanf (
"%d" ,
&empi d);
f l
ag=0;
f or(i=0;i<num; i++)
{
if
(empi d==e1[ i].empi d)
{
pr i
nt Sal ar y(e1[ i]
);
flag=1;
}
}
if(!flag)
pr i
nt f (
"nor ecor df ound! !\n") ;
pr i
nt f (
"Doy ouwantt ocont i
nue( 1/ 0) :
")
;
scanf (
"%d" ,
&ch) ;
if(!ch)
{
break;
}
}
get
ch(
);
}

OUTPUT:

RESULT:
Thus,Cprogram t
ocal
cul
atesal
arysl
i
pofempl
oyeesusi
ngst
ruct
ureandpoi
nter
s
wasexecutedsuccessf
ull
y
EXNO: 5 DYNAMICMEMORYALLOCATION
AI
M:
Towr i
teaCpr
ogr
am t
oext
endt
hememor
yspaceofanar
rayusi
ngdy
nami
cmemor
y
al
locat
ion.
ALGORI
THM:
1.St
artt
hepr
ogr
am.
2.Decl
arepoi
nterv
ari
abl
e*pt
r,
n1&n2.
3.Readt
hesi
zeofanar
rayn1.
4.Al
l
ocat
ememor
yforanar
rayusi
ngmal
l
oc(
)funct
ion.
5.Readt
heel
ement
sforar
ray
.
6.Readnewsi
zeofanar
rayn2usi
ngr
eal
l
oc(
)funct
ion.
7.Readr
emai
ningel
ement
sfornewl
yal
l
ocat
edar
ray
.
8.Pr
intt
heel
ement
sinar
ray
.
9.Apoi
nterv
ari
abl
eispassedt
ofr
ee(
)funct
ionwhi
chdeal
l
ocat
ethememor
y.
10.
Stopt
hepr
ogr
am.

PROGRAM
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
i
ntmai
n()
{
i
nt*
ptr
,i,
n1,
n2;
pr
int
f("
Ent
ersi
zeofar
ray
:")
;
scanf
("%d"
,&n1)
;
pt
r=(
int
*)mal
l
oc(
n1*si
zeof
(i
nt)
);
pr
int
f("
Ent
ert
heel
ement
s:"
);
f
or(
i=0;
i<n1;
++i
)
scanf
("%d"
,pt
r+i
);
pr
int
f("
\nEnt
ernewsi
zeofar
ray
:")
;
scanf
("%d"
,&n2)
;
pt
r=r
eal
l
oc(
ptr
,n2)
;
pr
int
f("
Ent
ert
her
emai
ningel
ement
sofanar
ray
\n"
);
f
or(
i=n1;
i
<n2;
++i
)
scanf
("%d"
,pt
r+i
);
pr
int
f("
Element
sofar
rayar
e:\
n")
;
f
or(
i=0;
i<n2;
++i
)
pr
int
f("
%d\
t",
*(pt
r+i
))
;
f
ree(
ptr
);
r
etur
n0;
}

OUTPUT:
Ent
ert
hesi
zeofanar
ray:
5
Ent
ert
heel
ement
s:
10
20
30
40
50
Ent
ery
thenewsi
zeofanar
ray
:3
Ent
ert
her
emai
ningel
ement
sofanar
ray
:
60
70
80
El
ement
sinar
rayar
e:
10
20
30
40
50
60
70
80

RESULT:
Thus,
Cprogr
am toext
endthememoryspaceofanar
rayusi
ngdy
nami
cmemor
y
al
l
ocati
oni
sexecut
edsuccessf
ull
y.

EXNO:
6a STACKSUSI
NGARRAYI
MPLEMENTATI
ON
AI
M:
Towr
it
eaCpr
ogr
am t
oimpl
ementst
ackoper
ati
onsusi
ngar
ray
.
ALGORI
THM:

1.Start
2.Readt hear raystacksi zeofn.
3.I
niti
ali
zet op=-1
4.Displ
ayamenul isti
ngst ackoperat
ionsusi
ngswi
tchst
atement
.
5.Readt hechoi ce
6.I
fchoice=1t henpusht heelements
I
ft op>=n- 1
I
ncr ementt op
Storeel ementatcur r
entposi
ti
onoftop
El
se
PrintStackov erflow
7.I
fchoice=2t hen, popt heelement
I
ft op<=- 1then
PrintStackunder fl
ow
El
se
Displaycur r
entt opelement
Decr ementt op
8.I
fchoice=3t hen,
Displaystackel ement sstar
ti
ngfrom t
op
9.Stop

PROGRAM:
#incl
ude<stdio.h>
i
ntstack[100],choi
ce,
n,
top,
x,
i;
voidpush(void);
voidpop(void);
v
oi ddispl ay (void) ;
i
ntmai n( )
{
 
  
 //clr
scr (
);
 
  
 top=- 1;
 
  
 printf("\nEnt ert hesi zeofSTACK[ MAX=100] :
");
 
  
 scanf ("%d" ,
&n) ;
 
  
 printf("\n\tSTACKOPERATI ONSUSI NGARRAY" );
 
  
 printf("\n\t -
--
----
--
---
--
----
--
--
---
---
--
--
");
 
  
 printf("\n\t1. PUSH\ n\t2.POP\ n\t3.DISPLAY\n\t4.
EXIT"
);
 
  
 do
 
  
 {
 
  
  
   
print f(
"\ nEnt ertheChoi ce:"
);
 
  
  
   
scanf ("%d" ,
&choi ce);
 
  
  
   
swi tch( choi ce)
 
  
  
   
{
 
  
  
   
   
 case1:
 
  
  
   
   
 {
 
  
  
   
   
  
   push( );
 
  
  
   
   
  
   break;
 
  
  
   
   
 }
 
  
  
   
   
 case2:
 
  
  
   
   
 {
 
  
  
   
   
  
   pop( );
 
  
  
   
   
  
   break;
 
  
  
   
   
 }
 
  
  
   
   
 case3:
 
  
  
   
   
 {
 
  
  
   
   
  
   display ();
 
  
  
   
   
  
   break;
 
  
  
   
   
 }
 
  
  
   
   
 case4:
 
  
  
   
   
 {
 
  
  
   
   
  
   printf(
" \n\tEXI TPOI NT" )
;
 
  
  
   
   
  
   break;
 
  
  
   
   
 }
 
  
  
   
   
 def aul t
:
 
  
  
   
   
 {
 
  
  
   
   
  
   printf("\n\tPl easeEnt eraVal i
dChoice(1/2/3/
4)"
);
 
  
  
   
   
 }
 
  
  
   
   
  
    
 
  
  
   
}
 
  
 }
 
  
 whi l
e( choi ce!=4) ;
 
  
 return0;
}
v
oi dpush( )
{
 
  
 i
f(top>=n- 1)
 
  
 {
 
  
  
   
print f(
"\ n\tSTACKi sov erflow");
 
  
  
   
 
 
  
 }
 
  
 else
 
  
 {
 
  
  
   
print f(
"Ent erav aluet obepushed: "
);
 
  
  
   
scanf (
"%d" ,
&x);
 
  
  
   
top++;
 
  
  
   
stack[top]=x;
 
  
 }
}
v
oi dpop( )
{
 
  
 i
f(top<=-1)
 
  
 {
 
  
  
   
print
f("\n\tStackisunderfl
ow");
 
  
 }
 
  
 else
 
  
 {
 
  
  
   
print
f("\n\tThepoppedel ementsis%d"
,st
ack[
top]
);
 
  
  
   
top--
;
 
  
 }
}
v
oi ddisplay()
{
 
  
 i
f(top>=0)
 
  
 {
 
  
  
   
print
f("\nTheel ementsinSTACK\ n"
);
 
  
  
   
for(i
=top;i>=0;i--
)
 
  
  
   
  
  
printf(
"\n%d",st
ack[
i]
);
 
  
  
   
print
f("\nPressNex tChoice"
);
 
  
 }
 
  
 else
 
  
 {
 
  
  
   
print
f("\nTheSTACKi sempty")
;
 
  
 }
 
  
 
}

OUTPUT
Ent
erthesi
zeofSTACK[
MAX=100]
:3

STACKOPERATI ONSUSINGARRAY
-
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Ent
ertheChoice:1
Ent
erav al
uetobepushed:12

Ent
ert
heChoi
ce:
1
Ent
eraval
uet
obepushed:
24

Ent
ert
heChoi
ce:
1
Ent
eraval
uet
obepushed:
98

Ent
ert
heChoi
ce:
3

Theel
ement
sinSTACK
98
24
12
PressNextChoice
Entert
heChoice:2

Thepoppedel
ement
sis98
Ent
ertheChoi
ce:
3

Theel
ement
sinSTACK

24
12
PressNextChoice
Entert
heChoice:4

EXI
TPOI
NT

RESULT:
ThusCpr
ogr
am t
oimpl
ementst
ackusi
ngar
rayi
sexecut
edsuccessf
ull
y.
EXNO:
6b QUEUEUSI
NGARRAYI
MPLEMENTATI
ON
AI
M:
Towr
it
eCpr
ogr
am t
oimpl
ementqueueusi
ngar
ray
ALGORI THM
1.Start
2.Def i
neaar rayqueueofsi zen=5
3.Ini
tiali
zef ront=0, r
ear=–1
4.Displayamenul ist
ingqueueoperat i
onsusingswit
chstat
ement
.
5.Readt hechoi ce
6.Ifchoi ce=1t hen,
I
fr ear=x
PrintQueueFul l
Else
I
ncr ementr ear
Storeel ementatcur rentposi
tionofrear
7.Ifchoi ce=2t hen
I
ff ront=r earthen
PrintQueueempt y
Else
Displaycur rentfrontel
ement
I
ncr ementf ront
8.Ifchoi ce=3t hen
Displayqueueel ementsstart
ingfrom fr
onttorear
.
9.Stop
PROGRAM
#incl
ude<st dio.h>
#incl
ude<coni o.h>
#definen5
voidmai n()
{
 
  
 i
ntqueue[ n],ch=1,front =0,rear=0,i,
j=1,x=n;
 
  
 cl
rscr (
);
 
  
 pri
ntf("Queueusi ngAr r
ay "
);
 
  
 pri
ntf("\n1. Insertion\ n2.Delet i
on\ n3.Displ
ay\n4.Exi
t")
;
 
  
 while(ch)
 
  
 {
 
  
  
  
 printf("\nEnt ert heChoi ce:");
 
  
  
  
 scanf ("%d" ,
&ch) ;
 
  
  
  
 swi t
ch( ch)
 
  
  
  
 {
 
  
  
  
 case1:
 
  
  
  
  
  
 if(rear ==x)
 
  
  
  
  
  
    
 print f("
\nQueuei sFul l");
 
  
  
  
  
  
 else
 
  
  
  
  
  
 {
 
  
  
  
  
  
    
 print f("
\nEnt erno%d: ",
j
++) ;
 
  
  
  
  
  
    
 scanf ("
%d" ,&queue[ rear++]);
 
  
  
  
  
  
 }
 
  
  
  
  
  
 break;
 
  
  
  
 case2:
 
  
  
  
  
  
 if(front ==rear )
 
  
  
  
  
  
 {
 
  
  
  
  
  
    
 print f("
\nQueuei sempt y")
;
 
  
  
  
  
  
 }
 
  
  
  
  
  
 else
 
  
  
  
  
  
 {
 
  
  
  
  
  
    
 print f("
\nDel etedEl ementi s%d" ,
queue[
front++])
;
 
  
  
  
  
  
    
 x++;
 
  
  
  
  
  
 }
 
  
  
  
  
  
 break;
 
  
  
  
 case3:
 
  
  
  
  
  
 print f("\nQueueEl ement sar e:\n");
 
  
  
  
  
  
 if(front ==rear )
 
  
  
  
  
  
    
 print f("
\nQueuei sEmpt y")
;
 
  
  
  
  
  
 else
 
  
  
  
  
  
 {
 
  
  
  
  
  
    
 for(i=f r
ont; i<rear;i++)
 
  
  
  
  
  
    
 {
 
  
  
  
  
  
    
   
  printf("
%d" ,
queue[ i
])
;
 
  
  
  
  
  
    
   
  printf("
\n" );
 
  
  
  
  
  
    
 }
 
  
  
  
  
  
    
 break;
 
  
  
  
  
  
 case4:
 
  
  
  
  
  
    
 ex i
t(0) ;
 
  
  
  
  
  
 def aul t:
 
  
  
  
  
  
    
 print f("
Wr ongChoi ce: pleaseseet heopti
ons" )
;
 
  
  
  
  
  
 }
 
  
  
  
 }
 
  
 }getch( );}

OUTPUT
QueueusingAr
ray
1.
Inser
ti
on
2.
Delet
ion
3.
Displ
ay
4.
Exit
Ent
ert heChoice:
1
Ent
erno1: 10
Ent
ert heChoice:
1
Ent
erno2: 54
Ent
ert heChoice:
1
Enterno3: 98
Ent
ert heChoice:
1
Ent
erno4: 234
Ent
ert heChoice:
3
QueueEl ementsare:
10
54
98
234
Ent
ert heChoice:
2
DeletedElementis10
Ent
ert heChoice:
3
QueueEl ementsare:
54
98
234
Ent
ert heChoice:
4
EXNO: 6a STACKUSI
NGLI
NKEDLI
STI
MPLEMENTATI
ON
AI
M:
Towr
it
eapr
ogr
am t
ocr
eat
eSt
ackusi
ngLi
nkedl
i
st
ALGORI THM
1.Start
2.Defi
neasi nglylinkedlistnodef orstack
3.CreateHeadnode
4.Di
splayamenul ist
ingstackoper ati
ons
5.Acceptchoi ce
6.I
fchoi ce=1t hen
Createanewnodewi thdata
Makenewnodepoi nttofir
stnode
Makeheadnodepoi nttonewnode
7.I
fchoi ce=2t hen
Maket empnodepoi nttofi
rstnode
Makeheadnodepoi nttonextoft empnode
Releasememor y
8.I
fchoi ce=3t hen
Displayst ackelement sstarti
ngf r
om headnodet
il
lnul
l
9.Stop

PROGRAM:

#i
nclude<stdio.
h>
#i
nclude<conio.h>
#i
nclude<process.h>
#i
nclude<al
loc.h>
st
ructnode
{
i
ntlabel
;
st
ructnode* next;
}
;
mai
n()
{
intch=0;
intk;
structnode* h,*temp, *head;
head=( st ructnode* )mal loc(si
zeof(
structnode)
);
head->next=NULL;
whi l
e(1)
{
printf
("\nSt ackusi ngLi nkedLi st\n"
);
printf
("1->Push" );
printf
("2->Pop" );
printf
("3->Vi ew" );
printf
("4->Exi t\n");
printf
("Ent ery ourchoi ce: ");
scanf("%d" ,&ch) ;
swi t
ch(ch)
{
case1:
temp=( structnode* )
(malloc(
sizeof(
str
uctnode)
));
printf ("Enterlabel fornewnode: "
);
scanf ("%d" ,&temp- >label)
;
h=head;
temp- >next=h- >next;
h->next=t emp;
break;
case2:
h=head- >next ;
head- >next=h- >next ;
printf ("Node%sdel eted\n",h-
>label)
;
free( h) ;
break;
case3:
printf ("\nHEAD- >");
h=head;
whi le(h- >next! =NULL)
{
h=h- >next ;
printf ("%d- >",h->label);
}
printf ("NULL\ n");
break;
case4:
exit(0) ;
}
}
}
OUTPUT
St
ackusi
ngLi
nkedLi
st
1-
>Push2-
>Pop3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
1
Ent
erl
abel
fornewnode:
23
Newnodeadded
St
ackusi
ngLi
nkedLi
st
1-
>Push2-
>Pop3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
1
Ent
erl
abel
fornewnode:
34
St
ackusi
ngLi
nkedLi
st
1-
>Push2-
>Pop3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
3
HEAD-
>34-
>23-
>NULL

RESULT:
Thus,
theCpr
ogr
am t
oimpl
ementst
ackusi
ngl
i
nkedl
i
sti
sexecut
edsuccessf
ull
y.
AI
M:
Towr
it
eapr
ogr
am t
ocr
eat
eaQueueusi
ngLi
nkedl
i
st
ALGORI
THM:
1.Star t
2.Def i
neasi nglyli
nkedlistnodef orstack
3.Cr eateHeadnode
4.Displayamenul ist
ingstackoper ati
onsusingswit
chst
atement
5.Readt hechoi ce
6.Ifchoice=1t hen
Createanewnodewi thdata
Makenewnodepoi nttofir
stnode
Makeheadnodepoi nttonewnode
7.Ifchoice=2t hen
Maket empnodepoi nttofi
rstnode
Makeheadnodepoi nttonextoft empnode
Releasememor y
8.Ifchoice=3t hen
Displaystackelement sstarti
ngf r
om headnodet
il
lnul
l
9.Stop

PROGRAM
#include<stdio.h>
#include<coni o.h>
#include<process. h>
#include<alloc.h>
structnode
{
i
ntl abel;
structnode* next ;
};
voidmai n()
{
i
ntch=0;
i
ntk;
str
uctnode* h,*t
emp, *head;
head=( structnode* )mall
oc(si
zeof
(str
uctnode)
);
head-
>next=NULL;
whil
e(1)
{
pri
ntf
("\nQueueusi ngLinkedLi
st\n")
;
pri
ntf
("1->Inser
t");
pri
ntf
("2->Delet
e" );
pri
ntf
("3->View" )
;
pri
ntf
("4->Exit\n"
);
printf("
Ent eryourchoi ce:")
;
scanf ("%d" ,&ch);
swi t
ch( ch)
{
case1:
temp=( structnode* )(mall
oc(si
zeof(
str
uctnode)
));
printf("
Ent erlabelfornewnode: "
);
scanf ("%d" ,&temp->label
);
h=head;
whi l
e( h->next! =NULL)
h=h- >next ;
h->next=t emp;
temp- >next=NULL;
break;
case2:
h=head- >next;
head- >next=h- >next;
printf("
Nodedel eted\ n"
);
free(h);
break;
case3:
printf("
\n\ nHEAD- >");
h=head;
whi l
e( h->next!=NULL)
{
h=h- >next;
pr i
ntf("
%d- >",
h->l
abel
);
}
print
f("NULL\ n");
break;
case4:
exit(
0) ;
}
}
}
OUTPUT:
Queueusi
ngLi
nkedLi
st
1-
>Inser
t2-
>Del
ete3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
1
Ent
erl
abel
fornewnode:
12
Queueusi
ngLi
nkedLi
st
1-
>Inser
t2-
>Del
ete3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
1
Ent
erl
abel
fornewnode:
23
Queueusi
ngLi
nkedLi
st
1-
>Inser
t2-
>Del
ete3-
>Vi
ew4-
>Exi
t
Ent
ery
ourchoi
ce:
3
HEAD-
>12-
>23-
>NULL

RESULT:
Thus,theCprogr
am t
oimpl
ementqueueusi
ngsi
ngl
eli
nkedl
i
sti
sexecut
ed
successful
l
y.
EXNO:
7a STACKAPPLI
CATI
ON–I
NFI
XTOPOSTFI
XCONVERSI
ON
AI
M:
Towri
teaCpr
ogr
am t
oimpl
ementt
heconv
ersi
onofi
nfi
xtopost
fi
xexpr
essi
onusi
ng
st
ack.
ALGORI THM:
1.Start
2.Def i
neaar rayst ackofsi zemax=20
3.Ini
tiali
zet op=- 1
4.Readt heinfixexpr essionchar acter-by -
character
5.Ifchar acterisanoper andpr inti t
6.Ifchar acterisanoper at or
Compar etheoper at or ’
spr ioritywi t
ht hestack[top]oper at
or.
7.Ifthest ack[ top]operat orhashi gherorequal pr i
ori
tythant heinputoperat
or,
Popi tfrom thest ackandpr intit.
Else
8.Pusht heinputoper at
oront othest ack
9.Ifchar acterisal eftpar ent hesis, thenpushi tont othest ack.
10.Ifthechar acterisar ightpar ent hesis,popal ltheoper ator
sf r
om t hest
ackand
pri
ntit
unti
lal eftparenthesi sisencount ered.Donotpr i
ntthepar enthesi
s.
PROGRAM
#i
ncl
ude<st
dio.
h>
charst
ack[
20]
;
i
ntt
op=-
1;
v
oidpush(
charx)
{
 
  
 st
ack[
++t
op]=x;
}
 
charpop(
)
{
 
  
 i
f(
top==-
1)
 
  
  
  
 r
etur
n-1;
 
  
 el
se
 
  
  
  
 r
etur
nst
ack[
top-
-]
;
}
 
i
ntpr
ior
it
y(charx)
{
 
  
 i
f(
x=='
('
)
 
  
  
  
 r
etur
n0;
 
  
 i
f(
x=='
+'|
|x=='
-
')
 
  
  
  
 r
etur
n1;
 
  
 i
f(
x=='
*'|
|x=='
/'
)
 
  
  
  
 r
etur
n2;
}
 
mai
n()
{
 
  
 charexp[
20]
;
 
  
 char*
e,x;
 
  
 pr
int
f("
Ent
ert
heexpr
essi
on:
:")
;
 
  
 scanf
("%s"
,exp)
;
 
  
 e=exp;
 
  
 whi
l
e(*
e!='
\0'
)
 
  
 {
 
  
  
  
 i
f(
isal
num(
*e)
)
 
  
  
  
  
  
 pr
int
f("
%c"
,*
e);
 
  
  
  
 el
sei
f(
*e=='
('
)
 
  
  
  
  
  
 push(
*e)
;
 
  
  
  
 el
sei
f(
*e=='
)'
)
 
  
  
  
 {
 
  
  
  
  
  
 whi
l
e((
x=pop(
))!
='(
')
 
  
  
  
  
  
  
  
 pr
int
f("
%c"
,x)
;
 
  
  
  
 }
 
  
  
  
 el
se
 
  
  
  
 {
 
  
  
  
  
  
 whi
l
e(pr
ior
it
y(st
ack[
top]
)>=pr
ior
it
y(*
e))
 
  
  
  
  
  
  
  
 pr
int
f("
%c"
,pop(
));
 
  
  
  
  
  
 push(
*e)
;
 
  
  
  
 }
 
  
  
  
 e++;
 
  
 }
 
  
 whi
l
e(t
op!
=-1)
 
  
 {
 
  
  
  
 pr
int
f("
%c"
,pop(
));
 
  
 }
}
OUTPUT:
Entertheexpr
essi
on:
:a+b*c
abc*+
Entertheexpr
essi
on:
:(a+b)
*c+(
d-a)
ab+c*da-+

RESULT:
ThustheCprogram t
oconv
erti
nfi
xexpr
essi
oni
ntopost
fi
xfor
m usi
ngst
acki
s
execut
edsuccessf
ull
y.
EXNO:
7b QUEUEAPPLI
CATI
ON–PRI
ORI
TYQUEUE
EXNO:
8 I
MPLEMENTATI
ONOFTREETRAVERSALS
AI
M:
Towr
it
ecpr
ogr
am t
odemonst
rat
etr
eet
rav
ersal
s

ALGORI THM:
1.Start
2.Createast ructuret hatrepresentsanodewi thvalue,leftandr i
ghtpoi
nter
.
3.newnodei saf unct iont hatcreatesanewnodeofabov estructureandret
urnsi
t.
4.Callthenewnodef unctiontocr eatearoot ,
inter
medi ateandl eafnodes.
5.Callpri
ntpreorderf unct i
ontoper form preordertr
av er
sal.
6.Callpri
ntI
nor derf unctiontoper forminordertraversal
.
7.Callpri
ntpostor derf uncti
ontoper form postordertraversal
.
8.Stop

Preor
derfunction
1.Pri
ntt
her oot
2.Pri
ntt
hel eftsubtr
eebyrecursi
vecal
l
3.Pri
ntt
her ightsubtr
eebyrecursi
vecal
l

I
norderf
unction
1.Pri
ntt
heleftsubtr
eebyrecursi
vecal
l
2.Pri
ntt
heroot
3.Pri
ntt
herightsubtr
eebyrecursi
vecal
l

Post
orderf
unction
1.Pr
intt
heleftsubtr
eebyrecursi
vecal
l
2.Pr
intt
herightsubtr
eebyrecursi
vecal
l
3.Pr
intt
heroot

PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
st
ructnode
{
i
ntdat
a;
st
ructnode*l
eft
;
st
ructnode*r
ight
;
}
;
st
ructnode*newNode(
intdat
a)
{
st
ructnode*node=(
str
uctnode*
)mal
l
oc(
sizeof
(st
ructnode)
);
node-
>dat
a=dat
a;
node-
>lef
t=NULL;
node-
>ri
ght=NULL;
r
etur
n(node)
;
}
v
oidpr
int
Post
order
(st
ructnode*node)
{
i
f(node==NULL)
r
etur
n;
pr
int
Post
order
(node-
>lef
t);
pr
int
Post
order
(node-
>ri
ght
);
pr
int
f("
%d"
,node-
>dat
a);
}
v
oidpr
int
Inor
der
(st
ructnode*node)
{
i
f(node==NULL)
r
etur
n;
pr
int
Inor
der
(node-
>lef
t);
pr
int
f("
%d"
,node-
>dat
a);
pr
int
Inor
der
(node-
>ri
ght
);
}
v
oidpr
int
Preor
der
(st
ructnode*node)
{
i
f(node==NULL)
r
etur
n;
pr
int
f("
%d"
,node-
>dat
a);
pr
int
Preor
der
(node-
>lef
t);
pr
int
Preor
der
(node-
>ri
ght
);
}
i
ntmai
n()
{
st
ructnode*
root=newNode(
1);
r
oot
->l
eft=newNode(
2);
r
oot
->r
ight=newNode(
3);
r
oot
->l
eft
->l
eft=newNode(
4);
r
oot
->l
eft
->r
ight=newNode(
5);
pr
int
f("
\nPr
eor
dert
rav
ersal
ofbi
nar
ytr
eei
s\n"
);
pr
int
Preor
der
(root
);
pr
int
f("
\nI
nor
dert
rav
ersal
ofbi
nar
ytr
eei
s\n"
);
pr
int
Inor
der
(root
);
pr
int
f("
\nPost
ordert
rav
ersal
ofbi
nar
ytr
eei
s\n"
);
pr
int
Post
order
(root
);
get
char
();
r
etur
n0;
}
OUTPUT
Pr
eor
dert
rav
ersal
ofbi
nar
ytr
eei
s
12453
I
nor
dert
rav
ersal
ofbi
nar
ytr
eei
s
42513
Post
ordert
rav
ersal
ofbi
nar
ytr
eei
s
45231

RESULT:
Thust
heCpr
ogr
am t
oimpl
ementbi
nar
ytr
eet
rav
ersali
sexecut
edsuccessf
ull
y
EXNO:
9 I
MPLEMENTATI
ONOFBI
NARYSEARCHTREE
AI
M:

Towr
it
eCpr
ogr
am t
oconst
ructabi
nar
ysear
cht
reeandper
for
m sear
ch
ALGORITHM:
1.Star
t
2.Call
insertt
oinsertanelementi nt
obinarysearcht r
ee.
3.Call
deletetodel
eteanel ementf r
om binar
ysear chtree.
4.Call
findmaxtofindtheel ementwithmaximum v alueinbinarysearchtree
5.Call
findmintofi
ndt heelementwi t
hmi ni
mum v alueinbinarysearchtree
6.Call
findtocheckthepr esenceoftheelementint hebinarysearchtree
7.Call
displ
aytodisplaytheel ementsofthebinarysearchtree
8.Call
makeempt ytodelet etheenti
retr
ee.
9.Stop

Insertfunct i
on
1.Gett heel ementt obeinserted.
2.Findt heposi ti
oninthetreewher etheelementtobeinsert
edbycheckingthe
element si nthetreebytraversingfr
om theroot.
3.Iftheel ementt obeinsertedi sl
essthantheelementi
nt hecur
rentnodeinthetr
ee
thent r
av erseleftsubtr
ee
4.Iftheel ementt obeinsertedi sgr
eatert
hantheelementinthecurr
entnodeinthetr
ee
thent r
av erserightsubtr
ee
5.Insertt heelementifthereisnof urt
hermov e

Deletef
unction
1.Gettheelementt
obedelet
ed.
2.Findthenodeint
hetr
eethatcont
aintheel
ement.
3.Delet
ethenodeanrear
rangethel
eftandri
ghtsi
bli
ngsi
fanypr
esentf
ort
hedel
eted
node

Fi
ndmaxf uncti
on
1.Tr
aversethetreefr
om theroot.
2.Fi
ndther i
ghtmostleafnodeoftheent
ir
etr
eeandr
etur
nit
3.I
fthetreeisemptyreturnnul
l
.

Fi
ndminf unction
1.Tr
averset hetreef
rom ther
oot.
2.Fi
ndthel eftmostl
eafnodeoftheent
ir
etr
eeandr
etur
nit
3.I
fthetreei semptyret
urnnul
l.

Fi
ndfuncti
on
1.Tr
aversethet
reef
rom t
her
oot
.
2.Checkwhethertheelementtosearchedmatcheswit
helementoft
hecur r
entnode.I
f
matchoccursreturnit
.
3.Other
wiseiftheelementi
slessthanthatoftheel
ementofthecur
rentnodethen
sear
chtheleafsubtree
4.El
sesearchrightsubtr
ee.

Makeempt
yfunct
ion
Makether
ootoft
hetreet
opoi
ntt
onul
l
.
PROGRAM:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<coni
o.h>
st
ructsear
cht
ree
{
i
ntel
ement
;
st
ructsear
cht
ree*
lef
t,
*r
ight
;
}
*root
;
t
ypedefst
ructsear
cht
ree*
node;
t
ypedefi
ntEl
ement
Type;
nodei
nser
t(El
ement
Type,
node)
;
nodedel
ete(
Element
Type,
node)
;
v
oidmakeempt
y()
;
nodef
indmi
n(node)
;
nodef
indmax
(node)
;
nodef
ind(
Element
Type,
node)
;
v
oiddi
spl
ay(
node,
int
);
v
oidmai
n()
{
i
ntch;
El
ement
Typea;
nodet
emp;
makeempt
y()
;
whi
l
e(1)
{
pri
ntf(
"\n1.I
nsert
\n2.Del
ete\
n3.Fi
nd\
n4.Fi
ndmi
n\n5.Fi
ndmax\
n6.
Displ
ay\
n7.
Exi
t\nEnterYourChoice:
");
scanf
("%d"
,&ch)
;
swi
tch(
ch)
{
case1:
pr
int
f("
Ent
eranel
ement:
");
scanf
("%d"
,&a)
;
r
oot=i
nser
t(a,
root
);
br
eak;
case2:
pr
int
f("
\nEnt
ert
heel
ementt
odel
ete:
");
scanf
("%d"
,&a)
;
r
oot=del
ete(
a,r
oot
);
br
eak;
case3:
pr
int
f("
\nEnt
ert
heel
ementt
osear
ch:
");
scanf
("%d"
,&a)
;
t
emp=f
ind(
a,r
oot
);
i
f(t
emp!
=NULL)
pr
int
f("
Elementf
ound"
);
el
se
pr
int
f("
Elementnotf
ound"
);
br
eak;
case4:
t
emp=f
indmi
n(r
oot
);
i
f(
temp==NULL)
pr
int
f("
\nEmpt
ytr
ee"
);
el
se
pr
int
f("
\nMi
nimum el
ement:
%d"
,temp-
>el
ement
);
br
eak;
case5:
t
emp=f
indmax(
root
);
i
f(
temp==NULL)
pr
int
f("
\nEmpt
ytr
ee"
);
el
se
pr
int
f("
\nMaxi
mum el
ement:
%d"
,temp-
>el
ement
);
br
eak;
case6:
i
f(
root
==NULL)
pri
ntf
("\
nEmpt
ytr
ee"
);
el
se
di
spl
ay(
root
,1)
;
br
eak;
case7:
exi
t(
0);
def
aul
t:
pr
int
f("
Inv
ali
dChoi
ce"
);
}
}
}
nodei
nser
t(El
ement
Typex,
nodet
)
{
i
f(
t==NULL)
{
t=(
node)
mal
l
oc(
sizeof
(node)
);
t
->el
ement=x;
t
->l
eft=t
->r
ight=NULL;
}
el
se
{
i
f(
x<t
->el
ement
)
t
->l
eft=i
nser
t(x,
t->l
eft
);
el
se
i
f(
x>t
->el
ement
)t-
>ri
ght=i
nser
t(x
,t-
>ri
ght
);
}
r
etur
nt;
}
nodedel
ete(
Element
Typex,
nodet
)
{
nodet
emp;
i
f(
t==NULL)
pr
int
f("
\nEl
ementnotf
ound"
);
el
se
{
i
f(
x<t
->el
ement
)
t
->l
eft=del
ete(
x,t
->l
eft
);
el
sei
f(
x>t
->el
ement
)
t
->r
ight=del
ete(
x,t
->r
ight
);
el
se
{
i
f(
t-
>lef
t&&t
->r
ight
)
{
t
emp=f
indmi
n(t
->r
ight
);
t
->el
ement=t
emp-
>el
ement
;
t
->r
ight=del
ete(
t-
>el
ement
,t
->r
ight
);
}
el
sei
f(
t-
>lef
t==NULL)
{
t
emp=t
;
t
=t-
>ri
ght
;
f
ree(
temp)
;
}
el
se
{
t
emp=t
;
t
=t-
>lef
t;
f
ree(
temp)
;
}
}
}
r
etur
nt;
}
v
oidmakeempt
y()
{
r
oot=NULL;
}
nodef
indmi
n(nodet
emp)
{
i
f(
temp==NULL|
|temp-
>lef
t==NULL)
r
etur
ntemp;
r
etur
nfi
ndmi
n(t
emp-
>lef
t);
}
nodef
indmax
(nodet
emp)
{
i
f(
temp==NULL|
|temp-
>ri
ght
==NULL)
r
etur
ntemp;
r
etur
nfi
ndmax
(temp-
>ri
ght
);
}
nodef
ind(
Element
Typex,
nodet
)
{
i
f(
t==NULL)
r
etur
nNULL;
i
f(
x<t
->el
ement
)
r
etur
nfi
nd(
x,t
->l
eft
);
i
f(
x>t
->el
ement
)
r
etur
nfi
nd(
x,t
->r
ight
);
r
etur
nt;
}
v
oiddi
spl
ay(
nodet
,i
ntl
evel
)
{
i
nti
;
i
f(
t)
{
di
spl
ay(
t-
>ri
ght
,lev
el+1)
;
pr
int
f("
\n"
);
f
or(
i=0;
i
<lev
el;
i
++)
pr
int
f(""
);
pr
int
f("
%d"
,t-
>el
ement
);
di
spl
ay(
t-
>lef
t,l
evel
+1)
;}}
OUTPUT:
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
1
Ent
eranel
ement:
10
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
1
Ent
eranel
ement:
20
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
1
Ent
eranel
ement:
5
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
4
Thesmal
l
estNumberi
s5
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
3
Ent
eranel
ement:
100
El
ementnotFound
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
2
Ent
eranel
ement:
20
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
6
20
10
1.I
nser
t
2.Del
ete
3.Fi
nd
4.Fi
ndMi
n
5.Fi
ndMax
6.Di
spl
ay
7.Exi
t
Ent
ery
ourChoi
ce:
7

RESULT:
Thust
heCpr
ogr
am t
oimpl
ementbi
nar
ysear
cht
reei
sexecut
edsucessf
ull
y

You might also like