0% found this document useful (0 votes)
7 views

stack

Uploaded by

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

stack

Uploaded by

soovamsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CHATER - 3 STAc

-ReF. TRUPESH TEL

DeinitidM CLuoL Coycep[


STAc
list which allews inueti Cinel eleleti8
A linea
O CIMelemmt af Oue eud onl4 is calead
STAC

opetahsM,_Pof_ Zelehi petid


PUSH lserhi elemeut
Top Mt aLressile elmtd, BeBtou= least
LIFo L u s tlu i s t Out) The Jst iwerted elemed
Opereti spossile
u s t b e emoves i r s
heee h i s mechanis
puy
S Oaleo ufo lust in_first _at)

Beside muge is H -
demorst utii of_gtuul
Phemomenou pile
o
taauys n a CaBetu
Hue a pesm_edlesimn
t inas hat oul
i s available im
oYh e at h S u L e
Couutey t A- Cafeteria tay hslder

Botlom doackof
A paideyTof keeps
H top elemtu i n Stuek
tucis
vecor o niially ohe He
ToP elemt empty ToP hus alue o
kor CneCin S0 M
Uns stac
etemeds
revesentdis
PRof RUPESH ATEL
Each time a Mu elemeut is iserted u He Stuk
Hhe poiutey is incvemented b y " oue beteve Hae
elments plaee_ou Hhe sck Thepoiutt
is deeremnted by" Lach time a oeeti is
aole t stuck
ApplicahioMs stusk
Recuhsi
valucah ok bpmssi
3 Peversin chuuets
4. Hemellh Henulwre tenpis
SUseo buektzicki
STAC Aoritm: 1.PuSHC3,Toef,x) nsvis
ay elememtX to_e top o Se wichu
s_Vpresetes by Nectx G Coutuiin elemets
Wit peiutex TOPdenohngthe topelemt

1 cheele Stuc OVelo 7


loP z N
he Jwrite STALKovepfLow)
Petwy
LncvementTor ,

Tef Tof +1
3 Lsert lemeut
SLTof X
4 finisheo 1
Redu

PoCS,op This uuetion_moves the topelemaut


ap aStuck which. s Yepreseteo
Ref.TRUPEsH PATEL
a VectoVS Clns vetums Hiis elemeut 10P is C
oitey the top elumeut O He SBuck

. L L chec foy clexlol o stack ]


TofO
write STAL UNDE RfLoy N fep')_
Tve_cchidn im respeuse t NeerHonl
etw
IDeerumeut Paidte
f Tof-1
3 Retuy ormeytep elewmeut
petuy(SLTopt11)
3. PEEP (S,Tof,I) his kuncHnS_Vetrums Hhe Value
He 2 elemet the Tor He Stack
which i s epresertee by a Vectos S Contuning
N elemets Thee elemem is Mot oleete e b
unehidn

1.cheuk kr tuck UndeHo


Top-T1 s0
The Lwite STAC UNDER fHo GN PEEr
Talee actidn in vespose t Undlelod
bit
elemeut top o stuc
2ehw (SUTop-T11)
4 CHANG ES,To, XT)) This pa2ceeburechauges E
Valuee eleme o he top of t
stac Veulue Centinig X
PRoF. TRuPEsM AT£L
1 L eheck Stuc heder Ho
Tp T +4 <0o
Thm wite(STAC UNDERFLo oN
_CHANGE).

Rety
Lahunae elened tep top ostuck]
SL Top-I +1 X
3 Linishe
2etwu
App licctids Stuek
Revse string
White CA culgoviHhw ohichwil check tht e give
String 6elougs to kollouiug grameno yet
L cw wle a,b?* Whne wis t
Kevnse o w
Agovitam Peeguse
He Gaivey an mput striu hm STPNG
alphnbet 2ab,c
NEXTCHAR uetiy whichVehuushe Mext

VECTOR S Seprseuls Stuck


t is al poiute fo Hhe tap elemeut
O th Stuck

I u i i a l i z e stel by pluci e lette


the tep 1
Top
fRef. TRUIES IATEL
9.LGet And 9Hec Smbols eihey c oYblae
eneoyutves
NEXT NEXTCHAR STeNG)
Pepeat WLile NExT E '
NEXT
The Wvite C uveliol stvin )_
Exit
Else CallPUsH (STof, NEXT
NEXT NExXÍcHAR-CSTPING)

3. L Scun characteys kllodi4 c Compau


