Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

AnI ntroduct iont oDat aSt r uct ur esGoal soft hisCour seYouwi llunder stand- whatt het ool

r stand- whatt het ool sar ef orst oringand postfixf or m weneednotconsi dert hePr i
or i
tyandAssoci ativepr opert y(orderofbr acket s).* E.g.x/ y* zbecomes
pr ocessi ngcommondat at ypes.- whi cht ool sar eap- propr iat ef orwhi chneed.Sot hatyoucan- makegood */xyzi npr efixandxy/ z*i npost fix.Bot hpr efixandpost fixnot ationsmakeExpr essi onEval uat i
onal oteasi er .
desi gnchoi cesasadevel oper , pr ojectmanager , orsyst em cust omer .Rei nfor cet heconceptt hatt her ear ecost s PrecedenceofOper ator s* Thefivebi nar yoper at
orsar e:1- addi t
ion,2- subt raction, 3-mul ti
pl i
cat ion, 4- divi sion
andbenef i
t sf orever ydat ast ruct ure.l ear nt hecommonl yuseddat ast ruct ur e.- thesef or m apr ogr ammer '
s and5- exponent iation.* Theor derofpr ecedencei s( highestt olowest )-Exponent iation - Mul tipl i
cat i
on/ division
basi cdat ast ruct ur e" tool ki t".Whati st hisCour seAbout ?Cl everwayst oor gani zei nf or mat ioni nor dert oenabl e *,/-Addi ti
on/ subt raction+, -
.Infixt oPost fix* Not ethatt hepost fixfor m anexpr essi ont hatdoesnotr equi re
ef icientcomput at ion.- Whatdowemeanbycl ever ?- Whatdowemeanbyef ici ent ?Ef iciencyAsol ut i
oni ssai d parent hesi s.* Consi der‘ 4+3* 5’ and‘ (4+3) * 5’
.Thepar ent hesisar enotneededi nt hefir stbutt heyar enecessar y
tobeef i
ci enti fi tsol vest hepr obl em wi thini t
sr esour ceconst rai nts.- Space- Ti me* Thecostofasol ut ioni st he i
nt hesecond.* Thepost fixf or msar e:4+3* 5 435* +.( 4+3) *
543+5*10.Al gorithm f orInfixt oPost fix1)
amountofr esour cest hatt hesol ut ionconsumes.Whati sPr ogr am ASetofI nst r uct ions, Dat aSt ruct ur es+ Exami net henextel ementi nt hei nput .2)I fitisoper and, outputi t
.3)I fi tisopeni ngpar enthesi s, pushi ton
Al gor ithmsDat aSt ructur e=ACo nt ai nerst or esDat a, Al gor i
thm =Logi c+Cont rolTe r mi nol ogyAbst ractDat a stack.4)I fitisanoper at or,theni )Ifst acki sempt y,pushoper at
oronst ack.i i)Ift het opofst acki sopeni ng
Type( ADT)- Mat hemat icaldescr iptionofanobj ectwi thsetofoper at i
onsont heobj ect .Al gor ihm -
t Ahi ghl evel , parent hesi s, pushoper at oronst acki ii
)I fithashi gherpr iori
tythant het opofst ack, pushoper atoronst ack.
languagei ndependent , descr i
pt ionofast ep- by- st eppr ocess.Dat ast ruct ur e- Aspeci icf ami lyofal gor i
thmsf or i
v)El sepopt heoper atorf rom t hest ackandout putit,repeatst ep4 5)I fiti sacl osingpar enthesi s, pop
impl ement inganabst ractdat at ype. operat or sf rom st ackandout putt hem unt i
lanopeni ngpar enthesisi sencount er ed.popanddi scar dt he
impl ement at ionofdat ast ruct ur e- Aspeci fi
ci mpl ement ationi naspeci ficlanguage.Whati saDat aSt ruct ur e? openi ngpar ent hesi s.6)I fther ei smor ei nputgot ost ep1 7)I fther ei snomor ei nput ,popt her emai ning
*Adat ast ruct ur ei saschemef oror gani zingdat ai nt hememor yofacomput er .* Adat ast r uct urenotonl y operat or st oout put .Eval uat i
onapost fixexpr ession * Eachoper atori napost fixst ringr efer st ot hepr evi ous
st or esdat a, butal sosuppor tst heoper ationsf ormani pulat ingdat ai nt hest r uct ur e.* Forexampl e, anar rayi sa twooper andsi nt hest r i
ng.* Supposet hateacht i
mewer eadanoper andwepushi tint oast ack.Whenwer each
dat ast ruct ur et hathol dsacol lect ionofdat ai nsequent i
alor der .* Youcanf indt hesi zeoft hear ray, stor e, anoper at or,itsoper andswi llthenbet opt woel ement sont hest ack.* Wecant henpopt heset woel ement s,
ret rieve, andmodi fydat ai nt hear ray.* Ar rayi ssi mpl eandeasyt ouse, buti thast wol imi tat ons:Li
i mi tat ions performt hei ndi catedoper ationont hem, andpusht her esul tont hest ack.* Sot hati twi l
lbeavai l
abl ef oruseas
ofar rays1- Onceanar r ayi scr eat ed, i
tssi zecannotbeal tered.2- Ar raypr ovi desi nadequat esuppor tf or anoper andoft henextoper ator .Eval uat i
ngapost fixexpr ession* Initialiseanempt yst ack.* Whi l
et oken
inser ti
ng, del eting, sor ting, andsear chi ngoper at i
ons.Or gani zingDat aAnyor gani zat ionf oracol lect ionof remai ni nt hei nputst r
eam.* Readnextt oken.* Iftokeni sanumber ,pushi ti ntot hest ack.* Else, i
ft okeni san
recor dscanbesear ched, pr ocessedi nanyor der , ormodi fied.Thewayi nwhi cht hedat ai sor gani zedaf f ect st he operat or, popt opt wot okensof ft hest ack, applyt heoper ator,andpusht heanswerbacki ntot hest ack.* Popt he
per formanceofapr ogr am f ordi ffer entt asks.Thechoi ceofdat ast ruct ur eandal gor ithm canmaket he answerof ft hest ack.Eval uat ingapost fixexpr essionEval uate 34-53*-s. push( 3);s. push( 4);r i
ght=
dif ferencebet weenapr ogr am r unni ngi naf ewsecondsormanydays.Comput erpr ogr ammer sdeci dewhi ch s.pop( );lef t=s. pop( );s. push( l
ef t-ri
ght );thest acknowhasoneval ue- 1Cont inuewi th53* -.s.push( 5) ;
dat ast ruct ur est ousebasedont henat ureoft hedat aandt hepr ocessest hatneedt obeper formedont hatdat a. s.push( 3) ;r i
ght=s. pop( );l eft=s. pop( )
;s. push( l
eft*r i
ght );5*3Cont i
nuewi th– r ight=s. pop( );l eft=s. pop()
;
CommonDat aSt ruct ur es*Ar ray* St ack* Queue* LinkedLi st* Tr eeCommonDat aSt ruct ur es* Dynami cdat a s.push( left-r ight );Theexpr essi onhasbeenpr ocessedTheval ueatt het opoft hest acki st heval ueoft he
struct ur es- Dat ast ruct ur est hatgr owandshr i
nkdur ingexecut ion.*Li nkedl i
sts- Allowi nser tionsandr emoval s expressi oni s- 16
anywher e.* St acks- Al l
owi nser tionsandr emoval sonl yatt opofst ack.* Queues- Allowi nser tionsatt hebackand
removal sf rom t hef ront .* Bi nar yt rees- Hi gh- speedsear chingandsor tingofdat aandef fi-cientel imi nat ionof
dupl icat edat ai tems.Funct i
onsofDat aSt ruct ur es* Add* Get* Change* Del et eWhySoManyDat aSt ruct ur es?
Idealdat ast ruct ur e:“ f ast ”, “ele- gant ”,memor yef ficientGener at esensi ons:- timevs.space.- per for mancevs.
elegance.- gener alityvs.si mpl ic- i
ty.- oneoper ation’ sper for mancevs.anot her ’s.Thest udyofdat ast ruct ur esi s
thest udyoft radeof fs.That ’swhywehavesomanyoft hem!Whi chDat aSt ruct ur eorAl gor ithm i sbet ter ?
*MustMeetRequi rement .* HighPer formance.* LowRAM f oot pr i
nt.* Easyt oi mpl ement .Pi cki ngt hebestDat a
St ruct ur ef ort hej ob* Thedat ast r uct ureyoupi ckneedst osuppor tt heoper at ionsyouneed.* Ideal lyi tsuppor ts
theoper at i
onsyouwi llusemostof teni nanef ficientmanner .* Exampl esofoper at ions: -
ALi stwi thoper at ions
inser tanddel et e- ASt ackwi t
hoper at ionspushandpopSel ect ingaDat aSt ruct ur eSel ectadat ast ruct ureas
fol lows:1- Anal yzet hepr obl em t odet er mi net her esour ceconst rai nt sasol ut ionmustmeet .2- Det er mi net he
basi coper at ionst hatmustbesuppor ted.Quant ifyt her esour ceconst raint sf oreachoper ation.3- Sel ectt hedat a
st ruct ur et hatbestmeet st heser equi rement s.SomeQuest ionst oAsk* Ar eal ldat ai nser tedi ntot hedat a
st ruct ur eatt hebegi nni ng, orar ei nser tionsi nt er sper sedwi thot heroper ations?* Candat abedel eted?* Ar eal l
dat apr ocessedi nsomewel l-def inedor der ,ori sr andom accessal lowed?Exampl e:AQueueAqueuei san
exampl eofcommonl yusedsi mpl edat ast ruct ur e.Aqueuehasbegi nni ngandend, cal ledt hef rontandbackof
thequeue.Dat aent er st hequeueatoneendandl eavesatt heot her .Becauseoft his, dat aexi tst hequeuei nt he
sameor deri nwhi chi tent er st hequeue, likepeopl ei nacheckoutl ineatasuper mar ket .Ex ampl e:ABi nar yTr ee
Abi nar yt reei sanot hercommonl yuseddat ast ruct ur e.I tisor gani zedl i
keanupsi dedownt ree.or gani zedl ike
anupsi dedownt ree.Eachspotont het ree, calledanode, hol dsani tem ofdat aal ongwi thal eftpoi nt eranda
rightpoi nt er .Exampl e:ABi nar yTr eeThepoi nt er sar el inedupsot hatt hest ruct ur ef or mst heupsi dedownt ree,
wi thasi ngl enodeatt het op, cal ledt her ootnode, andbr anchesi ncr easi ngont hel ef tandr ightasyougodown
thet ree. Choosi ngDat aSt r uct ur esBycompar ingt hequeuewi tht hebi nar yt ree, youcanseehowt hest ruct ur e
oft hedat aaf f ect swhatcanbedoneef fi
ci ent l
ywi t ht hedat a.Choosi ngDat aSt ruct ur esAqueuei sagooddat a
st ruct ur et ousef orst or ingt hingst hatneedt obekepti nor der , suchasasetofdocu- ment swai tingt obepr inted
onanet wor kpr i
nt er .Thej obswi llbepr intedi nt heor deri nwhi cht heyar er ecei ved.Mostnet wor kpr intser ver s
mai nt ainsuchapr intqueue.Choosi ngDat aSt ruct uresAbi nar yt reei sagooddat ast ruct ur et ousef or
sear chi ngsor teddat a.Themi ddl ei t
em f rom t hel isti sst oredi nt her ootnode, wi thl esseri temst ot hel ef tand
gr eat eri temst ot her ight .Asear chbegi nsatt her oot .Thecomput erei t
herf indt hedat a, ormovesl eftorr ight ,
dependi ngont heval uef orwhi chyouar esear chi ng.Eachmovedownt het reecut st her emai ningdat ai nhal f.
Itemscanbel ocat edver yqui ckl yi nat ree.Tel ephonedi r
ect or yassi st ancei nf or mat ioni sst oredi nat ree, sot hat
anameandphonenumbercanbef oundqui ckl y.Choosi ngDat aSt ruct ur esForsomeappl icat ions, aqueuei s
thebestdat ast ruc- tur et ouse.Forot her s, abi nar yt reei sbet ter.Pr ogr ammer schoosef rom amongmanydat a
st ruc- turesbasedonhowt hedat awi l
l beusedbyt hepr ogr am.Dat aSt ruct ur esi nAl i
ceAl icehast wobui lt-i
n
dat ast ruct ur est hatcanbeusedt oor gani zedat a, ort ocr eat eot herdat ast ruct ur es:* List s* Ar raysLi sts- Al isti s
anor der edsetofdat a.I tisof tenusedt ost or eobj ect st hatar et obepr ocessedsequent i
al ly.- Al i
stcanbeused
tocr eat eaqueue.Ar rays- Anar r ayi sani ndexedsetofvar i
abl es, suchasdancer[ 1], dancer[ 2], dancer[ 3],…I t
isl ikeasetofboxest hathol dt hi ngs.- Al isti sasetofi tems.- Anar rayi sasetofvar iabl est hateachst or ean
item.Ar raysandLi st s* Youcanseet hedi fferencebet weenar raysandl istswhenyoudel et ei t
ems.* I
nal ist ,the
mi ssi ngspoti sf ill
edi nwhensomet hi ngi sdel et ed.* Inanar ray, anempt yvar iabl ei sl eftbehi ndwhen
somet hi ngi sdel eted.Dat aSt ruct ur ePhi l
osophy* Eachdat ast ruct ur ehascost sandbenef its.* Rar el yi sone
dat ast ruct ur ebet tert hananot heri nal lsi tuat i
ons.Adat ast ruct urer equi res:- spacef oreachdat ai tem i tst or es.

