You are on page 1of 14
i Unit-2. STACK @ Concept ft Fx: A shadk 4a linen. dale struclwe 2 vihich av. chet met be. ae ov dele me yor end. te, The elenonts fom a stack revense order of that -m ahi > Stak feck pa wea il which porrls Gepmnst => “ 48 jretoinanlet le puehin (once 2) an hemor | ue Bee an aeging i ‘Sie porpg Cig) sad ; — A shack follows the principle afb itefil- asl re gale —> PUSH and POP anedvo sbume used for msertion and dol tp = " tp / operations am stack, vespecivel} ep D PUSH) spPUsH(8) — mb PUSH(c) ty) POP(C) y>Por(B) fi. pus pp operating sm slack. ; Applicatina of stack: > To wraluak the postfire and prefix expressions. >To keep the page- vietted, the m web browser. — To pertorre the undo Sequence ma text edther, > sell mM yecursion. ~> To check dhe correctness of parentheses once, > Te pose Hho porarneders beboeon Ihe fndions mc @ Siac AV: pho In Sta ADT (Abediad: Dota pe) we discuss abot oh cpoaltos thot can be porformedon stadk, Falloolyy ane. SU oboe Sn shock(S)—> Creale stad S whidr 13 mttoby 7 Sens Co 1h Fach, (S122)-2 Tree 2 of onzend of slack , called ais Hep ap Tonle 8 a nick emphys then reli, the ra ry foelS > szadk S 4 veh empty then delele tre clement ak 4x2 zp. BANE) > Sebearahe vfelhe, the allack § 44 full or nb 7 ~ fetwun true. 4S a ful rebum false othenotse. L ue kretl(S—> Detorrine whebho. tie shack Sw or note ann tess ES 10 9% emphynchan fale btbewtse Aeerazs senplernavdosign of sta (or shea ‘ Tri, traphensrdation indbbed u2e2, cna dimensional +o gore Bey ee Arig, senplernsrdpxion fps an lego vale . Zz dhe ae peation Ha sdack.Fach } «aoe, i042 snliernented, ov decrem : | Deed lete ed of cumenk ep of the aback / S3rarbine tor she: Re eh SL Atdefian mny.10 shod: shade planed mn eng do comp ; td tlomsinay] , {Declarin, to an po Pei ay 43 Marcld ck sla oly > Colyees of ty (ep F ore Upercitions for Stadic rmplemelabin afstade = Se See mrplemtaion of stock, | Greadins shack | * ee ee ee sndicades, the omply shck GC rnplerantabin) | wd snk ntl Ghul ladee5 [hoc berks ann | e.zop=—15 | iF Lop ) Stack cpby Onidatl Boel eke to Gia tak a top =-1) else 4 3 rebun 0; Wh Slack. Rall ov Owatlns This te the ethiadion whan! shack SETS fs ard No more Genes cake pled a the. 7 g the si docadtion af Ue Pee CE Peel he dhl a sult ‘losing incl will vehon rue. (464) f He stack © (60) otherwiee, mb ARE aa == MAaxsize-1) nds 3 rebon 0. s HAgorlhms for PUSH and por eratins (For Stabe olernordidbien). hed Stock Imaxsrze] San to Alemont He stack and, top Variable, clenoles Lig dsp ahd sled at K overflra Fle Set ada mos Seo ah) Sdepht Ca for stack overtlndsg aS : == MAXSIZE-1 ther er lack Ovefloo” and Exit He prgran elses srereage top by 4 08 Sel, dop=dop+ s: oRead ef nt to Te eed (say elemnt) Sheps: Sel, alark Coop = cane a eg noob “Slop Step Prd “Stack Underflrsand #2 Shiate payen | "Tooge te ne clemerc and. cet ths f the vorial “Seb thomende= 2 Sock eps M9 clecrernent 1 ag; 2: ¢ St tops dept oy Shep? : Bind “element? ao, dolled then fon the stack a She: Slop lish slemenbition of stack. waite namic tmplynenbbto,): ode » This mi Mice pate “tia ‘of Sue be So, & (Saeed 5 ah og ap any ne “hu Hes domed that s. cow JY 1 real Spuchnes strut: Inbel shack 7 adie bs a, ted cdack Meeks li $0 dypedes truck cia? slack NodeType, Arwkel slack “op, top= NUL; 8 Shack Operzclions: vaheh The PUSH curd, POP cpoutns ane performed on a, stack De clescrtbed ag PUSH spexaion — pale ek push creat +h gid 4p add or meert as ete add add, ft ee gae otack swe Breed we push ad > te lool ee put iy the stack 48 af Top 8 sneered when PUSH operation occur8 > PUSH operation. moeases size of shack. >We check. Overflas condihion ching PUSH speration. Example + wey i eb 28 a the oer . Shack Belo PusH Stack fe PusH (25) b pol whpoP ahve | vedion 18 vd co remove. or delele Bee dep elonerd fem alacke When we lon ts oped el opt fom slack. Phibhan an item 48 poped td fs ‘lem ithich 43 removee ~ Top 48 Jeceruntel loher Yon heap en > POP operation clecreases a af sa ck => We check Unda condition Al POP opercetion, Keomole (| Prortas | @ Graded Mellon (0R Debi Matching Pelion of stacks: One application of the shack 18 mynalching delimedus dn aprogreun.” No program 16 conetdered correch $V the debimtleys ae wiemalshed,In C peqram we have fallsing "+ aranthesta “C’ and ")”. * Square, brackels ‘~T and “S. . “Goby brackels ‘$” dnd“ , . o «Commend debinolers P® aw xy» Eearnplen: the opt of each delimeters : * te Co tl sap 0 chang orth dlneons ),3, 97 Tuk, x_and fo ax Nofation: ee: 4 the expt eee agliadin of Be ade ss dy evalake Dhret- ype fe apse re expression sn filoag “indy Infix Bxpression > Tf the operator 45 placed. behoeer ibs fur Speremes then tt is allel 9 x: notation, for eg At8; Gay OD, AXE ele Where, +), 4,» ane oneais, AyB 56,9, E on whi ; 4 Ms cone ne operancl, Relix expressin/Refe nababion» 1f she operator symbols placed before tds shoo operands thon. 18 4g from 28 prefix expression “ete +B, ¥ EE etc. : fosbhr. excpression.—> Tf the operator syrmbet 43 placed. aftr. fs vbeo operands ten shits Lynam BS postfix expression fer eg. AB+ CO ete. Note: Bob) prefix aund poslfix are. parenthesis free. expreggind, Forexample> (a4 8)eC [Fife fm] . ig CRefix fom AG+C* Chokle Stratlanly follosiyy Ane Some exaumples for snag A +8 Ag+ +AR A+R-C AB+C~ | —+ABC (A+8)* (c-ad)| ABTCD-% | F4A8-CD ® Convencion of one ex, obhen: CE geet D> Converting avr Indx expression om fet ggg Racleuefiles Before any Sie of comin, fre ve cabal Ho three levels sy precedence. a8 folloog:- b Dsponend (symlal or” fs ie Pee A, igor e. peer Uy Daillens and omy” ma be Pd P’ arty ‘Dh Addon ord Salle be bak patty. joer Braces, (Le, iLL Io cree ristthy, 20sobve pace [to f alt ee ree Hen ete acon or rrovmal, wathemabies snmphifeation pa te, / frst, those * and + and — finally « sample Coed tb om (0) *((02)8)/6 Posty sa se: sa sat (are) *((C-9) 48) /F Enh form] = (AR) (CD +E/E eampbe perfoos only me = GB)" (Cope ei oy ly tk =G@br)* (co-k YE =Ustco- -Pe*) JE = AB+CD-E+*P/ Chests fori] froceas? Rist convert He subexpression sp that 4 be evaluated vst and pl oles en ne subsdehule - endanedine posth x fh re selon, . Ot tunes way becauke ad rab > tcl we convert “Whe coe parenthesis be postfix, repulting as ayao operand . ai Porventhests should be Suecesor ely elimincdel, undil tre entre. . erpression 18, comvented, tthe tash pas 13 fo be opened wrthina group off "fact ca oe ian diane nyNeA Ww) ‘hia taal wnyfired—ou) behaviow uggs Ato use of shack, i Rul be ef, At(0-% \ nls fr emphasis AteBAett \ sis > A+(B*C) wh. Convo dhe scl Tig a AH@cr) % ee —> Alaa wy 05: rz m, Abe We solve Ip, p (Enis (A8)* (Ca) 48)/P Gini fore] = (Att) (€c-2)+E)/p = PEC O)LEVE eb Aa+=P) + = P(r +8 /F pre huh = pm See flat co-=4) =pH xP (S2ue/ 9 = Pe (ORE = POLLEY (fly He = Pet/e (Leb O&+=H) = POD-E+¥F/ (n=00- = PFYRS , FAR CO- EPR R/ = PstI (i HF/ =r) ‘ Ne subedehedog each, vabun ag tn igh Wt el postfix ag = ABECD-E+KE/ [hosbfix form] Converting an In resston to hefty expression: The recedence. rule, and process for convert from an roe before, | “ind ex, ressin fy posd ik Are came 24 we w Onl change 18 the operator 48 placed. before the opevands \ rather than after them. Method do eonverct . 4 ~ Bat seenal, vnethods, ome of them ray rnethed, as aay d Fired vevense the gven expression . tA Then conve, at tedo postfix ag we did before. md Agel finally revence ste expression to get prefcfom. Bearnplet, 2" /(5%2) +2. [Frade fom] Yerenrse_ Sxep ti 2+(2*5)/yrx E42: converd indo pos f 2t(2*5)/ Ay = 2+(25%// Age = zs ky ch FQ+Z5% SAS = 225%, Ragen Skee: apain veverse! ‘A Pedix = tay *522. Loepuirtd Rebix fon} @, Conversion. dp another. using, Stack: = spat) Converting cintix ty” nos x using Stack. eu notes le should rem enibe follsing some 5 While. converting sre db postfix ag ck ilkogl We wail) WAS, Ss the given e: reS Eloy eft 1 xfs zo , Bria ! gr eoales and firsd placed ] mM symbot tclumn. oe Gack pat in below fable will nly cotati operators and festhe » pot wtll confaty operands mye poped out oparaters from stack, welt -hi er prions (i, nrecedoy nator comes 4> cack shan the. ceed “3 atoll oe foe an shack Hora ox baie ® Buk # same. or lower ‘prietdy 0 cater comes te shack. ne p hig case. the tac! caer stack 43 prped ae Postfix. the precedence ¥% ag falles:— 4> Aes 247% Bot, ND Braces will vot be compared as ~higho, gy Incen precedence. but opening. cf brace will bel cempaned Gaile closing be brace Wh spared brace te clond an leback Ihe bere ot, volcan by | + (+ | A. 7 8 (= AB Z C/ Ab (ics) Cc ie AGC wer * (+x ABC/ ¢ (+*( ABC/ D (+*( ABC/D. a + | (+e ABC/D 7H E (+%(+ ARC/DE aN 2 (+5 ABC/TE+ since £7 = (= ARC /DE +E dese F = | ARC@E+> +E Lignan ) | ARC E FREER “ne pecan 60+ 18 pepe net ] pit rence J ven expression . Mowe convert 44 4, olf using shack as we did before. MH Again, we newense the P i Me saak ee EE Come tnltx (448% * @+8)-6) cto prefix using stack, , "(A+B - j Steph: sl we woe’ fo pan ; (F—(8+9)* Cf + A) Step2; Now we. convent Huis, reversed, expression to postfix. Stet Scanned Shack Posdfine ¢ [ F ( F = (- E Cl F E (<0 FE + (-(+ FE 2D - (-(+ PED r y (- FED % (-* FEo+ Cc (—* FED+C Y (Cy FED+C% r B (-/ FRD+C*B + . (-/+ FED +CxB C A . (-/+ FED+C*BA ) { FED+CHBA-/+ | Soedfix = PED +C0*BA—/+ Steps. Pally we reverse Hus postfix do pref form: +/-AB*C+DEF [reguived, pa . To evaluate poste "bok x ix Cx pression Usii stack. , Shaliten ol pile xpi Sirs 2] wees thet ey a Ban rin aluate the. As reselont Shept shack be velack (valu sdack), : ono me ‘fferade. ata time from deft + sight Slepd. rf season si : Correspondi nq value kn a eae eaten Ee ey X €Xpressio, loot 48 operand then read. elie and sch t4 arb vedack, Arf eer ayrabat 8 op erator then — pop awl, ple ribo {FF = ard place, mb apd. MP ude oot wate to THe Fk whe , : are push result inte yeh sen fe: Pop and display wl 48 sepa ve oft gen posttix od Trace. of Bvaluedhien: aia an -exareples tes eeen ne Seamed character] of? opt | Resull | vshack a a 8 = 2 : 1,2 é = 33753 4 C 3s 2 5 | 2° | % S |4 | 5 .\5 3 3 \53 ‘By 2 5372" 4 4 _| 73 2)4- = 4 la | 2 |saa | = 4 = \4 5) 4 [D— t # [5 [20 [a0] amt A evaluation cedure. wttl be, Same "Wl i ciffowee 5 Had frst WE Yevence iver expression ae, srakead of Song Seon Aol 4 do v4 Wwe, sean fom ‘rf de: eft) ‘and cse convert San Tin simply, wae athe we. = for peti 2 Realueln. of prefs expessin: | Er 2g 43—3%,9,2, 416,85 : Now we reverse this e: reseiom aince We aN vo : perform speration wf iL spetands are after apeoraters , $0) we. reverse ag tines 58,16, /, 272) % = + Te choy heb: ABCA 9 oe 4 ie Noo we proceed atria 5 we dl rp ostfie Peers Characher | wae Yond | opt [ opt et | Regutl vadeck : A 5 pete c 4 5S 7 a sot = 2 52/2 [ a |? | x 12/2/95, 7 2 | * sa3 = 4+ [oo [ 2 Be + | 25 *, He final value = % | (i

You might also like