hem to Hhe chayaetey's Stuc
epe ile SLToP] c
NEXT NEXTCHAR STRING)
X PoP (S,T0P)
- NET t X_
The Write INVALID STRIN
ExIT

4 L_Next Symbol Must be blqnk


NEXT E
They hlite ( VALID STRING
Else Write INVALID STEING

5 . L finishee
EAIT
fRe TRPESM ATELL
writ c cllyerithw detemiye Cu input
Choractey is o Hhe Lshene
Strin
7:1-
CL
Algovith_ REco@NIZEE

1 L Iihlize tue Cne Coudey


ToP
CoUNT&R- B4-o
)

. LGet Stuck ahavactey


a om
Strin ane Cout Lthe oCCwtanee o b
NEXT NEXT CHAR STR1N
epeuf while NEAT
E NEXT
W Theni PUsH CST NEXT)
Else CoUNTERB 4 coUNTE2B_t 1
NExT NEXTCHAR-(STEIN

. L fof he3tuck nill empty he_deeremet


h e CouNTE B
e p e t hile Tof l e 0_
PoP CS,TOP)
CoUNTER-B< CobNTERB 1

[cheek
l _CoUNER-B e e0
he hWrite NVALID STAIN ')
wte VALID sTRIS)
Els
fRof TRUpLsM TEL
Jrae o Conteut oStuk rRECohNIZEE
putShing churaetey Scanne Couteds o Staak

abcba yone C
CC
Cab
C cab
b CCL

Veulie shyinq.

CLabcaab Moye C
Ca
CL C au
b Caab
C laab
Cac
vlio strinq Sinee ath

aabcbaaa L ene C
Cu

C aab
C Caab
Lavlicdstrig C LC
Sie cis ToPR CCL
and NEXT E
C
PRef. TRufEsn Te
RecwrsiA: Kerwsi is H techniqu
o le ining a Set or a paDce
texms o itselt
A pteelwe hut CoutuuSme ealwre
Call to itsel a proceune Call to
5Lue p C e l u which evetully
CauseS Hh kinst pavteelwre t e celeel.
fecunive prceslwe

here Cl

Recwwsivel clefine funchidMs (imitive


Factoyi unet
. Recwisive. use_of_ puredwne : (Non-imiiv)
Find GCDof iY Muhmbeys.

Algorihm to nel kuctovie ofiven Mo. sing rewnsi

_F iwttgeh N we can Compute ucn' a_N.


Stuck A is_ useo to 9tpye CM activutM_ ecovel
CLSSecictes wiH ecc VeLusie CaU £acbh
achvetiM_ yecoro Contunshe CwneutValue o_N
And C m e _yetuu
Hhe _CLalolsos BET ApPR.
TEMPLBEC 1S als0 o eeoro whta Cotuns
two Vuriables PAA AppRESS ToP 1 o
paitey to the pelem eutt e_stc A.
niticlly rethu cLolalveys is 9et t th
CLelobres. PAR-A i s s e t to N imi htel
Calliug
Veue
PRoF. kuPESH PATeL
1 L Lsave Nand Ytusy anloliess
Cal PUSH (A, ToPTEMP-RE
L1S the base cYiterim ounel 11
I N :0
the FACTeRIAL 1
step
else PARM N-1
ADDRESS_ strp 3
C o tostep 1-

Lcalauute NI
ACToRLAL N N *_FACTOpiAL_

4 L_Restore prvious N Quel etsyasldres1


TEm REC PoP (A,ToP)
L fALMN, ADRLESS RETADDRsS
ApDRESss

RACE OFAlgorithm EAcToRIAL


Leve pescriphM Stack contentz

hevel 1 4 fusrn LA,0,2, muin 2


Alelvess)) Main
LAdelres
PARM 1,ADDPk stp 3 Top
RoF.TeuPESH TATEL

Level 1. PUSH CA,4CL,Stp 3) 1


step
RecLAsNe M-0,ADDPstep3 LAdldes3
cull) ToP

Level 1PusH CA,2,C0,step 3) 9

3 N 0 step Se3
FACToPAL 1 Adeless|
ReewmsiveL ToP
Call) Pof PoP CA,3)
0 t step 3 Main step3
Addses

Retw te 3. FACTORIAL *
wu L 4. PoPLA,2) Mun
0 to stp 3. Addves
ToP

Retun to 3 FALToRIL 2 * |
4 PoP CA1)
o mun _ualalvessl
ToP