-ti
met oper for m eachbasi coper ation.- pr ogr ammi ngef fort.Dat aSt r
uct ur ePhi -losophy( cont .)- Eachpr obl em
hasconst raint sonavai labl espaceandt ime.- Onl yaf teracar efulanal ysi sofpr obl em char act erist i
cscanwe
knowt hebestdat ast r uct uref ort het ask.- Bankexampl e:- Startaccount :af ewmi nut es- Tr ansact ions: af ew
seconds- Closeaccount :over night .
StackWhati sast ack?Ast acki sadat ast ruct ur eofor der edi tems.El ement sar eaddedt oandr emovedf rom
thet opoft hest ack( themostr ecent lyaddedi t
emsar eatt het opoft hest ack) .Thel astel ementt obeaddedi s
thefir stt ober emoved( LI FO:LastI n, FirstOut ).exampl e: consi dert raysi nacaf eter ia* t
ogett hebot tom t rayout ,
youmustfir str emoveal loft heel ement sabove.Whatcanwedowi thast ack?* push-pl aceani tem ont he
stack.* pop-Lookatt hei tem ont opoft hest ackandr emovei t.Ot herSt ackOper at ions* isFul l
:ABool ean
oper ationneededf orst at i
cst acks.Ret ur nst ruei ft hest acki sf ul l
.Ot her wi se, ret ur nsf alse.* isEmpt y:ABool ean
oper ationneededf oral lst acks.Ret ur nst r uei ft hest acki sempt y.Ot her wi se, r et urnsf alse.St ackAppl i
cat ions
*Stacksar eaver ycommondat ast ruct ur e- compi ler s* par singdat abet weendel imi t ers( br acket s)-oper at i
ng
systems* pr ogr am st ack- vir tual machi nes* mani pul at ingnumber s- pop2number sof fst ack, dowor k( suchas
add) .-pushr esul tbackonst ackandr epeat .- artifici al int elli
gence.* findi ngapat hSt at icandDynami cSt acks
*Stat i
cSt acks- Fi xedsi ze.- Canbei mpl ement edwi thanar ray* Dynami cSt acks- Gr owi nsi zeasneeded.- Canbe
i
mpl ement edwi thal i
nkedl ist .St acksPr obl em * Whathappensi fwet ryt opushani tem i ntot hest ackwhent he
stacki sf ull?* Whathappensi fwet ryt opopani t
em of fthest ackwhent hest acki sempt y?* St ackover flow- The
condi tionr esul tingf rom t ryingt opushanel ementont oaf ul lstack.i f(!stack. I
sFul l
())st ack. Push( item) ;St ack
under flow- Thecondi tionr esul ti
ngf rom t ryi ngt opopanempt yst ack.i f(!stack. I sEmpt y())st ack. Pop( i
t em) ;
Not esonpush( )andpop( )* Not i
cet hatt hepop( )met hodf ort hel inkedl i
sti si dent i
calt ot hatf oranar raybased
version.* Onl ydi ffer encei si npush( )met hod.- doesn’ tr etur nabool eanbecausei tcannotf ail.* unl esswer un
outofmemor y.- firstchecksi fthepushwi l
l exceedt hecur r entar ray.* ifso, creat eanewar rayt hat ’
s2xasbi g,
copydat a, andmaket hatt henewst ack.* thi si st hecaset hat ’sver ysl owNot esonpush( )andpop( )- maywant
tokeepasi zevar iabl et hatt rackshowmanyi temsi nt hel i
st .*maywantt okeepamaxSi zevar iabl et hatst or es
themaxi mum numberofel ement st hest ackcanhol d( sizeoft hear ray) .I mpl ement i
ngaSt ack:Ar rayTher ear e
twowayswecani mpl ementast ack:* Usi nganar ray* Usi ngal i
nkedl istWhi chmet hodt ousedependsont he
appl i
cat i
on.whatadvant agesanddi sadvant agesdoeseachi mpl ement ationhave?I mpl ement ingaSt ack:Ar ray
Basi ci mpl ement at ion* ini t
ial l
yempt yar ray.* fiel dt or ecor dwher et henextdat aget spl acedi nt o.* ifar rayi s
full
,push( )r et ur nsf al se.* ot her wi seaddsi tint ot hecor rectspot .* ifar r ayi sempt y, pop( )r et ur nsnul l.
*other wi ser emovest henexti t
em i nt hest ack.I mpl ement i
ngSt acks:Ar rayAdvant ages:* bestper formance.
Disadvant age:* fixedsi ze
StackCl ass( ar raybased)cl assSt ackAr ray{pr i
vat eObj ect []st ack; pr ivat ei ntnext I
n;publ i
cSt ackAr ray( i
ntsi ze)
{stack=newObj ect [size] ;next In=0; }publ i
cbool eanpush( Obj ectdat a) ; publ icObj ectpop( );publ i
cvoi dcl ear ()
;
publ i
cbool eani sEmpt y();publ icbool eani sFul l();}I mpl ement ingaSt ack:Li nkedLi stI mpl ement ingast ack
usingal inkedl isti sn’ tt hatbadei ther …* listi si nitiallyempt y.* St oret hei temsi nt hest acki nal i
nkedl ist.*The
topoft hest acki st heheadnode, thebot tom oft hest acki st heendoft hel ist.* pushbyaddi ngt ot hef rontoft he
l
ist.* popbyr emovi ngf rom t hef rontoft hel ist.I mpl ement ingaSt ack:Li nkedLi st .Advant ages: * always
const antt imet opushorpopanel ement .* cangr owt oani nfini tesi ze.Di sadvant ages:* thecommoncasei st he
slowestofal lt hei mpl ement at ions.Addi tional Not es* Itshoul dappearobvi oust hatl inkedl istsar ever ywel l
suitedf orst acks.- addHead( )anddel eteHead( )ar ebasi callyt hepush( )andpop( )met hods.* Ouror i
gi nall ist
impl ement at iondi dnothaveacl ear ()met hod.- i
t’sver ysi mpl et odo.- howwoul dyoudoi t?* Agai n, noneedf or
theisFul l
()met hod.- li
stcangr owt oani nfini tesi ze.St ackCl ass( li
stbased)cl assSt ackLi st{pr i
vat eLi nkedLi st
l
ist;publ icSt ackLi st (){l ist=newLi nkedLi st ();}publ icvoi dpush( Obj ectdat a){l ist .addHead( dat a) ;}publ i
cObj ect
pop( ){retur nl ist .del eteHead( ) ;}publ icvoi dcl ear (){l ist .clear (
);}publ i
cbool eani sEmpt y(){r etur nl ist.isEmpt y();}
}.

