DS Module 2
DS Module 2
MODULE
ropresertation
Stacks' Definition, stacle openattond, odcpreorlohir9p
Stacl appabors - pol'sh
Staka, staclea wstng dyname aag
post ks prenson,
ocatorn, Tofix to postx Corvevstor, EValuabon oP
Kecurston, Factotal, GED, bonacei Seguenee, Towes Hanoi,
Ackerman's fanckon. Queuei Defpator. Aay vepreserntaBon
Queue operab'oro, ctrcular queues,n Circulas qeues
dynamde anag, dequeue, prlrithy gueues, A mag Problem
Mulkpk stac tes and queue, prcfam mng exaye
Tezt bookI ch 3:- 3b3.7
Textboolea: ch6: 6-1 b 6"3,6-5, 6-7, 6-8, b- 1o, 6"12. 6-13
Stact operakiora'
elements are in seted oY deleted only at one end
calleo hp a dhe Btart.
Structure.
Overflow- check wbethex the stack s tell or not
Createl)
This Ph. tmplemenbed Stngle dimensona
SLSTAkS 2E];
Sgmbolie constant
arvay wsed b hold he elem ents
tbe stacl
dyned
STACS12E 5
int s[sTAcIN2);
Pash ( stach, bp .ma, ibem) n
Pop ( staet,
top,tbem)
0 dhen pnt indefow and eun
bp
a Se Ptem
stack[top]
3" Set top
op-i
Hirindging underfbaw
ovev flo w
the
depend exçlsney and
|p data, tbere ho crct Contl Ehe
Pngammen
Overflow depend upon the abitrang Chofce of tho
Pnghammey yhe amgunt
Cach Stact, h chore oloes nfuere the
nenbey g bnes
overfbw mag OCC
Reseruing
(O) ceereae the each Stack
ho n bmes overflow
OCcur (expensive
8pace is seldon sed)
Reserving
incese he no"
a Small amount
PF(Shacle ovevfiow")
elbe
ven tendel,
opera
element.
Ex pressiona'
The
Seçuence 9 operahos and opeanol that
Yeduce to a
Stnge Value ofte ewaluakon.
Repverentakon Preky
PostRx
In an express?on, if an Openab is fn behween 2
operand. It Can be panenthestzed unpanendheszad
a+b
(atb)
Post&y:
In an expreson, iP dhe opeah Rilows a operondo
npanenthes.s expre»stor Also Caled Sufb 0 Yevevse pol:sh
precedence Tles
Tales ane
PrecedeNe dhen
asocitakvthy
So Convest
3. (A+6) J(c-)
/+ Ae-cD
((A+(8-c) D)A E+ F)
(4e*-B DAE+F)
A+A - B(DE) +F)
Order
opeorda ore torne
orde y, operotos chomgs
presstrn'
Lot o be on ortthrnete esproston witer in igis
notakon.
hold opoas and byt
Parenthe»ts.
6’ I a vqh
(endegi)
aventhes s erounteveol dhen
)a) Repea tnlly pop an Sach
Stack anod aod to pench
bhe top stack) un) a legt
opeatv(on
paventhe's s enentered
o Remove the let
parentheaisclo not aoed ahe legt parerdhaa)
(eno gstep2)
The posthx
expreson p on be
contctd rom
lege to sigt wstg dhe operonolo
trom aa and the peahus
are
removed Rom Stack
H
A+(B C- (D |E^F) #G)
Push ''onto stach and add'' bdhe enol 2 a.
(+
(+(
(+(
(+(4
C (+(*
(+(
D
(+(
(+l-(
(+(-(!
(+(-/ ABrDE
(+(-(14 ABcDE
(+(-(19
(+l
(+(- 4
) (+(-*
(+
(+d
)
A+(B C- (D E^F) %G) H
A+ (6«c - (D | F ) c)* »
- (DEF 4I6 ) H
Iny bo postR:
I: ((A+ (&-c) D) ^ EF)
Y3^AM-N+po]+
abt c #de
Yisht
óperan
left
(
lef
)
I a
opeab is left as oc ta Fve, stack prececere
Value is Ilp Preceolence Valve.
1 an
operah s ght assoctabve, &tacle preceoerce
Valu ts 2 |p Preceolene Vale.
#
( Push ( -179
Pop A 8 > |
( Pusht O)
# (+ ( Push ( 2>9
#(+( B
t (+(8 Pop B 87|
# (+( (
(+( Push c 7
Pre By ho igs
Revevse the prekx exprerson
IP dhe Scanned chaYacker s cn ope-ond pesh i on
b dhe stacr.
I dhe scannec characte s an opeabr, deverse he
Operand l, Sopeand, dpeabr, o ope.and , ).
) -alb #c^de
’ (a-(b (cu (dae)))
a) -+ cbc
’ (( a+b) -c)
rek to postls
Revevse dhe prek epresorn
oprand, push tt ono he
IP dhe Scannel characte is an
Stack.
I dhe Scanned choracte s an
’abcde A /
) - alb + cAde
a) -+abc ’ab+¢ -
EValuokon ) expreston!
Operabs with highet preceslenee au evalateo rst
Expresfos ane always evaluateot hom dhe
innemes parendhesis exprexion t
postkx expres fon
Evaluakon 4posth
Consider P is an
aithmete exprer fon.
| ’ Ado a
vght panendheis) at the enod P(acts as a
Senkral)
SCan P Jnom left b ight and repeat step3 and
each element for
Pntounter.
(end g step a)
S’ Set Valu esual h the bp elernet on Stack
To
Smpy dhe task, assume expreston contains
y opeats t,-,*, l, 7: ad
that dhe opeanolo in he
eApreson ae
when opeand s pusheol on to dhe stack, ts
Asc Valus wN1 be
pushe
Stnce an
Opeand(
andl synbo') s initally aa charcter
Want b push the cor esponolg integ value achienes
subt>gmbol
racng - 'o' 'o (48 olectmas)
olectmas ) srom
trom an opeond
lample,
Symb'
Chararte ' has an AsÇ!
The statement Symol - 'o' Proouce
umbe
)
6a13- hatt
Symbol A stacl
6 6
6 a
6 3
3
W
3 3
3 3-3
) 8
Answe s 8
82| 3 - ha ( Inval
preston)
Recur
Re
3)
Aec
Answe = 31
Reult -24
)
Relt
Recurston
Recuvsve Procedure - p is a preceoure conto'ning e'th er Q
Call Btaement to itse call &latemeot to a Becond
Proredlure that eveatualy a call satement
result
back toD the on'ginal Procedue p. ps called a ree ussAue
Procedure. So that the progham Col not contonue to vun
indnibely
Prapnte,
J" Thee must be cer kain criteva Caled bese cteria,
Poy whth the proceclue oloe not cal iksel.
d Each Bime the pcelure does cal sey (oety
indvetiy), it must be clese dhe base cster'a.
Recursve funckon
A fo s Sard b be
a prope es,
J" Thee must be cestain
argment calle d bcae valles Ror
which the on does not refe to itse.
a. £ach kme the tn doe refen to itse,, the
O, Ebe t mwt be
argeument
close to a base Value.
FORTRAN, COBOL oloes nok
Yecurs on.
The prooluet of the poskve integers trom lton.n
h! -|3... (n-a)(n-)n
n!=n(n-)!
n0 nl)
4n0. n!:n(n-))
The fode Boed br all non
regake integen
levet
3 32)
leve
3! =3a= b
level q
The depth g
Oghs exeruton a pocedueps astqned leve|.
Verurstorn a Yerusive pocelure P wth a gn Bet 2 sumeni
b dhe may. evel nmbo p duvng :t execuHr.
fbonace Sequene'
D,, ), a,3,5,8, )3,2,3h. 55,..
Foo
beo s dhe um g dhe kwo jpececg tem
Each Bucredg
if n> then Fn= Fn-gtFn
6 retun