You are on page 1of 16

Prepared By:

UNIT- Mr. THUMU SUBBA REDDY

STACKS 8 QUEUES

Data Stouchure Data s t r u t tu i the ot * i

ond stoviy 4he data im Computer- So we Can W

Ccmputen efficiermt
These a typen * Daka structun

Lineat Dafa stTuctur

a Non-Linea Data Staucuu


near Data stvuctuve
,hict
AwE a ünear Data sluctui

elements
ContenA euenG

and inked s
Saeka, Quues
EX ATays,

o Non- ineaY Data Structu


Non-inear
Dala rueci
to
Soid
The Daka is
a equemcee
ane donot form
insie heelemem
hieravehicalJomat

near serie but form a

Tre-a rpha
FX ,

Data Stsuckures|

Non-Linear

LineaT
D a t a Strucures
Data StrucirA

ueu Linked Tree Saph


Aras Stacr
Page 58

Stack -
Stack is a Gnear daa stTuchune in whiw

the elemens îwentjon and deletion peHo aF

Rorme end. oftenl; (General Cal ft ar LIFO

Laat-1n -Ftat- out).


There tuo baie openationa performed in

Stack i n a aniab called top.


PUSH 3 PoP

TOP aluns tndi cai the las neted

elemen nthe Aack


Tnitiall he top value " -
T4 ou inaer the elemem nto atack, them
ahd AF
the top value ncremented bone
top location ue a inAertiTy Aheelamemk Tm
he stack.

4o anto delete) he elamem ro9 the 3ack,


n stack iu be delefed
the top location elemam

them We decrenemt he top Value r One

The last imaarted elemm u be prTOCesed

ala firat, ana Ahe firs+ mted elumenE

1 Procemed aluop lpt


PUSt PoP
Delehe
ner +top -3

Previous Year Questions on Stack


PUSH
a neu element
T+ i8 the PTo ces aci
*op d t h e stacK callad Pust.

insert mess elemen mer 4o the sack ther

toP i îneveent One


- Ahe slacK fvLL , then i f o u Defarm un

opexati sm on to the stacK hen b


Ove fou maerA maes+3 nast C

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,

them aack i empty Pf+er that e pervism


PoP OPessati on
Aat Stack ia Undeous
Delete A
Delete
oelete C

C Empt
Stack

top O toP-
PeeP

To fimd i element rom ye Atack


e aTe o
Chonai top alue
Peek -

To find Ahe top o the elememtrom h e stact

with our
chamg toP value mean we ae no

deleti 4he elumen from the stack.

Implemmertation Stac a ra
I i ao called athc inplementation Stack
Tne operation ot skacK aree

PuSH i)PoP i) peeP iv) Peek


USH
- e are
inaRtiTR he elememt in to the stack
Agon hm for puSH openation i
OT 111
Algovithom PUSHCfHm)
toen- then
wite Stack i oyerjlous"
e

Set toP top +1 3


StacktoP item
di) POp ):
delei the element rom Ahe Stack

Algorithm for pp operation is

Algovi thm POP

top = -1 4hem
stack is Unden4lous"
UreNo
els

eleted elemenh stovey m Feo variab

tem Stack[toP];
toP: Bop -1,

i ) Peep
toP of
e areindi h element Jrom ihe staek

lgorithm for Peep operahor

Lgor thm Peep CEES.1n

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

Factoia calcdation Recusaon


Arithmetic Eprim
Evaluate Poafiz eapreion
Evaluate Preti O enpraisn
Convert i f eapraion imto
Posa eaprsoy
ConvTE
Tnfienprajam mto prefin eapmion
parentheay Matehi

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

wnte Stack i Umderflow" ;

elR
Teturn stack[top]

APeicating ctStack M

Reverim a lisF

Pactoial calcdation Recuraicn


Arithmetic Epreiems
Evaluate Posfiz enpreion
Evaluate reti enpreon

Convet infin eapraion into Posfin exprumo


Csnvert s i n eaprion mo Prefin eapranisn
Parentheay
Matehi
(mplemem
Knoun.
Suntab when the mo.oF elemen alread

DiA 0dvamteag uUA


beyond the e
avra is iaes, o the elemen
si
Can't i e t elemens nto Atack

Lavger asray mon ead to high meme asbaRe it e

add onl4 feud elememn into Aack


RevorAsTA elemenS in a t
m

Read alt he iAt f elements and Pusted into


empt stack.
Then he elemens Ppesrom top poitien o
Stack and pint Out one bone or) maer
elkmen nto aro ntii the Aack is empt
Algovi thm or

Algosthm Rever )

input AroY d elemesA


OuFput Ain orra ith. seyeTAeI der
An or d Sye N, aving Nelemems,
I toP -\

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

Let uA COnide the Cox calclation ot fadtoriol

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