Algebrai cExpr essi on* Anal gebraicexpr essi oni sal egalcombi nat ionofoper andsandt heoper ator s.*Oper and
i
st hequant ity( uni tofdat a)onwhi chamat hemat i
cal operat i
oni sper for med.* Oper andmaybeavar i
abl eli
kex,
y,zoraconst antl ike5, 4,0,9,1etc.* Operatori sasymbolwhi chsi gni fiesamat hemat icalorl ogicaloper ati
on
betweent heoper ands.- Exampl eoff amiliaroper atorsinclude+, -,*,/ ,^ .* Consi der i
ngt hesedefini ti
onsof
operandsandoper atorsnowwecanwr iteanexampl eofexpr essi onasx+y* z.Infix, Post fixandPr efix
Expressi ons* INFI X:Fr om ourschool sti
meswehavebeenf ami l
iar wit ht heexpr essionsi nwhi choper ands
surroundt heoper ator,e.g.x+y, 6*3et cthiswayofwr i
tingt heExpr essi onsi scal l
edi nfixnot ati
on.* POSTFI X:
Postfixnot at i
onar eal soKnownasRever sePol i
shNot ati
on( RPN) .Theyar edi ffer entf r
om t hei nfixandpr efix
notati
onsi nt hesenset hati nt hepost fixnot ation,oper atorcomesaf tert heoper ands, e.g.xy+, xyz+*et c.
*PREFI X:Pr efixnot at
ional soKnownasPol ishnot ati
on.I nt hepr efixnot ation, ast henameonl ysuggest s,
operatorcomesbef oretheoper ands, e.
g.+xy, *+xyzet c.WHY* Whyt ouset hesewei rdl ookingPREFI Xand
POSTFI Xnot ati
onswhenwehavesi mpleI NFI Xnot ati
on? * Tooursur pr iseINFI Xnot ationsar enotassi mpl eas
theyseem speci allywhi l
eeval uati
ngt hem.Toeval uateani nfixexpr essi onweneedt oconsi derOper at
or s’
Priori
tyandAssoci ativepr oper t
y.-Forexampl eexpr ession3+5* 4eval uat eto32i .e.(3+5) *4ort o23i .
e.3+( 5*4).
*Tosol vet hispr obl em Pr ecedenceorPr i
or ityoft heoper ator swer edefined.Oper atorpr ecedencegover ns
evaluationor der .Anoper at orwi t
hhi gherpr ecedencei sappl iedbef oreanoper at orwi thl owerpr ecedence.I nfix
Expressi onI sHar dToPar se * Needoper at orpr i
or i
ti
es.* Thi smakescomput ereval uat ionmor edi fficul tt
hani s
necessar y.* Post fixandpr efixexpr essi
onf or msdonotr elyonoper at orpr i
orities.* Soi ti seasiert oeval uate
expressionst hatar einthesef orms.So* Duet oabovement ionedpr obl em ofconsi der ingoper at ors'Pr i
ori
ty
andAssoci ativepr opertywhi l
eeval uati
nganexpr essionusi ngi nfixnot ation, weusepr efixandpost fixnot ations.
*Bothpr efixandpost fixnot ationshaveanadvant ageoveri nfixt hatwhi leeval uat i
nganexpr essi oni npr efixor

You might also like