o w oHane is C YewrsivePble Lohch


has historic busis_n tHhe ihualo HeCucieu
Tow ot Bvahma.

Povblem i diss of_derensiu Size stuekea


NLeel)e Anel two ep
ROF. TRutEsH PATEL

Meeelles. It is to stucl all the discs


eiveo
a Secona Needle in deeusing Oveler
Thee third_heesdle maybe usee
as
o Si
tempovu stoYage
arplicable kor cliscS moveMeuf.
alleving u e sa
movle at quttme.
. Only Me disc my be
A eeslle
A dise hmay b e movesA m
ko ay oHhey

3. At o inL_ma lgu clsc estupev


Synalle du

Neele A Ntealle B NecolleC


(Strt et_prblem) Ciutmeeite)Ccemplehl ef paple)
i lowek Hanei

Soluhion2: move one olise e nedde A to c.(discz1)


disc:2 He
Move the ihs disc t yeeale B
Move e second m_Meeslle A to C
move clisc B to yeealle C
geuetel,_
1 Move N- discsww1 A t B.
Move disc N fo A te c
move N_discs Btoc.
fpof. TfufEsH ATEL

PoLISH ERESs IoN THEIRCompLATION


-Evahietia nix Ezpaessi :
aat b et d * e

A rpeatesl Scanni n m1 left to rit is

Meeelesl CSOpehatis Cppeas nsiele the


Opehelnel

eperteel Scannin is aveiclee i te ix


expysi s st Conveste te_an
e4uvuleut parenthess prefix ey Pesi}ix
exprsi

preix bpresisn:Opeatev opeana, Opehan


postix Expresi:opesand apea nd, Apeketey

Tis bpu of herahen's Poli'sh hotehis


Leverse 2ulish Nathid due Polish
Dictan Jan Lukasiewicz

lu bot Pei pastx eqwivalets o C


expresid) te aria bles
Same veluhive posihion

he expressnsiM ps1ix prfix


p eunests hL an apeatoy e _ kecnsanaoa
Csin4 ues reelenue
Pos1SHNOTATION PRoF. TufESr PaTe
No INfIx PosTFIX PREFIX
a
2 atb aht tab
atbtC ahtct ttab
atCbtc) abctt tatbc
atCb*c) Qhc*+ ta*bc
6. a*btC) abct atb
** abC

CONVERT INEIX h_PosTfIx ExP2EssioN


Preee denee
Symbol Shel Pauk
TeLeeee pTeLedeuLe nehs
lueti f fuuebis9
1 2 -1
3
S -1
Variable9

Algorit REvfoL
INFIX Sting coning An Inix expressicM
PoLisHstnin Coutuu Conveted evse Polish expressjo
Smbolhave preeeaenee a u e as e above tuble.
5 dnotes Vect to represe sTackTop denotes
he the stuck
qUsH, Pof ae ued u Stuck mai putid
NEXTeMMR he
EMP tempor Stoa prpese.
PReF T RufEsH PATEL
. Initialixe Stutk
Tof 1

Llihelize Output Stig Cnel Va coud


PoL1SH
RANL 0

LGet kisst inph Smbol1


NExT NEXTCHARINfIX)

4 L Tuslade the inay expresion


epeot thu step 4 Lslhile NEXT E

pYecoume
Symbals wit reateh
Skuu

he Lalnite CINVALID)
txit
Repent While fCNExT) 9(slTep1)
16mP POP (s, ToP)
foLISH POLISH O TEMf
Pan PAN + KLTEMP

ite INVAIP)
Ezit

6 LAve there medchi ng Purtuthess


I fNEX T) j(StToP1)
then Calul PusH CS,TolNEKT)
else PoP CS,To)
PRef.TeuEsH ATL
the eut iuput Symbol
NEXT NEXTCHAR CINfI)

Ls the erpesi Valicd 1


oV_ PANk #1
Hhen Write NVALD)
else Write VALID')
Exit

TouslhiN Latbtetd) *Ceik/d) CN pesTH)


chuute Coutents ef- Kevmse pdlsh
Scauneo Stuck tpvesigS

CL
b Ctb 1

C (+1c ab
C+M abc 3
abc
abcd M+
abcd ME
abcd 1+
ce abcd Mt 1
t abcd Mte
aba 1te
abd M ej 3
cttld abtdMteb_ 3
C* abtd Mtetd/t
abcd 1tetd/+* 1

You might also like