To find3 he lution a lager problem, a 3aner


Te thodi to veduca h e large problun nto one o

more a b problemmy This proCas usin CsTntmues upto

indi Ahe sslutin oAub problem. ATal


u aub problem automatically fjnd aution r

argenprobleo Notes.1n
Recusion meam A functin cal7 tself

for atorial calaulatio is


Algoi thm
igoithm acoriad (n)

.Ahem

e
uTn n* Jactovial (n-1);

ek Past we o findiT the Jatoria N Kat


In the

ackual diret trarslatan


ok =n»(n-)L,ir nL
he form factorial (n) n*factorial(n-).
The defnitien o that m unt b calclated

n-)! is knmoun, which tur


Ond on until n20, uhem t seurna 1.

Let Comunder Ahe calsati on of ÁT

Te cu*ye algoith m afolletg

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

tin Ahee termiol Condithion i Teached

In ateve calcaation SteP to step 5 a Pus

Operation, then bseauent Pop perahonA i e z e u t e

Ahe intenmedi ate caleatina tiM Ahe Stack iy eahause

Asithmahc FrpranT
detined a the Tumber etperandy
An Epreian
Combimed ith aeveal opesartarA
operator we can dáide ur
Bosed em acementt

eapreNOSn into 3tpRA

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

ere entaes in he cempurr ia irat Convented


nto postfia enprexim , Stosed into 4he rack them
fH calcalaed
Cperato precedenca

Faponential operator
Higheat
Mutipla|diviaon , NeTE

Phddition| Sutarachin Lear

)Evalucting Poi eaprakym:-


Algovilhm r postHi evaluafion i

E Read Ahe pes stm rom lest to ighF

Ste Initiolisu amempty Staek

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

hou Push operamd) int» sackK

Eapreien
Stack
T h e neat Character scanmed i",uhich ia opeatr
huPoP, the topP tuo elements from the Ahack and

perform Ahe Opeatin

EaPreen
Sta
The e u l t ot an eaprain i Puhed mt6
Skak

Lectug ot
Stack

The nea choraAer canned i +",uhich i opuato.


n PoP the top touoo elememS{rom he Atack ana
per fsrm the operaicn.

146-1
Stack Epresan

Stack EPTáan
Stack Epráon

Page 68

h e nent charattes is ogenamd A dire Pushed

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
"

POP Ahe. elomemks rom


tack uPto
encontesim C" and add PoPped
emenS to
posfin shi, ecept (
tll Yemoye nd i+
from atack.
iy f 4he chata tter is eperand, Ahen direxttu
add tohe
posin sh
Uv) If he characru is Opeator, then check he
Prioit et previqusy inerted
opesator, T+ he
Stack cpator
avirg los prioit direct Push
Ahe
opesator onte tte Stack Othesusu POP the
Gperater from the atack and odd i mtt ihe
POAHfin Arin then Puh Tadir charackty
On to he Sack.
SkEP na he infin s h r i Complered , check tne Atack

av the elkmens or not, i¥ it is hauira elkrmu


Pop a he elumunk amd odd into PosHix

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

PorniheAn enprejn, the opurond


Oo ciated ith cperatoY i m higher Precadan
a firat paTathesizid
h e Sub-eapreion uhich taa been Comuerted o

Posfin, it is tre.ated ay
kirgu Opeamd
OOnce the enpreim i canvete to o s f i korm
TEmove Ahe Paantheaiå^

An indin r a preyion , aubstitute the opTand

aue uHh p r e v i e l a u n i r g valu them e cam

ind un finol Post fi epseOn.


amp-
Let u aks infin eprasionAr (B+c)+(0+¬)*FJIG
Siven A+[(e+c)+(0e) *T]l6
A+Cec4)+ (oE)*F}l
LecioLPA] las Pec, V0C«
Ae+F*)jls
A+Pr]ls :.TF
PT+31s
s1s) CSaPT*
A+SG)
A+T C: T SGS

A SG+
=A P+6l+
Bct F* ^|+
ABCt DEAP*G|+
AP++
= ABc+ F * 5 | t

ABct DEAF* S|t

Page 71

Prefia expraorm;- &


ii) Convert Ifir exprexmom
Algovithm i

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

d i "". m CeumtY, POP au the elumnt frOm


enCoumtered. omd Tmove
Stack uPto
paranthaiy
(Im) t pesand encoumened diret add to pref
esmgm
opator encaumtened, PoP the
rom stact and add to pPrefiz ft i 4

u i igher P r e c e d a n e 4han radia Ope


Otherusies d i r e y Fuah on to Stack.
StEP uhen the infin stri9 Cempleted, Checkithe atk
hauire tte elamermks or not , H* tavig etoma
P au 4he elumemts and add tnto pefi stri
Rapl-ecture Notes I

LectureNotes.in

You might also like