Professional Documents
Culture Documents
Efficiermt: Stacks 8 Queues
Efficiermt: Stacks 8 Queues
STACKS 8 QUEUES
Ccmputen efficiermt
These a typen * Daka structun
elements
ContenA euenG
and inked s
Saeka, Quues
EX ATays,
Tre-a rpha
FX ,
Data Stsuckures|
Non-Linear
LineaT
D a t a Strucures
Data StrucirA
Stack -
Stack is a Gnear daa stTuchune in whiw
A K
Exnort Stack top0 top-
top1
POP
he pIOC) o delefima h e elememt
Srom top 4he tack i callad Pop Opevation.
shen e elememt i deleted rom top he
ck, hen top altue decrememted bone becau
op i aluays Poinira he ar elemem m he
Stack.
O he elemenm are deleted Hrom top o stack,
C Empt
Stack
top O toP-
PeeP
with our
chamg toP value mean we ae no
Implemmertation Stac a ra
I i ao called athc inplementation Stack
Tne operation ot skacK aree
top = -1 4hem
stack is Unden4lous"
UreNo
els
tem Stack[toP];
toP: Bop -1,
i ) Peep
toP of
e areindi h element Jrom ihe staek
toP - 1 the
Wite
Stack iundasflou
el
t Dectare vamiabl 3
J top-i +1
Tetum skackLJ
toP - 1 4hem
rite
Stack undes flou,
e
Dectae variablk J
top-i+1
retum stacklJ;
Page 62
) Peek:-
e axe
indirg toP o the element Jrom
he Shatk
Algoithm for peeK operaion
Algonthm Peek )
top - hen
eN
"
wnte Stack i underflow
elR
Teturnn
stack [tor)
Aecations et Shack-
Reverma a ls
EOAy o implemem
Su bohr thon Dm
(Iv)PeeK
e aye findingfop
o the element Srom
he Statk
peek operation i
Algoithm for
Aleoithmn peek
toP - I them
elR
Teturn stack[top]
APeicating ctStack M
Reverim a lisF
Algosthm Rever )
for it t N- de
top op+1
s top AG
CCtU Ena for
for
ALi: s(toP]
top: toP 1
End o r
Faamp
iwer
tADAM N 5
arraeemeak
PoP he elemems
ino Sack
rem Atack amd
VTer into aTTOy
StacK
Stat
Fackoial Callation w Pecuráon
wmmm
o a mumber nis
n « (n-i)«(m-a)«
- - -
- -*3* 2*|
this is 1terative defini ton t findin jackrial
The fachoriol Cam aAo eaprAed
Tecuivelt
fac (n) -
nfactn-) itnm>o
argenprobleo Notes.1n
Recusion meam A functin cal7 tself
.Ahem
e
uTn n* Jactovial (n-1);
3- 31 32
Puahes into the
3 a
Stack
G 1
2 PoPPfom the
Stack
8 3 3*3 6
G6 a
intermediate CaltalatiaT
tlere i s uired to Pub h e
Asithmahc FrpranT
detined a the Tumber etperandy
An Epreian
Combimed ith aeveal opesartarA
operator we can dáide ur
Bosed em acementt
1Infi A+
Ae+ (Revewe polish utation)
3 Prefi +1(Poliuh notatien)
The Posfir notation i a type atien uhieh is
mos u i bable for a compute to Callate ar
enprexoon, ond b the UnivenAall ace epteed
TotatiSn
More Over the ptfin otation he Computs
lcok touard arny aithmati c eapraóGn , f
Faponential operator
Higheat
Mutipla|diviaon , NeTE
an 9
S d ) T} tne Acamed characr
add in to top c he sac
4the scanned chara cker an opTator
tyom stack
POPPd Ahe toP t u o elemen*omd pea-for m
CpeTatien uth cperatoT 4hen a u l t uily
Psh imto Stack.
Step Repeat Step 3 ntii ahe p o s a i end
Step s Final te ave a one elemem n ne
ack tnat Ault ui setuTn
Eample
Le ee hous tte alqori thm evaluatir eapreim
Poptai 123*+ *
Initially the ack is empt
The firas 3 charaten in posstima operands Ao
Eapreien
Stack
T h e neat Character scanmed i",uhich ia opeatr
huPoP, the topP tuo elements from the Ahack and
EaPreen
Sta
The e u l t ot an eaprain i Puhed mt6
Skak
Lectug ot
Stack
146-1
Stack Epresan
Stack EPTáan
Stack Epráon
Page 68
toP he acK
&tack
The net choracker ib "+",shich ib opeoto. *hud poP
toPta0 elemesn roTm the stack and pesferm the
paatisn h the oP<atos
Shack
44
EPreicn
he TeulE siu be dirrectn puahed imte Ahe sacK
NoL, Athere ia To charactens ouma in poti aprRNOn
R in PoP the rRutfrom rack and Teturm N
Volue
End TeAult
?
Poskfim shira: 133*++
*Ret : 1 Ote .1n
Lii)
Comyertia
m_enpr«sdon mnto Posi_euprNGT)
Algosthn Jar CoTVertirg injin eprenin into posHii
Step1 Puah "" on to the Stack
p Add")4 he end e infin enpravoon)
Skep Repeat nti infn
eapreion i endi
44he characten o
)
" , directM Puh into Stack
i} he
<haratta iy
"
Exampla
LeF ty take imin caprusien i AB4c).
initiall add ) a end omin enprersoT T
* (B+c))
NOu Puahll o t top tAhe aCK
Curren+ 3mb OprratoY Posi
seck
B AB
A
((+
T ABC
8 Aect
Aec+
Method a
Paamthe the eaprawdon aastrq -from lett toright
Posfin, it is tre.ated ay
kirgu Opeamd
OOnce the enpreim i canvete to o s f i korm
TEmove Ahe Paantheaiå^
A SG+
=A P+6l+
Bct F* ^|+
ABCt DEAP*G|+
AP++
= ABc+ F * 5 | t
Page 71
stack
Step PUah "" onto
end d infia e a p r a i e
step a Add (" to 4he
Complefion
SEPS Read all the characteu uPto "infin eLprxnan
) encauntered, directy Push onto Sack
LectureNotes.in