0% found this document useful (0 votes)
40 views43 pages

Dsu CHP.3 Stack

DSU notes

Uploaded by

rashi doiphode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
40 views43 pages

Dsu CHP.3 Stack

DSU notes

Uploaded by

rashi doiphode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 43
. [Initialize stack +o be empty: “\Check %# +ne stack {6 empty ov not CuuneePtows) UNIT 3: STACKS AND QUEUES Data shucturte Uncox Non- Uneot Avroy Queue stack LINEAR DATA STRUCTURE: “In his, dota fs arcianged Tn Linear sequence - Data tems can be hanvertsed in a single yun- Th Unto dota shuctune elements ane accessed ow placed in contiguous _(+togethev in sequence) mamow tocotion. What is Stack 2 A stack is Called a last-in-finxt eur (LPO? | couection © “This means that the lost thing we added (pushed) ic the fist thing +nat gers pulled | Cpoppedd off: A stack 7s a sequence of Tema tnat ane accessible at enly sng end ef the sequence - STACK AS AN ABSTRACT DATA WPE pony ps is seid to oy Apt if so Check te +e Stack 1s full ey not: ( ovexttows | A. Incest a new eement into ‘he Stacts (ie. Spevation ) Ge Deleee Ane topmost element POP operation) & | Retieve the value of topmost clement. Posy from tha Stack Cig STACK REPRESENTATION IN MEMORY USING ARRAY: . Stak cs an aowy ; | -40 | > Top — —_ _ : \ p48 | = = : 7 _ [Fea yo) as | | s | Lay | [iat og] 2s [33] is Lae] OPERATIONS PERFORMED ON STACK: PUSH () OPERATION « he process ef Inseiting new elements to the top oF tne stacK ‘is Cotled os ush opevadoys” : en a oan pus ¢ PUSH PUSH aan ; ey a L < ] ey a a — al D | Beto D iz 24Top e \2 te ’ c 8 | 1< TOP B| Bale [Ba |t A_|o< Top A|9 {A ]o A Lo lA 4 1 -\ 1 ~\ When stack is full and user try +o peyferm push spevatien- tis candition is _calied aA stack ovevflou Exampie : Sack Cs] ee Creates oraaaaed pee : Beg eee E | 4 «10 b 13 D3 Pee (es | Se |2 6 |) Bt Alte (ave lS) Sack Initraly =! aa POP) OPERATION : : a : The process _e| deleting elements from the stop ©} othe stack is cated ax _ pop__opervatur - | 2 | WWlhen —_stmcis is empty nd we my +o perform Pop Opevatien on stack then that Londition_ is — Caued os stack undevftow-_ Ieee dee eee Once STACK | th, Choxauo, 4 sng one pushed onte fo, OM popped one by one - since the chowatter Last Puthed In commen out fioct Subsequent , RUS in the reversal othe Stving: the string ' Reverse? left to wight and ib AS given pelow: the Sting is Chonocey are +, Checking tne Na\idtty ef an expression containing NOAted Parentnesis + Hacks axe also —URed +o Che -| Ortthmetic expression containing ts Prperty — -parnenthesizen. — The _pyogyam —fov__checking the Nalidity ef an —-Crepession verifies that foy each lett parenthesis ~ BYAUR gy nacket , thou sa vovrer ponding lnsing Symbol and SyMbols ane Appropriately | noastest. CK Whether a given nested Parenthesis, | oe. TT Evaluating axtitnmettc oe “INEIX NOTATION: = cme geneial WY oj _uniting ceekel preci | ig_enown 08 Ange notation : ay 5 <0) - Exampie > (a+b) —s | _| PREFIX NOTATION! ©°9> + AB — ___ Pos TELY NOTATION! —€-9 + AB +_ : : = _ Algorithm: of Display in stack ft —_____—_——- —— _— | ~ Step 1 Display (top, t, ota) == a — Step 2- if top> 0 then oe || _ pint "STAC _etaeTy” | Se ot = = —_ a —— | else _ — Sep 35 fo frp 9 . — ——4 — paint alit_ : | End toy — : —_—— — Bee 47 Exit al Aigoxitiors 3} deletion io_stacts : (pop? —_ Step; Deretion Ca, top item) —_Step 2+ if +op=0 then _ — print“ STACK UNDER FLOW” = exit _ else giep 3 j4tm >a (+op] end if ster at t top =+top-) sep > Exit Algovithm ef Insertion in stack ___|+—_—_ — step 1s Insertion Ca, top, item , max) Step 25 xf top? max then print ‘STACK OVERFLOW ’ Excite else a Step 3— top > top +1 ena if - Step 4 +a Crop] > Trem Step Sr Exit — > APPLICATIONS OF STACIS | Reverstng a_ist _ Polish Notaxton CposH) Conversion 6} Intix +o Postfix _ Expression Ni 2: 3 4 5. | Evaluatien ef Postfine Eounession 6... Evaluation | _ Patfix _ Exprtssion 4: Recunsion 8. Towev_ of — Hanot | Conversion ef — Infix to Prefix Expressten ft LPoish notation: The process of voriting the opevators 4 oan ercprtssien etthevy beforr tere epevants ov ayer tnem is Caucd aos polish notation ste | — “ares of PO Lane operands then the | Postfix: _If_ the “optratoy, _ phe opevands— then the — expression. LISH NOTATION: Prefix xe tre wpevotoy symbol! yre opevands then ane ee Ae angi Th tne — &q: AtB a notation :— a oe Rules ( Operation — paonity) | _ Brackets oy _ Panenthesis ee — 9. -— oS - 7 _ 2+ Erxponentiation ee eg. * 4 | Mustplication_/ Division be(1 to ~Satk coo ee Addition | gubhatton — eg- + 4 = eee oe try i [eonveRsion OF “Inetx To pOsTEX. EXPR| is placed b expression Ys in Pree opevorery symbols Ont plated behves, expression 1s 1 Intix noted “symbols oat placed ake iS im postfire = SION 2y (At) *¢ ang) (A+B) *c (AB+) *c Substituting (ABH) as P Pe Pc ¥ Resubstituting AB+ CK eS sc a cl a as 3> tater / Ccep) Ans) | (at8)_/ (OFD) } (aB+) /_ Cco*) Substitube _(AB+) by P and Cco¥) by 9 | P/ 9 eg! | Resubstituting A&+ Cox / 4) | Ca-m+¢% D/E-F ans) (AB)+ C¥D/E-F Cas-) + (co) E-€ : Substituting (AB-) by & ona. Cep*®) by g | P+ Sle-e | P +(QE/)-F | Susttuting (ge4) by R- | pAR-P : | (PRED BP | Substituting Cert) by s eee eee eee Soe fp (se-y — Reswostituting yalues pRt FO | ape GEA F- AB- co* ehFo sy An cpxc) +d! (elF) 4 p/ CelP) p | (eel) fms) |_ A= (BEC) ) by P and (erlby 8 A- (eck) + Substituting (ac* peas sPE pls [Ae eek (ee Substituting, (pg/) by B A- P+R (ae-) +B Substituting (aP-) by S — stk Oo srt oo Resubsttuting ie AP- b9/+_ : oo ee a en eae col Case) % Cen a —__Ans) AB+ ras —____| Substituting —AB4+_by_P as Px c/pd ——— Pee 7a on a _ Substituting PC¥ a ego bg Qd/ Resubsti XING pc ® D/ AB+ C#D/ gy AB Cewnre? pnt) AB I C C# DES) Sunstituting DE” by Pp : a-B | (Cc # PD AB | CO¥ Substituting _ce# by 4 Ga A-B/8 : : A-Bg/ — 7 - Substituting @9/ by R A-R Hee AR - - : 7 Recwo stitusing 7 A8g/- - oa ABCP*/~- - ABC DE” */ — — 8) (A-BIc) * (p* €-F) eee a ia | Ans) CA- Bic) * (p¥ E-B) 1 _CA- BC/) ® ( peEx -F) + | Substituting @¢/ by P and Dex by © CA- gi ( 9-F) tee Cag) CAF-) it esos . Substituting Ag- by Rand @F- by S$ 4 | R*¥ s oe ee a Scere — Rs*® ieee cea See ee ee tee : AQ- GE- * ABC /- QE ABC |- DEX F- ¥ a) (ate) C/p+E*Fl & Ang) CAtB) ¥ ¢ /D + ENF Lao (AB+) * C/D + EMF IG Substituting ABt os P Px c /p tler*)/ @ Substitusing EF by 9 7 P* C/p + OG 7 (eck) / D+ GIG - - 7 Substituting (Cece) by R / can R/p + 9/G@ oo (RD/) + (Gal) Pee anes : 7 Substituting (RD/) by S$ and (9G/ byt) | s+T 4 e = st+ He = | Resub stituting a = - | RD/T+ | Ro/ 9al+ Po*® D/EEN G/+ ABt+ C* D/ EFS G@/t+ CONVERSION OF INFIK EXPRESSION EXPRESSION USING STACK + ®, CA+B) Ans) CA*B) Chootactey Scanned TO POSTFIX stack Expression Cs C A. C A + C+ A B C4 Aa ) - AB+ > AK B+ Ans) (A® B+C) > Parenrhesis CCA¥B) 4.6) ea enon Chanattyy Scanned Stack) Expression C iG C ce A Cc A * Cl* A } B cx AB | ’ C AB¥ + C+ ABx s C+ ABC AB* C+ 3% A+B/Cc-D Ans) (A+ B/c-d) CLA + (By 02-9) Chanaciey Scanned Bracks Expression C C ¢ CC A cc A + Cl+ A C Ct4+C A (; aaessiaasont Pere sere 7 cee Ce+C/ AB ee cas Cotes ABC 7 cet ABC/ i | ) ( ABC/+ ae 4 = oe ABCI+ + D cy f= ART ED ae Da oe, ABC/+D-— —___ 4>. (A+B) / Cc-D) BEE 3 Ans), C(At8) / €C-D)) : : _= | —Chostatter Scanned Stack Expriessten _—* C ¢ _s | C (aa fae {—_____-__A_______¢¢ A —1 + C+ A % B (C+ AB = } x AaBt | Z iv, AB+ a sa an vuy C/E c/e Cle- £1 C= c/s 5> CA+B) /COFD) we C E/F)) Ans) CC CA+8) / CC¥D))% CE/e)) Chonattty Scanned CU TN MOK VU OR ODS YORE ROO stack c cc cee eee Cec + Se tee ce Ces cere (CCC. Cclce¥ CC/cx*% cel} c Ce CEL CRC ORCS CCT C* AB+ ABt+e AB+C AS+ cd AB+ CD- AB+cp-7 Expression A A AB ABY AB+ AB+ AB+C ABtC AB+ CD —_ AB *CD*_ AB+ CDR, —AB + CDx*/ AB+ CD¥/ AB+ Cp¥/E AB + CD¥/E _AB+ CD*/ EF -AB+ CO¥/EF/ ABS CDO¥/ Crs % fqhes pat | | 6) CA® B+? Edepre sci Ans) | Chonactey scanned — — r ( a * if > Che B por Ve” coe er. “ee C+ c Co >) 7 D (A+ Ble-p) . ~ Ans) Chaxauen Scanned stack C C : _ A i Goan A él = ct A Hf - an Uf So —Cd) Ae Ls ' LD = Cte ce ~ . 7 for t® Gy Rec ' it C- ABC /+ i B 5 : = ABC /+D_— ABC /+D- 8)| Catere-p) : - Ans) | Chosta ctey scanned cae pet 2 ¢ C+ ABC = Cz ABC/+ D (et ABC/+D > Aecj4+D- a) A+ (Be C- (Dre RED ¥G)ARHD Ans) Choxauey Scanned Stack Expression C C A C A ote a A Gade Gua A 8 +e AB ® fered AB C gop Wc ae ABC LE Qe — pam Vf C4 __ ABC C C4 © ABC % D CHC Apes Bb / (40-7 ABC#D E ~ cee? > ABCEDE sp ® Cacce ABC DE/ F CAO ABC¥ DE/E 7 C+ Wve ABC DE! x | Hp) (+(-% ape pe ee & C+ t—* ABC # DE /F RG 2. (Fup ABR DE) Fx Cre - ae c+¥ ABC *DE/ Fax - . Ct* ABC ®DEIP# e-H ABC €DE/F% @ #-He- oy A+ (BC (PEt) HOH Ans) A+ (Be ce COMESED *@)*H - (posers) ¥@ *H A+ (8 c~ (DLP) wa) *H ee ee (tet pee = 9. A+ (BC - O¥ &) #H fz ee = eee A+ ( R- @*G@) #*H Le - + = *H = - § rT A + (R-S)*H B + 7 H Bi seat : al A+ LxH - —— i A_*+_THY¥ ae a | et TH* -V » | | AT a _= Da Av + 7 - Resubsttuting Recubsh tus 4 = ARS-+ _ : ATHR+ a ABC Gar + __ARS- Hl et | apex DP/ G@*- + ARC aa‘ 14 ——T _ ABC ® DER @*-+ ppcw beyG +4 | ABCY pelt / ge —tett | aa | =a | a a ny COA+e% ¢/D) Ans) Chartactey Stanned Stack Emprssion C ¢ C CC A ce A + (c+ A 8 (t+ AB ) c Ag+ * es AB+ c Ce ABT C sp C/ ABS C % D CL AB+C¥ D > AB + CFD / 212) (a-e/ (cen ey) Ans) Grortavter Scanned wack Expression AC C A C A = (- A 8. cost AB eff) cy AB C C-1¢ AB c c-10 ABC. 7 C108 ABC 7 ay “10% _ ABCD CCA ABCD E C-1 CN ABCDE 7 C=] ABCDEN® ABCDEN ¥€/ — | @ay (a+ Caxc- (pfere) * a) ¥ HD) Ans) [cnacwacaee Scanned C +> | 1 Seale a ye } Oo ——— He ow Stack ¢ C Cr (4+ (+0 a (4 GaGa FC Geis c+c- © Hebi 7 __ (+(e pate Cf C4 0-4 C4+COip Cera ee ate (4 6-¥* “ C4ue (+% C+* Expression A A A AB AB ABC __ABC® ABO ABO¥D ABL*D -___ABC + DE ABC DE ABC DEF ABC per*/ ABC * DEF*/ ABC DEFRAG ABC * DEFA/ G@¥= — ABC® DEP*/G#- ABC ® DERS/ G¥-H ABC * Dern / &¥ Pee tHe ALGORITHM TO, CONVERT - ZNF\* EXPRESSION TO posTAX EXPRESSION A= Snyin_ Erprers B- Eqiuyotend Posr(ix erepoession Sep \: Push left parenthesis “C" onto stacs and add yight panenthesis “40 the end of A Hep 2: Stan A from icf +o ght and xepeok step 2+ 6 fev each element of A Until the Stack is empty step 3: Tf an epevand is encountered add it to 8 Step 4: TF eft porenthais "C" is enlounttned push it ento__dhe stats Step S: IF an epenoto is, encountered’ then A) Repeatedly POP from +he stack ond add to B each epevasey which hos the Same : pwonty ex _highew priority than oper otev 5) Add epevotoy 4to_ stacis Step 6+ If sight ponentnesis "9" is enuountoted then PD) Repeatedly OP from the Stack and add te e Cach _epevatey until leet panenthesis "" 1S envountned + Remove the left panenthicis "Cov Tgnove the left panenthesis 4H Step: Boctt ts g-| Ans) Vowwwors Convert the FoOUOWING, post % empression UA > (Caxe & CclD)) -€) Chanautys Sunred iS bowen ane #27 re \ Stack Expression C ce CC A ( A 1" 8 fe (ie en cen Goode a CO+6 AB U+¢ AB¥C Cees AB* C (C4 ABtCD (Geass AB+CD/ - — AB+CD/+ e SoH Siena AB+cpl+ Z (Gites AB+ cCpIt+F _— qnfix — 2CPHSSIONS — Into ing stack - o> Car (C8404 (pte) * F/G) Ans) — Chonauty Scanned Stack ¢ C ait C+ C+ +e CECE Cacet C+ Cet (4C C+ Cr C+ +e C+ C40 CACHOF+ C4 Ct ae! (404% C+ U4 * = Cis. eae a C+/ _ C41 Llp \doaln lke be le RE eh em ft Enpxssion AB ABC ABC+ Aet+ ABC+ ASL+D ABC+D ABC +DE ABC+ 0E+ ABC + DE*+ ABC + pes F ABC + DE+ Fe? ABC +DE+F*+ ABC + DET EST] ABC+ DE+FYA G4 Say] Unser x crp sere / &) Ans) Chavauev Scanned Sack a < 7 3 | Aste + + +P c ide CC (c+ (t+ C C#& sm tao0o0*F Oo +? 77 is it > -SNT7 nh P x Expression A A AB AB+ AB+ ABTC — AB+ Ce AB+C#D — AB+C%# ps AB CHD/E ABTCED/E —— AB +¢ ¥ D/EF _ AB+CKD/EFA AB+ C¥D/EPSG AB+ CD (EF*G/4+. Per ee te ee >) EVALUATE POSTFIX EXPRESSION (5s 6 2 + # 12 4 74 = IAP ans) Input CP) Stack Opevasion (ncron) 5 5 6 5,6 = 5 \6.2\ - + 5,8 . 64278 * 40 5%*B7 40 12. 40,12 4 40,12.4 Z (40,2 21423 oS 3 40-372 34 Pz at > Pe 5 Co + * 8B 4 J = PB Stock = Ackion | 5 5 6 5,6 oe | i S16.2 + 5.8 64+2.>8 7 40 See > 40 8 40,8 + 40,84 8/422 é 40.2 40-27 38 ALGORITHM POR EVALUATION OP POS TRY EXPRESSION This alguvithm finds the VOMAC | ootithmese expraston CP) — stetwin in postfix. notorio, a ures a stack to hold epevands: Step 1: Stan P from left to Wight: Repeat Step 2 to 3 foy_ each tuken ef Punt AU i envounttned = CNuu- \O) i Sttp 2: IF an epevond is _emcountMred push ony stack + eo oo Step 3: TE an epevarsy “is enrountered then _ A Pop +o opevands from the stack 5 Peviexm epevation on that operands _ Cc Push yesult bac ento stack 7 Step 4: Set yalue equol +o the top element sj: 7 Q Stach _4p_get+ the final volue- Step 52 Exit ain anne Q- Evaluate the postfix expression using stack: \y) Prt 6 6 2 4+ w& 8 tse oa ——E Ans) {| z_| [L 5 S| 64 a (4 a i 223 5 Exe 7 40_ » | - ~ 7 | 3 2 BS 4 7 | | 40 /43 0 40) Ans) Input Stack Opevakion ¢ 6 a 6,3 ¢ 6.13.61 + 7 6,9 34629 _ 5 6.9.5 * 6.46 Ax52745 4 6,45,9 L 6.5 451925 es A 6-52) 3)| 9 3 + 4 #* 5 / Ans) Input ___ Sra, @pevation 2 2 3 PES + 5 24395 a S14 7 * 20 5%*4720 S 20,5 sane / 7 4 20\5>4 4) 4 3 4 * g 4 4 7 - Ht Ans) Stack Gpevasion 3x4 2x12 124+8 2 20 ie ee a 4 apo , 4) / \q,5 2l4¢zs : 4 A754 | Salas Ans) Spevastion igo 2 3x82 94 | aoe 5 ae 2445229 Ans) Input Stack Operation 4 : 4 - 5 4,6 - | x 4.5.4 - | 2 4.5,/4.4/ - | a 45,16) 4% a2 — | so 4,2) 1645221 — | = coe a 2iKq2 34 — ok 2 ean . os . 84,2,2 —~| fe a es Sae4g 22+ 4-4 cf 4 4, 4,4 * | 3 St. 4.9.3] “<7? a 24,14, 25 4/33 * 84,12 4x3212 ese 42 94-122 F2 Q- Evaluate *he Following in Fix erepression iy to + CCa5) +10) /2 Ans) 10 * (2419) 12 yo + (2/12 1O0 + 6 hel zl 6+ a2 -9 % e/a =-6 / (a1) Ans) 5+ 242-9 *6/2-6/2 S+q-2%6/2- 6/2 549-\2/2 -612 5+9-6- 6/2 S4 9-6-3 (4 =-06-3 i fa lie = t 7 >I mes), 2>, -__fAns)_| (AB) AC a > ans) EXPRESSION “CONVERTING: INFIK EXP RESSION INTO PREE}y Convert ine to postfix” me foc nolation A+B +AB_ ( (Axe) Let P= AB P+ = SeABC Oe (ater o)* Dd) CcA/(e*c)) *D) (CAL (ABO) ¥ DD Let P2 *BC Cc A/p) ®D) C (AP) # DY let G+ /AP (Q¥*D) ¥* OD 2 . Resubstitwting enioalhee | Waiting opevatoas before operon ls | Resubstituting %*7APD SAS BCD 4) (A4e/0) * Upn* ele) ans) (A-C@/c)) ¥ (ti p¥e) =F) CA-(Bc)) * CC xDe)- let Pa /BC let @ > # DE (A-9)) * (Q-F) (ar) * (FD let Ra “AP lef S = = OF RS HRS Resubstituting , *-AP-OF *-A/ BC-% DEF 5) (AxB 4 Ce/p)) -& : Ans)| CC A¥B) + Cerp\) -e CeAB) + (cd) -@ let P> RAB Let Qt /¢tp | C p+) -e (+°9) -e Let _Rz +P9 R-€ ~ RE Resubstituting » - +PQF = 4+ #AB/ CDE Oates Cobre)! mene wires (p*e))) A- (BJ (C¥ (Ate)? [epee eee i a- ( e/(c#P)) —_— A- (Bl (cP) 7 | —tet_@s #00 oo | A- (8/@) . as (/ BQ) | let R > /BQ — | \A-R) = - - AR. ~_— —— Resubstituting ae —A/B9 A BCP __-A/ BC DE 3 ((A+ (ox O-((pre se) * +4) ans) | Co Ue) - COp/ LESE)) & @))) +H) CUA+ Clepc) = CU py (AFD) G0) +H) | let P2 *BC let @? AEF Ltt as (p- CC2/Q) * @))) +4) Ce sae : Oe ee HD let R» 7 | Clas CP (Rate) )) +H CLA (P- Lxgadyy 4H) Let $= RU (CA+ Ce-s))) tH) CUA+ esi +H) Lex {> Ss: CUA4+T)) 4H) C CHAT) +H) let U2 4+at uth +04 | Resubstitunag. ++ ATH ++ A-PSH ++ A-P ¥ROrH AHA P % /Dg GH t4A-P #/DAEEGH t+ AP & /DAEFGH ++ A-P BC #/ DS EFGH 8), A+ C(Bao) *Coee) * EA Ans) A+ f (480 4+ (406) ¥F) /G@ | let 2 BC let Q24DE | A+ CP4Q%FI/G Ax (p+ (#9) /@ let Ra * BF At (P4R/@ fee Re eee ie t let $7 +PR eee eee eee eee eee Sa oe Let © 7 be ps ea eas t Dal ans) (rap) #c Zp + esr |e jag * C/I | A+ Este Dae Clea et eee a AXT 4+AT a ResubstTtusing, + ALSO —— +AIFPRG 4A /44BC ¥OF GE (+A) 44 BOK + DERG _ Case) * C/p + ESF / Ge __— _ fet Peat AS = tet P (ope cp eee ge eee Poe o/b (See) ga ce | (ePCy / p+ g/@ PON / 4 eo Ro *P — R/D+Q/q RSD 4 — |e) + 1 9G) (ep) + oe Eee Ge se RO and airs 7.QGe Sacks : See = i ag) = er : C+ ~ | Re substituting, eteel ERD ge = ae 7 LEPC D/SAECG Sp/ or +0 + A — | PASCO SS EPG eee O/C | : : 4/% +ABOD Converting “infix SxPression Tnto pretin | €Xpxssion using stack - ALGORITHM: (Step lt Reverse the Inpus shing Step 2: Read the Nock element th the Toput Step 3: Tf +4 As operand, ada t& to the suipuk ching prefix expression) - : a Step 4° TF t+ t5 dasing panentrusis , push 44 onto stacts_ = a - : Satp S: Tf tt ts -Spevakoy , then ae | a) If sinc is empty, push -Spevakion en stack a Meee ) TE tne wep ef stack is axsing Parentneris . push -SPevOseY en ctack = a —<\ J4 it hoa same ey higher priovity than the topo} _stacé push spevaier en Shing 1) Else pop tne _eptvadey from tna stack and add _ — At t0_ ouput —_shino_ ae Step 6. Te it is -spening _panentnasis + pop _eperatey from tha stacks and add them te sting Until _dasing = Panenth esis is encountered - Pop and _Aiscand the closing panentnesis - ie i: —— Step t+: If tnene ts meme input 9p to__step 2 Step 2. Revewxe the Steud _sining Go Convert tnfice ExRACSSION 40 _poeFin _exprssien Using Stack - - — cua : Ny (A+B¥C) sat = | i | ACESS gear Chaxnauey Scanned Ee _Gp HP eeeneeeeiee Z ae : [fe 2 - 2 eS a oe . ae = Ee oo nae mie ee peat eey at =e eee Ce x [eee ane 4) (Caw p+ Cerp>)-F) = —Ans) Chonactey Scanned 2 ) | a Soa BD 7 | ee Stack ) » » )- yee 1) -# n= > a* 2*)> me) IDL 2 WD Ne ——— 3 )- j*)— ae FED —— FED — ___ oto ee Pep * = Po eeOe ce FED¥-C FED e-CBo Fep ¥-cB/ __ FED *- (CB/A _ Fep*- cea/A- — FED ® — CB/A—#* | us Ans) Chanock scanned * ) ) c . * > > ®) P—_ au Lt A d+ : . Answer: +A¥BC a) (Alpe +b) Ans) Chonauity Scanned Stack ) ) b > [ a a) LP s oe yap lr | oe | ets *)+0HP all ZUR d+/ : A d+/ | = - a = Answer + /A* BCD rae a _— sy Ca=Bicd %* (D¥ E-F)) - aE hs) CCA-B/c) * (p* €-F)) 7 Answer: %*- A/BC- ¥ per + = + poe FDC /p A : a a jae EDC /B 7 eo sat a ; © 2= FDC ba ey © Foc Jan x4— — Ancwev: + * AB/_C DF _ EVALUATION. — OF PREFIX EXPRESSION ALGRORITHM: . Step 1: Read the expession— from. Yigint +o lett _ : Sep 2: Read tne next element, if the elements ax . epevand then push the element in +e stack Step 3: Ti the nent element is __epevutey A) Pop two sperands from ne stacs _ _ £1) Bvoluate the exprcssien fromed by the spevonds ond ene epevatoy - s ©) Push the sesut {the expression back ta the — stock D) TR no moe elements then ‘pop Ane art i else go +o step ene- | Evalue 9 the foupwing Prefin escpre scion ' 423445 — 4A*B 40D : And)| Sonne _thowauen 5 i 4 + 3 9,3 * 2s qx a 2 24,2 * 24 2442 oy #4 4305 ae TK ABCD Ans) Scanned _Choniacter Stous Action So 5 = = 2 5.2 | 3 5.2.2.4 Cs 5.9.3.4 = eu 5.2.4 344 = ie Si\4 2% = -4 5-714 Q- “Tronstore the Following infix expression to its equi- \NOLent prefix expreesion - as — Ww (t+y) # lpr) H272,N'3. pzo.q22 Ans) Gxy) * pq) ey = Se ee ee SS = Ee Scanned character Sk. Ackion Pia = 2. = —_ 6 : a -4 2-6 a -4.2 3 -4, 213 —$_—_——— — pladcs + * -20 “AS L REVERSING A STRING: STACK: 4 a 4 4 aie = Ss Ee c [a | a a r+ | 4 + cl i S$ S s Ss 6 Push Ss Push + push a push © puch } - : Sh o $4 eee K | i | + c | a to a - t| | ¢ | £ te s Es S S ] ae Pope pope popt AES : Result Keats Reversing a__stying can be achieve by nealing the— input sting chawauev by chaxauey and puth ——— that onto _stacls AU end of the sacs Ts rachel once aU tne chatocseys of the strings ane pusht ente the stacs they oxte popped ene by one —— | dtnce tre chanactey last push tn comes ote fins > Thus, 8Ubsequent pop ep evatien pes uLts / An Q mverysa ef a stying - pEUNCTioN: Tt is a biocs ef ctorements oY Tnshwe aie ei some specific +ash- _— RECURSION: When a funcien definatien caues irsey — Lagain and again then +nis process. is Known Os ACU STON - _ TOWER OF HANIO: eee a Rulés: Only ene disk can be moved ot oa time- >)Bnly the top dish can be 2emoved. cae 3)No taxge dict can set on sma disk. Tritial state => Deviianon Aue {ower tour = nee disk |) | dick 2 frisk 3 7 : Roe eZ x = 2 yi > “Tack LL - [Rogvam ; arincuade < stcio-h> te wncuda < vonio-h> I “BR Anclude < mash: h> "de define mare jo Global_sleclaration void push © 4 = _ Notd pop O 4 _ _ void dfspiay O 5 = —4nt_ stack Cmaxd 5 - _ _| int _+np 2-15 : q ofa moaindd — — int cnotce 5 —— — —| do - t Te | pointe ("\n 2. ruse”) 5 —- prt ("An 2. PoP Wp + print ("\n 3: DISPLAY “Ys = pyinrt Xn 4. exit’ ds prinre (emer choice” ) 5 scant ( % Ad" , & noice) suite (choice) 7 case 4+ push C94 ] break 5 Case 2' pop?) brtak 5 case 2: display Oy eas; case 4: exit ( 5 break; default: printt (" Invaltia choiced; } while Cchotte 12553 geten 195 j void push ©) 2 prtnte(’ stacs Fs Fu"); else printt C" Enter etement : "ys scanf ("Slod” , fete J; +op > top +15 stack Ctep} = cle 4 y | ‘yotd pop) = int ele 5 ae Cop =2 “0 (___pitinte C" stack 3s empiy ; __ ese ele = stack Ctopl , > ts tine * prorat top > top-\ 5 tL __votd_ display (9 int 15 —_|__1 Ctop == -1) _ | printé (* crack is empty”) ; | else | —_fer Liz tep 7 17704 %--) 4 print Cun ted 5 Stack C14 5 L

You might also like