Anda di halaman 1dari 49

COMPUTER PROGRAMMING LAB

Program-1:
Write a program to evaluates the folloi!g alge"rai# e$pressio!s after rea%i!g
!e#essar& values from the user'
Program-1(a):
Aim: Compute e$pressio! (a$)"*+(a$,"*'
-'start
.'Rea% a /$ a!% "
0'if((a1$,"*234* the!
5' "egi!
6' #3(a1$)"*+(a1$,"*7
8' rite #
9' e!% if
:' otherise
;' <%ivisio! is !ot possi"le=
-4' stop
Program-1(b):
Aim: Evaluate the e$pressio! .'6log$)0.
4
)>$
.
,&
.
>) .$&
-'start
.'Rea% $ a!% &
0'if($234* the!
5' "egi!
6' ?3.'6log$)0.
4
)>$
.
,&
.
>) .$&7
8' rite ?
9' e!% if
:' otherise
;' <E$pressio! Evaluatio! is !ot possi"le=
-4' @top

Program-1(c):
Aim : Evaluate the e$pressio! -+( .*e
(,($,m*+ .s** .
-'start
.'Rea% $/m a!% s
0'if(s234* the!
5' "egi!
6' ?3-+( .*e
(,($,m*+ .s** .
8' rite ?
9' e!% if
:' otherise
;' <E$pressio! Evaluatio! is !ot possi"le=
-4' @top
-
COMPUTER PROGRAMMING LAB
Program-2(a):
Aim A Printing three given integers in ascending order
-'start
.'Rea% a/" a!% #
0' if(aB"* the!
5' I!ter#ha!ge a a!% "
6' Otherise if(aB#* the!
8' I!ter#ha!ge a a!% #
9' Otherise if("B#* the!
:' I!ter#ha!ge " a!% #
;' Write a/ " a!% #
-4'@top'
Program-2(b):

Aim: Compute Sum of 1 + 2+ 3 + _ _ _ _ _ n
'
-'start
.'Rea% !
0' i!itiali?e i3-/sum34
5'if(iC3!* the!
6'"egi!
8' sum3sum)i7
9' i))7
:' goto step 5'
;' e!% if
-4' otherise Write sum
-4'@top'

Program-2(c):

Aim: Compute sum of 1 + x
2
/2! + x
2
/ 4!+ _ _ _ _ _ upto ten terms
-'start
.'Rea% $
0' i!itiali?e i3-/sum3-
5'if(iC9* the!
6'"egi!
8' sum3sum)($1$*+fa#t (.1i*7 +1 Dere fa#t( * is a fa#torial fu!#tio! e have to rite it
i! a program hile impleme!ti!g 1+
9' i))7
:'goto step 5'
;' e!% if
-4' otherise Write sum
--'@top'
.
COMPUTER PROGRAMMING LAB
Program-2(d):

Aim: Compute sum x +x
3
/3! + x
5
/5!+ _ _ _ _ _ upto 7
!
digit accurac"
-'start
.'Rea% $
0' i!itiali?e i34/sum34
5'if(iC39* the!
6'"egi!
8' l3.1i)-7
--' sum3sum)($
l
*+fa#t(l*7 +1 Dere fa#t( * is a fa#torial fu!#tio! e have to rite it i! a
program hile impleme!ti!g 1+
-.' i))7
-0' goto step 5'
-5' e!% if
-6' otherise Write sum
-4'@top'
Program-2(e):
Aim : Rea% $ a!% #ompute &3- for $B4
&34 for $34
&3,- for $C4
-'start
.'Rea% $
0'if($B4* the!
5' &3-7
6' otherise if($334* the!
8' &347
9' otherise &3,-7
:'rite &'
;' stop
0
COMPUTER PROGRAMMING LAB
Program-3:

Aim : rite # program usi!g for stateme!t to fi!% the folloi!g from a give! set of .4
i!tegers
a* total !um"er of eve! i!tegers "* total !um"er of o%% i!tegers
#* sum of all eve! i!tegers %* sum of all o%% i!tegers

Input : Rea% .4 i!tegers'
Output : Write total !o' of eve! a!% o%% i!tegers a!% also rite sum of eve! a!% o%%
i!tegers'

-' @tart
.' I!itiali?e i3-/@E3@O3TE3TO34
0' if(iC3.4* the!
5' "egi!
6' Rea% !7
8' if(!E.334*
9' F
:' TE))7
;' @E3@E)!7
-4 G
--' else
-.' F
-0' TO))7
-5' @O3@O)!7
-6' G
-8' i))7
-9' goto step 0'
-:' e!% if +1 first if 1+
-;' otherise
.4' rite TE/TO/@E/@O
.-' @top'
5
COMPUTER PROGRAMMING LAB
Program-4:
Aim: #rite a $ program to o%tain the product of t&o matrices ' of si(e )3*3+ and
, of si(e )3*2+- he resu.tant matrix $ is to %e printed out a.ong &ith ' and
,- 'ssume suita%.e va.ues for ' / ,-
Input: Rea% to 0 1 0 matri#es sa& A a!% B'
Output : Write matri$ C'
-' @tart
.' Rea% matri#es A a!% B of or%er 010
0' i347
5' if(iC0* the!
6' F
8' H347
9' if(HC0* the!
:' F
;' #IiJIHJ347
-4' K347
--' if(KC0* the!
-.' F
-0' #IiJIHJ3#IiJIHJ)aIiJIKJ1"IKJIHJ7
-5' G
-6' K))7 goto step --'
-8' H))7 goto step 9'
-9' G
-:' i))7 goto step 5'
-;' G
.4' Write matri#es A/B/C'
.-' @top
6
COMPUTER PROGRAMMING LAB
Program-5:
Aim : 0sing s&itch1case statement2 &rite a $ program that ta3es t&o operands
and one operator from the user2 performs the operation and then prints
the ans&er- )consider operators +212/24 and 5+-
Input : To opera!%s a!% o!e operator'
Output : Compute% result of respe#tive arithmeti# e$p'
-'@tart
.'Rea% a/" a!% a!& o!e Operator(op*
0'if(op33 L)L* the!
5' #3a)"7
6' 'else if(op33M,N* the!
8' #3a,"7
9' else If(op33M+M* the!
:' #3a+"7
;' else If(op33M1M* the!
-4' #3a1"7
-- else If(op33MEM* the!
-.' #3aE"7
-0 e!% if
-5' Write #
-6' @top'
8
COMPUTER PROGRAMMING LAB
Program-6:

Aim: #rite $ procedures to add2 su%tract2 mu.tip." and divide t&o comp.ex
num%ers )x+i"+ and )a+i%+-'.so &rite the main program that uses these
procedures-
1. Addition Procedure
a%%( *
F
?-3a)$7
?.3")&7
Write ?-/?.
G
2. Subtraction Procedure
su"( *
F
?-3a,$7
?.3",&7
Write ?-/?.
G
3. Multiplication Procedure
mul( *
F
?-3a1$,"1&7
?.3a1&)"1$7
Write ?-/?.
G
5. Division Procedure
%iv( *
F
?-3(a1$)"1&*+($1$)&1&*7
?.3("1$,a1&*+($1$)&1&*7
Write ?-/?.
G
9
COMPUTER PROGRAMMING LAB
Program-7:

Aim he tota. distance trave.ed %" vehic.e in 6t7 seconds is given %"
distance 8 ut+1/2at2 &here 6u7 and 6a7 are the initia. ve.ocit" )m/sec-+ and
acce.eration )m/sec2+- #rite $ program to find the distance trave.ed at
regu.ar interva.s of time given the va.ues of 6u7 and 6a7- he program
shou.d provide the f.exi%i.it" to the user to se.ect his o&n time interva.s
and repeat the ca.cu.ations for different va.ues of 6u7 and 6a7-
-'@tart
.'Rea% the regular time i!terval( t- O t. *values
0'Repeat
5' F
6' Rea% the values of i!tial velo#it&(u* a!% a##elaratio!(a*
8' %3u1t-)4'61a1t-1t-7
9' t-3t-).7
:' Write %
;' G
-4' u!til (t-C3t.*7
--'@top'
:
COMPUTER PROGRAMMING LAB
Program-8:
Aim:
' c.oth sho& room has announced the fo..o&ing seasona. discounts on purchase
of items-

Purchase 'mount 9iscount )Percentage+
:i.. $.oth !and.oom items
111;; 1 5-;
1;112;; 5-; 7-5
2;113;; 7-5 1;-;
'%ove 3;; 1;-; 15-;
#rite a $ program using s&itch and <f statements to comp.ete the net amount to
%e paid %" a customer-
-'@tart
.'Rea% the t&pe of item to "e pur#hase%'
0'Rea% the Pur#hase Amou!t
5'if pur#hase% item is mill #loths the!
6' if(paB3- OO paC3-44*
8' %is347
9' else if(paB3-4- OO paC3.44*
:' %is367
;' else if(paB3.4- OO paC3044*
-4' %is39'67
--' else if(paB044*
-.' %is3-47
-0' !a3!a)(pa,(%is1pa*+-44*7
-5' Otherise if(paB3- OO paC3-44*
-6' %is36'47
-8' else if(paB3-4- OO paC3.44*
-9' %is39'67
-:' else if(paB3.4- OO paC3044*
-;' %is3-47
.4' else if(paB044*
.-' %is3-67
..' !a3!a)(pa,(%is1pa*+-44*7
.0' Repeat the pro#ess o! !um"er of pur#hases'
.5'Write <Net Amou!t To Be Pai% B& Customer is A= / !a
.6'@top'
;
COMPUTER PROGRAMMING LAB
Program-:
Aim : =iven a num%er2 &rite $ program using &hi.e .oop to reverse the digits of
the num%er- >xamp.e 1234 to %e &ritten as 4321-
-'@tart
.'Rea% the !um"er to "e reverse%'
0' I!itiali?e rev34'
5'if(!B4*
6' "egi!
8' rem3!E-47
9' rev3rev1-4)rem7
:' !3!+-47
;' goto step 5'
-4' e!% if
--'otherise Write <the reverse !um"er is P/rev
-.'stop'
Program-1!:
-4
COMPUTER PROGRAMMING LAB
Aim : he ?i%onacci se@uence of num%ers is 1212223252AB %ased on the
recurrence re.ation f)n+ 8 f )n11+ + f )n12+ for nC2-
#rite $ program using d;1&hi.e to ca.cu.ate and print the first m fi%onacci
num%ers-
-'start
.'Rea% NmM
0'i!itiali?e i30/a34/"3-'
5'rite a/"'
6'if(iC3m* the!
8' "egi!
9' #3a)"7
:' Write #
;' a3"7
-4 "3#7
-- i))7
-. goto step 6'
-0 e!% if
-5' Otherise stop'
--
COMPUTER PROGRAMMING LAB
Program-11 :
Aim : 11- #rite $ programs to print the fo..o&ing outputs using for .oop-
1 1
2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4 4 4
Program-11(a):
Aim : Program to the Tria!gle Qormat'
Ri!#lu%eCst%io'hB
mai!(*
F
i!t i/H/!7
#lrs#r(*7
pri!tf(Pe!ter a!& !um"erS!P*7
s#a!f(PE%P/O!*7
for(i347iC3!7i))*
F
for(H3-7HC3i7H))*
F
pri!tf(P E%P/i*7
G
pri!tf(PS!P*7
G
get#h(*7
G
+1 @AMPLE OUTPUT
e!ter a!& !um"er
6
-
. .
0 0 0
5 5 5 5
1+
-.
COMPUTER PROGRAMMING LAB
Program-11(b):
Aim : Program to pri!t ETuilateral Tria!gle Qormat
Ri!#lu%eCst%io'hB
mai!(*
F
i!t i/H/K/!7
#lrs#r(*7
pri!tf(Pe!ter a!& !um"erS!P*7
s#a!f(PE%P/O!*7
for(i3-7iC3!7i))*
F
for(H307HBi,-7H,,*
F
pri!tf(P P*7
G
for(K3-7KC3i7K))*
F
pri!tf(P E%P/i*7
G
pri!tf(PS!P*7
G
get#h(*7
G
+1 @AMPLE OUTPUT
e!ter a!& !um"er
6
-
. .
0 0 0
5 5 5 5 1+
-0
COMPUTER PROGRAMMING LAB
Program-12:
Aim : #rite a $ program to extract a portion of a character string and print the
extracted string- 'ssume that m characters are extracted starting &ith the
nth character
Ri!#lu%eCst%io'hB
mai!(*
F
#har strI64J/su"strI04J7
i!t m/!/i/H7
#lrs#r(*7
pri!tf(PE!ter the Origi!al @tri!gS!S!P*7
flushall(*7
gets(str*7
pri!tf(PS!E!ter the positio! a!% !um"er of #hara#ters to "e e$tra#te% from the stri!gS!P*7
s#a!f(PE%E%P/Om/O!*7
for(H3-/i347HC3!7m))/H))/i))*
su"strIiJ3strIm,-J7
su"strIiJ3LS4L7
pri!tf(PS!E$tra#te% @tri!g is A EsP/su"str*7
get#h(*7
G
+1 @ample Output A
E!ter the Origi!al @tri!g
Uig!a! I!st' Of Te#h' O @#ie!#e
E!ter the positio! a!% !um"er of #hara#ters to "e e$tra#te% from the stri!g
0
-4
E$tra#te% @tri!g is A g!a! I!st'
1+
-5
COMPUTER PROGRAMMING LAB
Program-13:
Aim : ' :aruthi $ar dea.er maintains a record of sa.es of various vehic.es in the
fo..o&ing form D
Vehicle type Month of Sales Price (s!.
:aruthi E A;; ;2/A7 752;;;
:aruthi E 9* ;7/A7 F52;;;
="ps" ;4/AA 121;2;;;
:aruthi Gan ;A/AA A52;;;
#rite a $ program to read this data into a ta%.e of strings and output the detai.s
of a particu.ar vehic.e so.d during a specified period- he program shou.d re@uest
the user to input the vehic.e t"pe and the period )starting month / ending month+-
Ri!#lu%eCst%io'hB
stru#t #ar
F
#har !ameI54J7
i!t mm/&&7
i!t pri#e7
GsI54J7
mai!(*
F
i!t sm/em/i34/#ou!t347
#har #h/#V!ameI.4J7
#lrs#r(*7
%o
F
flushall(*7
pri!tf(PE!ter the t&pe of the #arS!P*7
gets(sIiJ'!ame*7
pri!tf(PMo!th / &ear a!% #ost of pur#hasi!gS!P*7
s#a!f(PE%E%E%P/OsIiJ'mm/OsIiJ'&&/OsIiJ'pri#e*7
pri!tf(PWo U Wa!t to Co!ti!ue'''(press X + N*S!P*7
flushall(*7
#h3get#har(*7
i))7
#ou!t))7
Ghile(#h33LXL*7
-6
COMPUTER PROGRAMMING LAB
pri!tf(PCAR TXPE MM+XX PRICES!P*7
for(i347iC#ou!t7i))*
F
pri!tf(PEs E%+E% E%S!P/sIiJ'!ame/sIiJ'mm/sIiJ'&&/sIiJ'pri#e*7
G
pri!tf(PE!ter #ar t&pe/ starti!g a!% e!%i!g mo!th of sol%S!P*7
flushall(*7
gets(#V!ame*7
s#a!f(PE%E%P/Osm/Oem*7
pri!tf(PCAR TXPE MM+XX PRICES!P*7
for(i347iC#ou!t7i))*
F
if(str#mp(sIiJ'!ame/#V!ame*334 OO (sIiJ'mmB3sm OOsIiJ'mmC3em**
pri!tf(PEs E%+E% E%S!P/sIiJ'!ame/sIiJ'mm/sIiJ'&&/sIiJ'pri#e*7
G
get#h(*7
G
+1 @AMPLE OUTPUT
E!ter the t&pe of the #ar
Maruthi,:44
Mo!th / &ear a!% #ost of pur#hasi!g
.
;:
.0444
Wo U Wa!t to Co!ti!ue'''(press X + N*
X
E!ter the t&pe of the #ar
Ua!
Mo!th / &ear a!% #ost of pur#hasi!g
5
:;
56444
Wo U Wa!t to Co!ti!ue'''(press X + N*
N
CAR TXPE MM+XX PRICE
Maruthi,:44 .+;: .0444
Ua! 5+:; 04444
Maruthi,:44 6+;; 0-444
E!ter #ar t&pe/ starti!g a!% e!%i!g mo!th of sol%
Maruthi,:44
-
9
CAR TXPE MM+XX PRICE
Maruthi,:44 .+;: .0444
Maruthi,:44 5+;; 0-444 1+
-8
COMPUTER PROGRAMMING LAB
Program-14:
Aim : #rite a function that &i.. scan a character string passed as an argument
and covert a.. .o&er case characters into their upper case e@uiva.ents-
voi% stri!gVu(#har 1s*
F
hile(1s23LS4L*
F
if(1sB3LaL OO 1sC3L?L*
1s31s,0.7
s))7
G
G
mai!(*
F
#har strI04J7
#lrs#r(*7
pri!tf(PE!ter A!& @tri!gS!P*7
gets(str*7
stri!gVu(str*7
pri!tf(PUppar Case @tri!g Is A EsP/str*7
get#h(*7
G
+1@AMPLE OUTPUT
E!ter A!& @tri!g
vig!a!
Uppar Case @tri!g Is A UIGNAN
1+
-9
COMPUTER PROGRAMMING LAB
Program-15(i):
Aim : IMPLEMENTATION OQ @TACY U@ING ARRAX@'
Algorithm for Push operation:
-'voi% push(i!t $*
.'F
0' if(top33MAZ,-*
5' F
6' pri!tf(P@TACY I@ OUERQLOWS!P*7
8' retur!7
9' G
:' sta#KI))topJ3$7
;' G
Algorithm for Pop operation:
-'i!t pop(*
.'F
0' if(top33,-*
5' F
6' pri!tf(P@TACY I@ UNWERQLOWS!P*7
8' retur!(,-*7
9' G
:' retur!(sta#KItop,,J*7
;'G
Algorithm for stacktop operation:
-'i!t sta#Ktop(*
.'F
0' if(top33,-*
5' F
6' pri!tf(P@TACY I@ UNWERQLOWS!P*7
8' retur!(,-*7
9' G
:' retur!(sta#KItopJ*7
;'G
Algorithm for Display operation:
-'voi% %ispla&(*
.'F
0' i!t i7
5' if(top33,-*
6' F
8' pri!tf(P@TACY I@ UNWERQLOWS!P*7
9' retur!7
:' G
;' for(i3top7iB347i,,*
-4' pri!tf(PE%StP/sta#KIiJ*7
--'G
-:
COMPUTER PROGRAMMING LAB
Program-15(ii):
Aim :Impleme!tatio! of Li!ear [ueue usi!g arra&s
Algorithm for insert operation:
-'voi% i!sert(i!t $*
.'F
0' if(rear33MAZ,-*
5' F
6' pri!tf(P Ove%rfloS!P*7
8' retur!7
9' G
:' TueueI))rearJ3$7
;' G
Algorithm for delete operation:
-'i!t %elete(*
.'F
0'if(rearCfro!t*
5'F
6' pri!tf(P[ueue Is U!%erfloS!P*7
8' retur!(4*7
9' G
:' retur!(TueueIfro!t,,J*7
; G
Algorithm for Display operation:
-' voi% %ispla&(*
.' F
0' i!t i7
5' for(i347iC3rear7i))*
6' pri!tf(PE%StP/TueueIiJ*7
8'G
Program-15(iii):
-;
COMPUTER PROGRAMMING LAB
Aim :Impleme!tatio! of Cir#ular [ueue usi!g arra&s
Algorithm for insert operation:
-'voi% #Vi!sert(i!t $*
.'F
0'if(rear33MAZ,-*
5' rear347
6' else
8' ))rear7
9' if(rear33fro!t*
:'F
;' pri!tf(P Ove%rfloS!P*7
-4' rear,,7
--' retur!7
-.' G
-0' #TIrearJ3$7
-5' G
Algorithm for delete operation:
-'i!t #V%elete(*
.'F
0' if(rear33fro!t*
5' F
6' pri!tf(PCir#ular [ U!%erfloS!P*7
8' retur!(4*7
9' G
:' if(fro!t33MAZ,-*
;' fro!t347
-4' else
--' ))fro!t7
-.' retur!(#TIfro!tJ*7
-0'G
Algorithm for Display operation:
-'voi% #V%ispla&(*
.'F
0' i!t i7
5' if(fro!t33MAZ,-*
6' i347
8' else
9' i3fro!t)-7
:' for(7i23rear7i3(i)-*EMAZ*
;' pri!tf(PE%StP/#TIiJ*7
-4' pri!tf(PE%P/#TIiJ*7
G
.4
COMPUTER PROGRAMMING LAB
Program-16:
Aim : - <mp.ement %inar" search tree using .in3ed .ist and perform the fo..o&ing
operations-
i+ <nsertion ii+ 9e.etion iii+ <norder raversa. iv+ Preorder raversa.
v+ Post Hrder raversa.
Ri!#lu%eCst%io'hB
stru#t B@T
F
stru#t B@T 1l7
i!t %ata7
stru#t B@T 1r7
G7
t&pe%ef stru#t B@T NOWE7
NOWE 1root3NULL/1temp/1prev/1#ur/1!e7
voi% #reate(*7
voi% i!sert(*7
voi% %elete(i!t*7
voi% i!or%er(NOWE 1*7
voi% preor%er(NOWE 1*7
voi% postor%er(NOWE 1*7
mai!(*
F
i!t #hoi#e/$7
#lrs#r(*7
pri!tf(PIMPLEMENTATION OQ BINARX @EARCD TREE S!P*7
hile(-*
F
pri!tf(PS!-'CreateS!.'I!sertS!0'WeleteS!5'I!or%erS!6'Preor%erS!8'PostorerS!9'E$itS!P*7
pri!tf(PE!ter Xour Choi#e A P*7
s#a!f(PE%P/O#hoi#e*7
sit#h(#hoi#e*
F
#ase -A #reate(*7
"reaK7
#ase .A temp3(stru#t B@T 1*(mallo#(si?eof(stru#t B@T***7
pri!tf(PWhi#h Ualue U Wa!t to E!ter A P*7
s#a!f(PE%P/O$*7
temp,Bl3NULL7
temp,B%ata3$7
temp,Br3NULL7
i!sert(*7
"reaK7
#ase 0A pri!tf(PWhi#h Eleme!t U Wa!t To Welete Qrom The B@T A P*7
.-
COMPUTER PROGRAMMING LAB
s#a!f(PE%P/O$*7
%elete($*7
"reaK7
#ase 5A pri!tf(PResult Of I!or%er Traversal Is A S!P*7
i!or%er(root*7
"reaK7
#ase 6A pri!tf(PResult Of Preor%er Traversal Is A S!P*7
preor%er(root*7
"reaK7
#ase 8A pri!tf(PResult Of Postor%er Traversal Is A S!P*7
postor%er(root*7
"reaK7
#ase 9A e$it(4*7
%efaultA
pri!tf(PI!vali% #hoi#eS!P*7
G
G
get#h(*7
G
voi% #reate(*
F
i!t #/$7
%o
F
temp3(stru#t B@T 1*(mallo#(si?eof(stru#t B@T***7
pri!tf(PWhi#h Ualue U Wa!t to E!ter A P*7
s#a!f(PE%P/O$*7
temp,Bl3NULL7
temp,B%ata3$7
temp,Br3NULL7
if(root33NULL*
F
root3temp7
G
else
F
prev3#ur3root7
hile(#ur23NULL*
F
prev3#ur7
if(temp,B%ataC#ur,B%ata*
#ur3#ur,Bl7
else
#ur3#ur,Br7
G
if(temp,B%ataCprev,B%ata*
..
COMPUTER PROGRAMMING LAB
prev,Bl3temp7
else
prev,Br3temp7
G
pri!tf(PWo U Wa!t To Co!t'''Press,B- + 4 A P*7
s#a!f(PE%P/O#*7
G
hile(#33-*7
G
voi% i!sert(*
F
if(root33NULL*
F
root3temp7
G
else
F
#ur3root7
hile(#ur23NULL*
F
prev3#ur7
if(temp,B%ataC#ur,B%ata*
#ur3#ur,Bl7
else
#ur3#ur,Br7
G
if(temp,B%ataCprev,B%ata*
prev,Bl3temp7
else
prev,Br3temp7
G
G
voi% %elete(i!t $*
F
i!t fou!%347
temp3root7
hile(-*
F
if(temp33NULL*
"reaK7
if($33temp,B%ata*
F
fou!%3-7
"reaK7
G
if($Ctemp,B%ata*
.0
COMPUTER PROGRAMMING LAB
temp3temp,Bl7
else
temp3temp,Br7
G
if(fou!%334*
F
pri!tf(PE% Eleme!t Not Qou!% I! a Bi!ar& @ear#h TreeS!P/$*7
retur!7
G
#ur3root7
hile($23#ur,B%ata*
F
prev3#ur7
if($C#ur,B%ata*
#ur3#ur,Bl7
else
#ur3#ur,Br7
G
if(#ur,Bl33NULL OO #ur,Br33NULL*
F
if(prev,Bl33#ur*
prev,Bl3NULL7
else
prev,Br3NULL7
G
if(#ur,Bl33NULL OO #ur,Br23NULL*
F
if(prev,Bl33#ur*
prev,Bl3#ur,Br7
else
prev,Br3#ur,Br7
G
if(#ur,Bl23NULL OO #ur,Br33NULL*
F
if(prev,Br33#ur*
prev,Br3#ur,Bl7
else
prev,Bl3#ur,Bl7
G
if(#ur,Bl23NULL OO #ur,Br23NULL*
F
if(#ur33root*
root3root,Br7
temp3#ur,Br7
hile(temp,Bl23NULL*
temp3temp,Bl7
temp,Bl3#ur,Bl7
if(#ur23root*
.5
COMPUTER PROGRAMMING LAB
F
if(prev,Bl33#ur*
prev,Bl3prev,Bl,Br7
else
prev,Br3prev,Br,Br7
G
G
retur!7
G
voi% i!or%er(NOWE 1temp*
F
if(temp23NULL*
F
i!or%er(temp,Bl*7
pri!tf(P,BE% P/temp,B%ata*7
i!or%er(temp,Br*7
G
G
voi% preor%er(NOWE 1temp*
F
if(temp23NULL*
F
pri!tf(P,BE% P/temp,B%ata*7
preor%er(temp,Bl*7
preor%er(temp,Br*7
G
G
voi% postor%er(NOWE 1temp*
F
if(temp23NULL*
F
postor%er(temp,Bl*7
postor%er(temp,Br*7
pri!tf(P,BE% P/temp,B%ata*7
G
G
.6
COMPUTER PROGRAMMING LAB
+1 @AMPLE OUTPUT
IMPLEMENTATION OQ BINARX @EARCD TREE
-'Create .'I!sert 0'Welete 5'I!or%er 6'Preor%er 8'Postorer 9'E$it
E!ter Xour Choi#e A -
Whi#h Ualue U Wa!t to E!ter A 5
Wo U Wa!t To Co!t'''Press,B- + 4 A -
Whi#h Ualue U Wa!t to E!ter A .
Wo U Wa!t To Co!t'''Press,B- + 4 A -
Whi#h Ualue U Wa!t to E!ter A 0
Wo U Wa!t To Co!t'''Press,B- + 4 A 4
-'Create .'I!sert 0'Welete 5'I!or%er 6'Preor%er 8'Postorer 9'E$it
E!ter Xour Choi#e A5
Result Of I!or%er Traversal Is A
,B. ,B0 ,B5
-'Create
.'I!sert
0'Welete
5'I!or%er
6'Preor%er
8'Postorer
9'E$it
E!ter Xour Choi#e A .
Whi#h Ualue U Wa!t to E!ter A 8
-'Create
.'I!sert
0'Welete
5'I!or%er
6'Preor%er
8'Postorer
9'E$it
E!ter Xour Choi#e A 6
Result Of Preor%er Traversal Is A
,B5 ,B. ,B- ,B8
-'Create
.'I!sert
0'Welete
5'I!or%er
6'Preor%er
8'Postorer
9'E$it
E!ter Xour Choi#e A 9
1+
Program-17(i):
.8
COMPUTER PROGRAMMING LAB
Aim :IMPLEMENTATION OQ @INGLE LINYEW LI@T
Ri!#lu%eCst%io'hB
stru#t @LL
F
i!t %ata7
stru#t @LL 1!e$t7
G7
t&pe%ef stru#t @LL NOWE7
NOWE 1start/1e!%/1temp/1prev/1#ur7
voi% #reate(*7
voi% a%%(*7
voi% a%%V"eg(*7
voi% a%%Ve!%(*7
voi% a%%Vmi%(*7
voi% %ispla&(*7
voi% %el(*7
voi% %elV"eg(*7
voi% %elVe!%(*7
voi% %elVmi%(*7
mai!(*
F
i!t #hoi#e7
#lrs#r(*7
pri!tf(PIMPLEMENTATION OQ @INGLE LINYEW LI@T S!P*7
hile(-*
F
pri!tf(P -'CREATES! .'AWWS! 0'WELETES! 5'WI@PLAXS! 6'EZITS!P*7
pri!tf(PE!ter Xour Choi#e A P*7
s#a!f(PE%P/O#hoi#e*7
sit#h(#hoi#e*
F
#ase -A #reate(*7
"reaK7
#ase .Aa%%(*7
"reaK7
#ase 0A%el(*7
"reaK7
#ase 5Apri!tf(PList Eleme!ts Are AS!P*7
%ispla&(*7
"reaK7
#ase 6Ae$it(*7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
get#h(*7
G
voi% #reate(*
.9
COMPUTER PROGRAMMING LAB
F
#har #hoi#e7
i!t $7
start3NULL7
%o
F
pri!tf(PE!ter the %ata valueA P*7
s#a!f(PE%P/O$*7
temp3mallo#(si?eof(NOWE**7
temp,B%ata3$7
temp,B!e$t3NULL7
if(start33NULL*
F
start3temp7
e!%3start7
G
else
F
e!%,B!e$t3temp7 e!%3temp7
G
pri!tf(PWo U Wa!t To Co!ti!ue''' press,BL&LAP*7
flushall(*7
#hoi#e3get#har(*7
G
hile(#hoi#e33L&L*7
G
voi% a%%(*
F
i!t $/#h7
pri!tf(PE!ter the %ata value to "e a%%e% A P*7
s#a!f(PE%P/O$*7
temp3mallo#(si?eof(NOWE**7
temp,B%ata3$7
temp,B!e$t3NULL7
pri!tf(PPostio! of Eleme!ts AS! -'Begi!i!gS! .'Mi%%leS! 0'E!%S!P*7
pri!tf(PI! Whi#h Lo#atio! U Wa!t To E!ter The Eleme!t A P*7
s#a!f(PE%P/O#h*7
sit#h(#h*
F
#ase -A a%%V"eg(*7
"reaK7
#ase .A a%%Vmi%(*7
"reaK7
#ase 0A a%%Ve!%(*7
"reaK7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
.:
COMPUTER PROGRAMMING LAB
voi% a%%V"eg(*
F
temp,B!e$t3start7
start3temp7
G
voi% a%%Vmi%(*
F
i!t i3-/pos7
pri!tf(PI! Whi#h positio! U Wa!t To E!ter A P*7
s#a!f(PE%P/Opos*7
prev3#ur3start7
hile(iCpos*
F
if(#ur,B!e$t23NULL*
F
prev3#ur7
#ur3#ur,B!e$t7
G
if(#ur33NULL*
"reaK7
i))7
G
if(i23pos*
F
pri!tf(PI!vali% Positio!S!P*7
retur!7
G
prev,B!e$t3temp7
temp,B!e$t3#ur7
G
voi% a%%Ve!%(*
F
for(e!%3start7e!%,B!e$t23NULL7e!%3e!%,B!e$t*7
e!%,B!e$t3temp7
G
voi% %el(*
F
i!t #h7
pri!tf(PPostio! of Eleme!ts To Be Welete%AS! -'Begi!i!gS! .'Mi%%leS! 0'E!%S!P*7
pri!tf(PQrom Whi#h Lo#atio! U Wa!t To Welete The Eleme!t A P*7
s#a!f(PE%P/O#h*7
sit#h(#h*
.;
COMPUTER PROGRAMMING LAB
F
#ase -A %elV"eg(*7
"reaK7
#ase .A %elVmi%(*7
"reaK7
#ase 0A %elVe!%(*7
"reaK7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
voi% %elV"eg(*
F
temp3start7
start3start,B!e$t7
temp,B!e$t3NULL7
free(temp*7
G
voi% %elVmi%(*
F
i!t i3-/pos7
pri!tf(PQrom Whi#h positio! U Wa!t To Welete A P*7
s#a!f(PE%P/Opos*7
prev3#ur3start7
hile(iCpos*
F
if(#ur,B!e$t23NULL*
F
prev3#ur7
#ur3#ur,B!e$t7
G
if(#ur33NULL*
"reaK7
i))7
G
if(i23pos*
F
pri!tf(PI!vali% Positio!S!P*7
retur!7
G
prev,B!e$t3#ur,B!e$t7
#ur,B!e$t3NULL7
free(#ur*7
G
voi% %elVe!%(*
04
COMPUTER PROGRAMMING LAB
F
for(prev3e!%3start7e!%,B!e$t23NULL7e!%3e!%,B!e$t*
prev3e!%7
prev,B!e$t3NULL7
free(e!%*7
G
voi% %ispla&(*
F
for(e!%3start7e!%23NULL7e!%3e!%,B!e$t*
pri!tf(P ,,B E% P/e!%,B%ata*7
pri!tf(PS!P*7
G
+1 @AMPLE OUTPUT
IMPLEMENTATION OQ @INGLE LINYEW LI@T

-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A -
E!ter the %ata valueA 6
Wo U Wa!t To Co!ti!ue''' press,BL&LA&
E!ter the %ata valueA 8
Wo U Wa!t To Co!ti!ue''' press,BL&LA&
E!ter the %ata valueA 9
Wo U Wa!t To Co!ti!ue''' press,BL&LA!
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 5
List Eleme!ts Are A
,,B 6 ,,B 8 ,,B 9
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 6
1+
Program-17(ii):
0-
COMPUTER PROGRAMMING LAB
Aim : IMPLEMENTATION OQ WOUBLX LINYEW LI@T
Ri!#lu%eCst%io'hB
stru#t WLL
F
stru#t WLL 1left7
i!t %ata7
stru#t WLL 1right7
G7
t&pe%ef stru#t WLL NOWE7
NOWE 1start/1e!%/1temp/1#ur7
voi% #reate(*7
voi% a%%(*7
voi% a%%V"eg(*7
voi% a%%Ve!%(*7
voi% a%%Vmi%(*7
voi% %ispla&(*7
voi% %el(*7
voi% %elV"eg(*7
voi% %elVe!%(*7
voi% %elVmi%(*7
mai!(*
F
i!t #hoi#e7
#lrs#r(*7
pri!tf(PIMPLEMENTATION OQ WOUBLE LINYEW LI@T S!P*7
hile(-*
F
pri!tf(P -'CREATES! .'AWWS! 0'WELETES! 5'WI@PLAXS! 6'EZITS!P*7
pri!tf(PE!ter Xour Choi#e A P*7
s#a!f(PE%P/O#hoi#e*7
sit#h(#hoi#e*
F
#ase -A #reate(*7
"reaK7
#ase .Aa%%(*7
"reaK7
#ase 0A%el(*7
"reaK7
#ase 5Apri!tf(PList Eleme!ts Are AS!P*7
%ispla&(*7
"reaK7
#ase 6Ae$it(*7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
get#h(*7
G
0.
COMPUTER PROGRAMMING LAB
voi% #reate(*
F
#har #hoi#e7
i!t $7
start3NULL7
%o
F
pri!tf(PE!ter the %ata valueA P*7
s#a!f(PE%P/O$*7
temp3mallo#(si?eof(NOWE**7
temp,Bleft3NULL7
temp,B%ata3$7
temp,Bright3NULL7
if(start33NULL*
F
start3temp7
e!%3start7
G
else
F
e!%,Bright3temp7
temp,Bleft3e!%7
e!%3temp7
G
pri!tf(PWo U Wa!t To Co!ti!ue''' press,BL&LAP*7
flushall(*7
#hoi#e3get#har(*7
G
hile(#hoi#e33L&L*7
G
voi% a%%(*
F
i!t $/#h7
pri!tf(PE!ter the %ata value to "e a%%e% A P*7
s#a!f(PE%P/O$*7
temp3mallo#(si?eof(NOWE**7
temp,Bleft3NULL7
temp,B%ata3$7
temp,Bright3NULL7
pri!tf(PPostio! of Eleme!ts AS! -'Begi!i!gS! .'Mi%%leS! 0'E!%S!P*7
pri!tf(PI! Whi#h Lo#atio! U Wa!t To E!ter The Eleme!t A P*7
s#a!f(PE%P/O#h*7
sit#h(#h*
00
COMPUTER PROGRAMMING LAB
F
#ase -A a%%V"eg(*7
"reaK7
#ase .A a%%Vmi%(*7
"reaK7
#ase 0A a%%Ve!%(*7
"reaK7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
voi% a%%V"eg(*
F
if(start33NULL*
start3temp7
else
F
temp,Bright3start7
start,Bleft3temp7
start3temp7
G
G
voi% a%%Vmi%(*
F
i!t i3-/pos7
pri!tf(PI! Whi#h positio! U Wa!t To E!ter A P*7
s#a!f(PE%P/Opos*7
#ur3start7
hile(iCpos*
F
if(#ur23NULL*
F
#ur3#ur,Bright7
G
if(#ur33NULL*
"reaK7
i))7
G
if(i23pos*
F
pri!tf(PI!vali% Positio!S!P*7
retur!7
G
temp,Bleft3#ur,Bleft7
temp,Bright3#ur7
#ur,Bleft,Bright3temp7
#ur,Bleft3temp7
G
05
COMPUTER PROGRAMMING LAB
voi% a%%Ve!%(*
F
for(e!%3start7e!%,Bright23NULL7e!%3e!%,Bright*7
e!%,Bright3temp7
temp,Bleft3e!%7
e!%3temp7
G
voi% %el(*
F
i!t #h7
pri!tf(PPostio! of Eleme!ts To Be Welete%AS! -'Begi!i!gS! .'Mi%%leS! 0'E!%S!P*7
pri!tf(PQrom Whi#h Lo#atio! U Wa!t To Welete The Eleme!t A P*7
s#a!f(PE%P/O#h*7
sit#h(#h*
F
#ase -A %elV"eg(*7
"reaK7
#ase .A %elVmi%(*7
"reaK7
#ase 0A %elVe!%(*7
"reaK7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
voi% %elV"eg(*
F
temp3start7
start3start,Bright7
temp,Bleft3temp,Bright3NULL7
free(temp*7
G
voi% %elVmi%(*
F
i!t i3-/pos7
pri!tf(PQrom Whi#h positio! U Wa!t To Welete A P*7
s#a!f(PE%P/Opos*7
#ur3start7
hile(iCpos*
F
if(#ur23NULL*
F
#ur3#ur,Bright7
G
if(#ur33NULL*
06
COMPUTER PROGRAMMING LAB
"reaK7
i))7
G
if(i23pos*
F
pri!tf(PI!vali% Positio!S!P*7
retur!7
G
temp3#ur7
#ur,Bleft,Bright3#ur,Bright7
#ur,Bright,Bleft3#ur,Bleft7
temp,Bleft3temp,Bright3NULL7
free(temp*7
G
voi% %elVe!%(*
F
for(e!%3start7e!%,Bright23NULL7e!%3e!%,Bright*7
temp3e!%7
e!%,Bleft,Bright3NULL7
temp,Bleft3temp,Bright3NULL7
e!%3e!%,Bleft7
free(temp*7
G
voi% %ispla&(*
F
for(e!%3start7e!%23NULL7e!%3e!%,Bright*
pri!tf(P ,,B E% P/e!%,B%ata*7
pri!tf(PS!P*7
G
+1 @AMPLE OUTPUT
08
COMPUTER PROGRAMMING LAB
IMPLEMENTATION OQ WOUBLE LINYEW LI@T
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A -
E!ter the %ata valueA -
Wo U Wa!t To Co!ti!ue''' press,BL&LA&
E!ter the %ata valueA .
Wo U Wa!t To Co!ti!ue''' press,BL&LA!
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 5
List Eleme!ts Are A
,,B - ,,B .
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A0
Postio! of Eleme!ts To Be Welete%A
-'Begi!i!g
.'Mi%%le
0'E!%
Qrom Whi#h Lo#atio! U Wa!t To Welete The Eleme!t A -
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 5
List Eleme!ts Are A
,,B .
-'CREATE
.'AWW
0'WELETE
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 6
1+
09
COMPUTER PROGRAMMING LAB
Program-18(i):
Aim :IMPLEMENTATION OQ @TACY U@ING @INGLE LINYEW LI@T
Ri!#lu%eCst%io'hB
stru#t @LL
F
i!t %ata7
stru#t @LL 1!e$t7
G7
t&pe%ef stru#t @LL NOWE7
NOWE 1start3NULL/1top/1temp/1prev7
voi% push(*7
i!t pop(*7
i!t sta#Ktop(*7
voi% %ispla&(*7
mai!(*
F
i!t #hoi#e/$7
#lrs#r(*7
pri!tf(PIMPLEMENTATION OQ @TACY U@ING @INGLE LINYEW LI@T S!P*7
hile(-*
F
pri!tf(P -'PU@DS! .'POPS! 0'@TACYTOPS! 5'WI@PLAXS! 6'EZITS!P*7
pri!tf(PE!ter Xour Choi#e A P*7
s#a!f(PE%P/O#hoi#e*7
sit#h(#hoi#e*
F
#ase -A
temp3mallo#(si?eof(NOWE**7
pri!tf(PWhi#h eleme!t U Wa!t To Push i!to the sta#KA P*7
s#a!f(PE%P/Otemp,B%ata*7
temp,B!e$t3NULL7
push(*7
"reaK7
#ase .A
$3pop(*7
if($234*
pri!tf(PPoppe% Eleme!t is A E%S!P/$*7
"reaK7
#ase 0A
$3sta#Ktop(*7
if($234*
pri!tf(PTop Of The @ta#K Eleme!t is A E%S!P/$*7
"reaK7
0:
COMPUTER PROGRAMMING LAB
#ase 5A pri!tf(PList Of @ta#K Eleme!ts Are AS!P*7
%ispla&(*7
"reaK7
#ase 6A e$it(4*7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
get#h(*7
G
voi% push(*
F
if(top33NULL*
F
top3temp7
G
else
F
temp,B!e$t3top7
top3temp7
G
G
i!t pop(*
F
i!t $7
if(top33NULL*
F
pri!tf(P@ta#K Is Empt&S!P*7
retur!(4*7
G
temp3top7
$3temp,B%ata7
top3top,B!e$t7
temp,B!e$t3NULL7
free(temp*7
retur!($*7
G
i!t sta#Ktop(*
F
i!t $7
if(top33NULL*
F
pri!tf(P@ta#K Is Empt&S!P*7
retur!(4*7
G
$3top,B%ata7
retur!($*7
G
0;
COMPUTER PROGRAMMING LAB
voi% %ispla&(*
F
if(top33NULL*
F
pri!tf(P@ta#K Is Empt&S!P*7
retur!7
G
for(temp3top7temp23NULL7temp3temp,B!e$t*
pri!tf(P E% C,, P/temp,B%ata*7
pri!tf(PS!P*7
G
+1 @AMPLE OUTPUT
IMPLEMENTATION OQ @TACY U@ING @INGLE LINYEW LI@T
-'PU@D
.'POP
0'@TACYTOP
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A -
Whi#h eleme!t U Wa!t To Push i!to the sta#KA -
-'PU@D
.'POP
0'@TACYTOP
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A -
Whi#h eleme!t U Wa!t To Push i!to the sta#KA .
-'PU@D
.'POP
0'@TACYTOP
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A5
List Of @ta#K Eleme!ts Are A
. C,, - C,,
-'PU@D
.'POP
0'@TACYTOP
5'WI@PLAX
6'EZIT
E!ter Xour Choi#e A 6
1+
54
COMPUTER PROGRAMMING LAB
Program-18(ii):
Aim :IMPLEMENTATION OQ LINEAR [UEUE U@ING @INGLE LINYEW LI@T
Ri!#lu%eCst%io'hB
stru#t @LL
F
i!t %ata7
stru#t @LL 1!e$t7
G7
t&pe%ef stru#t @LL NOWE7
NOWE 1rear3NULL/1fro!t3NULL/1temp7
voi% i!sert(*7
i!t %elete(*7
voi% %ispla&(*7
mai!(*
F
i!t #hoi#e/$7
#lrs#r(*7
pri!tf(PIMPLEMENTATION OQ LINEAR [UEUE U@ING @INGLE LINYEW LI@T S!P*7
hile(-*
F
pri!tf(PS!-'IN@ERTS!.'WELETES!0'WI@PLAXS!5'EZITS!P*7
pri!tf(PE!ter Xour Choi#e A P*7
s#a!f(PE%P/O#hoi#e*7
sit#h(#hoi#e*
F
#ase -A i!sert(*7
"reaK7
#ase .A $3%elete(*7
if($234*
pri!tf(PEleme!t Welete% Qrom The [ueue Is A E%P/$*7
"reaK7
#ase 0A
%ispla&(*7
"reaK7
#ase 5Ae$it(4*7
%efaultA pri!tf(PI!vali% Choi#eS!P*7
G
G
get#h(*7
G
voi% i!sert(*
5-
COMPUTER PROGRAMMING LAB
F
i!t $7
temp3mallo#(si?eof(NOWE**7
if(temp33NULL*
F
pri!tf(P[ueue Is OverfloS!P*7
retur!7
G
pri!tf(PE!ter the %ata value to "e i!serte% i!to the [ueue A P*7
s#a!f(PE%P/O$*7
temp,B%ata3$7
temp,B!e$t3NULL7
if(fro!t33NULL*
F
fro!t3rear3temp7
G
else
F
rear,B!e$t3temp7
rear3temp7
G
G
i!t %elete(*
F
i!t $7
if(fro!t33NULL*
F
pri!tf(P[ueue is U!%erfloS!P*7
retur!(4*7
G
temp3fro!t7
$3temp,B%ata7
fro!t3fro!t,B!e$t7
temp,B!e$t3NULL7
free(temp*7
retur!($*7
G
5.
COMPUTER PROGRAMMING LAB
voi% %ispla&(*
F
if(fro!t33NULL*
F
pri!tf(P[ueue Is Empt&S!P*7
G
else
F
pri!tf(PList of [ueue Eleme!ts areA S!P*7
for(temp3fro!t7temp23NULL7temp3temp,B!e$t*
pri!tf(P ,,B E% P/temp,B%ata*7
pri!tf(PS!P*7
G
G
+1 @AMPLE OUTPUT
IMPLEMENTATION OQ LINEAR [UEUE U@ING @INGLE LINYEW LI@T
-'IN@ERT
.'WELETE
0'WI@PLAX
5'EZIT
E!ter Xour Choi#e A -
E!ter the %ata value to "e i!serte% i!to the [ueue A -
-'IN@ERT
.'WELETE
0'WI@PLAX
5'EZIT
E!ter Xour Choi#e A -
E!ter the %ata value to "e i!serte% i!to the [ueue A .
-'IN@ERT
.'WELETE
0'WI@PLAX
5'EZIT
E!ter Xour Choi#e A -
E!ter the %ata value to "e i!serte% i!to the [ueue A 0
-'IN@ERT
.'WELETE
0'WI@PLAX
5'EZIT
E!ter Xour Choi#e A 0
List Of [ueue Eleme!ts Are A
,,B - ,,B . ,,B 0
-'IN@ERT
.'WELETE
0'WI@PLAX
5'EZIT
E!ter Xour Choi#e A 5 1+
Program-1 :
50
COMPUTER PROGRAMMING LAB
Aim:<mp.ement the fo..o&ing sorting techni@ues-
i+ ,u%%.e sort ii+ <nsertion Sort iii+ Iuic3 Sort iv+ !eap Sort-
Program-1(i):
Aim :Algorithm for Bu""le @ort Te#h!iTue
-'@tart
.'Rea% N!M eleme!ts to "e sorte%'
0'@ort the eleme!ts "& usi!g folloi!g "u""le sort te#h!iTue'
5'Compare the first eleme!t ith se#o!% eleme!t if first eleme!t is greater tha! se#o!%
eleme!t the! sap the to eleme!ts a!% the! #ompare first ith thir% eleme!t a!%
sap the eleme!ts if first eleme!t is greater tha! thir% eleme!t'
Repeat the pro#ess for N!M eleme!ts'
6'Repeat step 5 N!,-M times'
8'No eleme!ts are ill "e i! sorte% or%er'
9'@top'
Program-1(ii):
Aim :Algorithm for I!sertio! @ort Te#h!iTue
-'@tart
.'Rea% N!M eleme!ts to "e sorte%'
0'@ort the eleme!ts "& usi!g folloi!g i!sertio! sort te#h!iTue'
5'Co!si%er o!e eleme!t i! the list a!% assume that the list is i! sorte% or%er'
6'TaKe .
!%
eleme!t a!% Compare se#o!% eleme!t ith all the eleme!ts i! the list'
A!% i!sert a! eleme!t i! a! appropriate pla#e i! the list'
8'Repeat step 6 for all eleme!ts'The! the list ill "e i! the sorte% or%er'
9''@top'
55
COMPUTER PROGRAMMING LAB
Program-1(iii):
Aim :Algorithm for [ui#K @ort Te#h!iTue
Algorithm for quick sort :
-'voi% Tui#KVsort(i!t left/i!t right*
.'F
0'i!t l/r/pivot7
5' pivot3aIleftJ7
6' l3left7
8' r3right7
9' hile(leftCright*
:' F
;' hile((leftCright* OO (aIrightJB3pivot**
-4' right,,7
--' if(left23right*
-.' F
-0' aIleftJ3aIrightJ7
-5' left))7
-6'G
-8' hile((leftCright* OO (aIleftJC3pivot**
-9' left))7
-:' if(left23right*
-;' F
.4' aIrightJ3aIleftJ7
.-' right,,7
..' G
.0' G
.5' aIleftJ3pivot7
.6' pivot3left7
.8' left3l7
.9' right3r7
.:' if(leftCpivot*
.;' Tui#KVsort(left/pivot,-*7
04' if(rightBpivot*
0-' Tui#KVsort(pivot)-/right*7
0.'G
56
COMPUTER PROGRAMMING LAB
Program-2! :
Aim: <mp.ement the fo..o&ing searching method-
i+Se@uentia. Search ii+ ,inar" Search
Program-2!(i) :
Aim :Algorithm for Li!ear @ear#h Te#h!iTue'
-'@tart
.'Rea% N!M eleme!ts'
0'Rea% NKe&M value to "e sear#he%'
5'Compare ea#h eleme!t of the list ith Ke& eleme!t/ if it is eTual to a!& o!e of the
eleme!t the! the "earc# i$ "ucce$$%u& a!% %ispla& the positio! of the Ke& eleme!t7
otherise "earc# i$ 'n$ucce$$%u&(
6'@top'
Program-2!(ii) :
Aim :Algorithm for Bi!ar& @ear#h Te#h!iTue
-'@tart
.'Rea% N!M eleme!ts'
0'Rea% NKe&M value to "e sear#he%'
5'@ort all the eleme!ts "& usi!g a!& o!e of the sorti!g te#h!iTue'
6'Appl& folloi!g "i!ar& sear#h te#h!iTue o! the Ke& eleme!t/ if the eleme!t is fou!%
the! the "earc# i$ "ucce$$%u& a!% %ispla& the positio! of the Ke& eleme!t7 otherise
"earc# i$ 'n$ucce$$%u&(
6'@top'
Algorithm for Binary Search :
-'i!t "i!Vsear#h(i!t Ke&*
.'F
0' i!t lo/high/mi%7
5' lo347
6' high3!,-7
8'hile(loC3high*
9' F
:' mi%3(lo)high*+.7
;' if(Ke&33aImi%J*
-4' retur!(mi%*7
--' else if(Ke&CaImi%J*
-.' high3mi%,-7
-0' else
-5' lo3mi%)-7
-6' G
-8' retur!(,-*7
-9' G
58
COMPUTER PROGRAMMING LAB
Program-21 :
Aim : i+ $onversion of <nfix expression to Postfix notation-
ii+ Simp.e expression eva.uator2 that can hand.e +212/ and 4-
Program-21(i) :
Aim : $onversion of <nfix expression to Postfix notation-
Ri!#lu%eCst%io'hB
Ri!#lu%eC#t&pe'hB
R%efi!e MAZ -44
#har sta#KIMAZJ7
i!t top3,-7
mai!(*
F
#har i!fi$IMAZJ/postfi$IMAZJ7
i!t ip/op7
#lrs#r(*7
ip3op3,-7
pri!tf(PE!ter the i!fi$ e$pressio!AS!P*7
s#a!f(PEsP/i!fi$*7
sta#KI))topJ3L(L7
hile(i!fi$I))ipJ23LS4L*
F
if(isalpha(i!fi$IipJ**
postfi$I))opJ3i!fi$IipJ7
else if(isoperator(i!fi$IipJ**
F
hile(pre#(i!fi$IipJ*C3pre#(sta#KItopJ* OO 2(i!fi$IipJ33L\LOOsta#KItopJ33L\L**
F
postfi$I))opJ3sta#KItop,,J7
G
sta#KI))topJ3i!fi$IipJ7
G
else if(i!fi$IipJ33L(L*
sta#KI))topJ3L(L7
else if(i!fi$IipJ33L*L*
F
hile(sta#KItopJ23L(L*
F
postfi$I))opJ3sta#KItop,,J7
G
top,,7
G
G
hile(topB4*
59
COMPUTER PROGRAMMING LAB
postfi$I))opJ3sta#KItop,,J7
postfi$I))opJ3LS4L7
pri!tf(PS!S!Es ETuivale!t Postfi$ E$pressio! Is A EsS!P/i!fi$/postfi$*7
get#h(*7
G
pre#(#har #h*
F
sit#h(#h*
F
#ase L\L A retur!(0*7
#ase L1L A
#ase L+L A
#ase LEL A retur!(.*7
#ase L)L A
#ase L,L A retur!(-*7
#ase L(L A retur!(4*7
G
G
isoperator(#har #h*
F
sit#h(#h*
F
#ase L\L A
#ase L1L A
#ase L+L A
#ase LEL A
#ase L)L A
#ase L,L A retur!(-*7
%efault A retur!(4*7
G
G
+1
@AMPLE OUTPUT
E!ter the i!fi$ e$pressio!A
((a)"1#*\(%,e**\(f+g*
((a)"1#*\(%,e**\(f+g* ETuivale!t Postfi$ E$pressio! Is A a"#1)%e,\fg+\
1+
5:
COMPUTER PROGRAMMING LAB
Program-21(ii):
Aim : Simp.e expression eva.uator2 that can hand.e +212/ and 4-'
"include#stdio.h$
Ri!#lu%eC#t&pe'hB
Ri!#lu%eCmath'hB
i!t #ompute(#har/i!t/i!t*7
mai!(*
F
#har postfi$I54J7
i!t sta#KI54J/top3,-7
i!t i3,-/op-/op./value7
#lrs#r(*7
pri!tf(PImpleme!tatio! Of Postfi$ E$pressio! Evaluatio! S!S!P*7
pri!tf(PE!ter The Uali% Postfi$ E$pressio! A P*7
flushall(*7
gets(postfi$*7
hile(postfi$I))iJ23LS4L*
F
if(is%igit(postfi$IiJ**
sta#KI))topJ3postfi$IiJ,L4L7
else
F
op.3sta#KItop,,J7
op-3sta#KItop,,J7
value3#ompute(postfi$IiJ/op-/op.*7
sta#KI))topJ3value7
G
G
pri!tf(PS!S!Result Of The Postfi$ E$p A Es Is A E%P/postfi$/sta#KItopJ*7
get#h(*7
G
i!t #ompute(#har #h/i!t a/i!t "*
F
sit#h(#h*
F
#ase L)L Aretur!(a)"*7
#ase L,L Aretur!(a,"*7
#ase L1L Aretur!(a1"*7
#ase L+L Aretur!(a+"*7
#ase LEL Aretur!(aE"*7
#ase L\L Aretur!((i!t*po(a/"**7
G
G
+1@AMPLE OUTPUT
Impleme!tatio! Of Postfi$ E$pressio! Evaluatio!
E!ter The Uali% Postfi$ E$pressio! A .0)5\
Result Of The Postfi$ E$p A .0)5\ Is A 8.6 1+
5;

Anda mungkin juga menyukai