You are on page 1of 11

Cnct -T

Data Sauctures
Set af aues.
Data
means a
value or
dafa
pafa SEvuckure is
D a f a SE a
ay of soring G ogangng
manner
efffcient
an
n
of the daa Su ckurse is £o
The matn ofectioe
The e he
Sore oetrieo e dafa im
effectiuelg Pientty
Mathemolcal ,a daa sbucture D is a 6ipleE,
D - (d,f, a) f= data object (see asOamaetes)
fSetof fumcions
asecsules. lo mrlement-axbons
mefhod of.
CSassication DE-Based on 0ganizég dàta staudue
Dala SeotucEures are cafegoige nfo 2 ypes

Back Dafa Sfouclre s

linea Daa Sfouclures


Nn-dimeas Data Srut
Toees
Arrpas
LisEs (Linked) Gaphs
Heaps
bstackRs Dicionarieas
Queues ec

oinear DS-
dala skruckare is Saiad o be 4inear f tes
Tfa
etemen¬s orm a Seaaenééal ovefer or in a

near ovder.

Mon- Cnear PS:-


A data SEruclare is Said to be mon-1inear f
S etemente are n mdom ooder (i-e: noE in
a Seauence):
hecuxsion g-
Recursion ts a Cechrnque that Soloes a pao6e
paoblem
b soling a Problem of thee
Smalter Same tpe
same

inooking i¬serf, either direc


direck,
A decusive sunctir
or ndisecty.
used as an altesratve Co
Rearsiom Can be

Ceraton
Pwesful booC in
Ke Cudston is an ampoatan& l
poolem Soloimg 4pvogvamoning
Recuvsion ts a progvammimg techmue thatmatural
mpleTments he divde Couer pooblem
Soluine HethodoB}.
oocer C¬eria ofa Keursie Solubion-
1 4 ecursioe fanction calls TEsef
2 Each ecunstue -fanetlen call soloes an denliCal
u t s m a l l e r P r obCem.
mase have a

3. A CesG fo ba se Case (t mus6 6fop'at Some poinE)

m f n=o
f n>o

Destgn He£hedolosyg
The Statemenf that soCves the pro GCem s kvoo
as he base case
2
Ese
Eseng ecuasive atgo»thm must have a base
case The rest of the al8 thm S Knouwmn
as the 3emeral case.

3 The geneal Case contains the lgic neece


the se the
uce of problem
e x - nfactorial example , the base case is

fact (o) the geneval Case a


m fact (n-).
menCatien
Jnyiementatien o ecurst ve poi¬hms:-
the base case
6¬, , dete3mine.
hen detemine the 8eneral case
a
Combine the base a s e e the 8 e n e d a l ases
3
nbo an ago thm .

Limcbatrons

a
on works besE when the alkotthm uses
Recurs TeCursion
data suckare
data Sfruceure
thal maturally Supoals
Toees.

cevtatn alpotthms ave matural"y


2. In other Cases
Suited to. Tecurston .

e Binary seaach , fHanoi touse


nof aLoopîng alporitrms C or

On the other hand,


wfhecursion.
Should be implemerted
4Reurcve soetons may mvo
lir ex¬enstoe ooes head
Choth tme t memovy becmuse they u s e alls
Eac Cale takes m e
A TeCunsiue algoodthm generaly Turs mge 6low
tha Ts mon - Tecws swe tmmplem entation.,

x-GCp pesin :- uen


Gren two Enfegers a
tb, gcd is
ged (65 T Base Case

3cd (b,amod b) Geaal age.


bonacce esigon: To Stat a ftsnacci sene s, we meed
to knou the h s¬ tuo umbers.

fbonacei n) -
5 o fn-o 6ase case
fnar J
fkon)4 Pbo (m-2) Sena v
Lnea Binau Kecsion -
dinea Reazston
the mos E Comnonn e eansion in ohickh
funetion Ca11s tse geately untit base Cre Gom
teamiratlon Case s Teachedl. Once the ba se Cage
esulE s alle an etarn o the
is each ed the
funclton. I f a TecYSue uncben ts caled oml
S Called a lineas TecCurstOn.
Once then
fnding -factoriad af a
umbe
ex
Soct Cint S)

ean Cf* fact (f-));

n&ain C)
nt a t
fact fact (5);
Pointf ( factooiat ts fodfact );

ePfactovtas ts 1a0.
Btnavy Kecunssn
Some recareie fanctton don't juse haue one Cal! to
hemselses , the fave t ( more):GunCeons wh
tworeawse CaDS ae efeoved to as &nary
Decnave fun ctionS.

e in ftb Cint n).


.
fCn-)
Cn--=).
etuan 4 .
ctn 6 En-) + ftbEn-2)

ofe-
3

m s Db

1
n-D+ f?s(n-a)J
4Tb [o) - o

AStSeven
ba) Fb) + Fb (6) = I

ftb(3)ffb) +fbO)
=
a

fb0) sbca) + fb)-3


fbls) fb(4) 4ffbla)=5

Feltminanies oAlgoitfrm.
An algo thm a y be destned as
fnle Seuence
nsfructons each fwhtcA has a ctean mcaning

c a n bepesem ed w th * a n l e amount s f
effetm a ffn Ee tevgth af tme
the Aabic
he atgoritfem r d oviginat eol fsomn
which s nked to the mame
wod Agoris m
the Arabic, matfemiian Al Khuwanzm
of
He is Considered t the Jt»st algoth om
ubers.
desgner for adoling
Souckune Algootm : - 4n algottm has Jollooirg shuckne
Tput S¬ep
2 Assigmen E SEtp

3 Decisn,, 6tep
4. Repetitoe sCep

S.Oubpit step .
.

Algovithm peopeotaS
fnilemess An algottm must termanate ater a
fnife u m b e r o6feps
.

ha
akovithm musE be
-Defomi feness e Sfeps of
defined, ax
Precsel

algethm must be genearc anouph t o


3. Generay 3 An
SoCwe a,ctlems of a artculas class.

be
Effeekioenat the opevabions of algon-ithm must
basfe, enouph to put doun onpenut G
should nof be too Complerc
pape +hey
uxant cocty an othe algooithm or
fo
he operabhm.
S rut-Out The algoithm must hae
a e ccetain
eai m
tn

ypoecise. îrpues , uputs habmm


be generabed kotk at ts mEeomeda
fnal 5¬eps

oo pes
. To Saue tme (Ttme. (mpleiy) :-Aprgram at rng
aslarts a betee
2 To PO m.
Sawe Space (Space lompleay): A
Pogram that Saue
Space ases o
(ompeking
P am is onscolered
Tme Ccanpledt of an
agotttm odestalRe
na
a Prgrarm is
a
funcon of the
sunn time
f the alpooitm
apogoamTn other wodS the amount
Conmpue er ime t n eels to Tn t
,
Coypleton.
of

Space anflaiy of an agoothn opogram ps a


ken ckicn o the
Space needed by the algo»itm
o
regvam Canto Completion
ralysng Alpotims-
Suppge M a n algonithm t Supose n is the
f the opeee- data learly the Coplexiy
a)
.

H increases as nindteAseS. tE S
usually the Take
afncrease o n w 6 Some Sfarcdavd fanckom.
he mos Comon
Computing imeg Qe
oCD,o(la,n) , Om) On), O%*),on®), O0n).
foag waro Sgment A Ps goam Segment B Pgoam Segment C
foo k-I tom do do
o to n
*t2 do
Jo = to n

end
Coune
end
equar ant 3-nt
h e toa! equency Cocrmk fthe prgram Sepmens
A,6C gan y 1 , (n4) G 3*43n4! ekioely
axe ezpoeked ar O(), OCn) O Cn*).
The axe
aeffored to time Complecitfes of the pongsa
segmen since ehey au indicatie of e unnng
ftmes 0t, the PoKram Sepmen k

n a$inflaa rama Space Cnplexeftes e a Pragam


Can adbe be ecpressed in toms afmalemati ca) motahan
phi'ch s notRine bu he amount
omemey
Aauire fr ther execufon

tnkec tsE
> A Linked List s a inear Colectiom of olata
eleiments calle
nocles where the tinear ooded is
gioen by
oineams of poinfer
cach made ts dfuided ento oo
partf
The frcEparE Contains infomatien olement
2 The Second
Jaot Cantains akress o met dode
Cttnk/pointer[ fte ) in the fs ¬
he daba serg in the inked ts ane moE n

Adoanage6
These
These
aT dnamic odata Struchraes.7e.they can groy
or Shoink duofng the execution ofaPogam.
2. LL haue
effcient memooy utiuzaon. ttee
mem oy s n p e - ato Caled. Meme is aloaled
henever tt s equed t iE s de alocated
Csemoved) when it S no Conger eeded.

3Tnserlon Deletions axe easter efeden.


Limked Lis Cs ach Proide flexih& n nseekin e
a daka tem at a Speciffed posCan deletfon o
he data em om he oenpost
pos'C'n .

an be ea auied out
Harng Comper applcations
Mang
n k ts6.

DisadontaeS becaue eveby nooe aeqies a


Cansama
rsam me Space
-to S e a/oess of the nect
addtkonal poneer
2 Searchng a pa C'culas element n 18s ¬ is

diffteult also time Con6uminf.


Snle Lioked tis¬:
The
The Smplest knd of tinked ust is a S-L-L
which has o n e Cnk per n0de
Ths link oints fo the noct mode im the i'st,

a null value empty


ust
if iE t h e
fral nole
A S.L.L node ts duided nto two pa6.
The s pavf holdS o poinls nfomation
abou he mode Secovod pat hols the
address nere nodt.
A S.LL tavels one
The L En whteh, atul nodes are tinked tether
tn
Te s
sme 5equen fal oname
a1So alted as tinean
s called af.t.t.
Pnked drsl -
eslokes s t is a Seutnce g elemen s n
eeeyeemant Aas link t n element in the
Sequen Slses addsay mat node
ata Cink
Mode Sto Actua) uhle
4dre
04 o8
18 19 55
10101

Aomt

Cperafons n SL
The ollouuing optabiom oopeformad On a SLl
rsortien
Trsextion, Deletion ana Searching
Po need to
eehaeue fmplernenb ackun epexatiana
un

Sel pan ernp isE

Skp1 nctude al headix ffe ytd tnth pgam


Step2Declaxa all uer dehimad fanckians
Stp3 Pegtne Node u h two mumbed olaka G. NEXT.
be ffne a e l e poinka head setie s NOLL
SReph
Steps rmplcment the maín mekod ly diplayfng peatony
nsertion -

Thsertom
opaalon Can Be peroome en thra

(1-Trsetin At Beginäng of lis¬E


we e the followtg steps to s e r t a new node at bginain

of the tnge umked lts


Caeate a meLnbde t h gRen Salw
Stepl check h ether 4sE fs emPEy head = sOLL)
Step2
s hen neNodednert = NuLand
Steps-Tf it rnpty Se

tt ts noE Empty then set He ad= mewnbde


neundde
Tere head and heao =neunwod e .

tnselig a t énd of tist


o tnsests Mew node a
we use the folowing s¬eps
he sngle l n Kecd tCsc
end of
ith gven vaue and
Coeale a mewbde
Stept meat as wULL
newnode >

check whether sE ts
Emply (head= =rkoe)
Stee?
then S e t head neuode
-

Steps If e is mplhy
S ndot mpey then , defne a node pointer Lemp
Step Tf i th head
and tnicalge
next node untit it
Steps-Keep mOving thhe temp to its

7eaches to ast ode tn the uSt (CuntiC femp next


s equal to wuil
mect newnbde.
Step 6 Set temp =

tnseafing at Speole Locafton n s Steps


Can u se the folocw ne to inseat a new mde
ater a nade LsE
neNode usth gven Salu e
Cyeale a
oL)
Stepz -
hecK whethev ts6 ts Gmpty ( head--
next =NUL
E 1s rpty Hhen Set menocde
Step3 I head TeuNadHe

define a nocle Po?mter temp


te4I t ts Not Ep then head
with
ad initialige
cts next mode unt?C 7eadhes
the temp to
StepsKeep movin uhth we uant 6o mserst he
to the. nade alter
me untft tnp-Sdota is ual to oatin)
de
S 7eached to 1aSE
e y tme check whethe Cemp
rE 15 deached to laSE modethen
modc mot If or

th st
Gtven rode is not fourdin
display cmp to neslnal
bthewise move
nseotion Ts nel poss e
Stepi fal See meuNode mext

temp ne re newWode

Dekion;
nSingke inked 'st deleeion pealon Can be Petomed
thdee.
9ays
Defetfng kom Beginnine o Gst
sC s emy head ==OLL )
Sepl
rpl Check whether
'LisE s fmpyDeJe
Stp2 If it 7s
Enyd,
Empey then di's poy
tesminale
the fur
is mo possiECe
Step: f * is ot mly then des?ne a 1Wode pointe 'Lerr
lernp
o ttialize uuith head
nde
Stpu check whetherr (ist s hauirg Dnly Cne

temp mext ==NULL)


Stepsa If tt s TRUE the Set head= NOLL delet e Lem
Settine rmply CËst Coodî tions)
Step6 If is fALSc then Set head temp net and
de tel ee
Cemp
7 Deletien fiorm Eod of cst
check wheeher s is Ernpty (head=NOLt)
Step
-Tf 9s Croptg then lisplay Lfst s Grpty Deletin
Step2
S o t goss ble terminal e the funeion.
Step3 Tf i t s no(- mpty,then dejfne wwo Ncode ponters
templ and 'temp2' inilalige temp1 wth heac
Step4 check hethe ist has ony one Made (lemp s nel=
Steps t ss TROE then Set hed = NoLL and OLLY
deleke temp1
Stepc Tf t t s salse then, set
emp1 to S nelmode
temp 2 - {emp1 and move

2aches Lo the 1ast nRepeak


ode
the Same ntl
the
ani
NULtNULL )
Step1 ral Set temp2 nex = NULL delete kmpl
i Dele ting a Speuf wade
checK whether tst is
Sep1 Gop head= NOL
Te s
Step2 T nply then
display s t ts 6nply I Deletion
,

netosS ble& teminake e fanctiom


IFt s n t Gmpky then define two node
'tcmp1 emp2 ad intalze tcrnp 1 R
potnER9
hcnd
SEcp keep movirg the mp1 ent?t eahe ts
etact nle to iRelec o -
And eeery {ime Set Ectp2= tcmgt belpae mmen
temp tmeat nale
Tf
' tis s each ed to last made then
splaiy
oen made noé found in the ist
D elelfon qot pogstd
s deadh ed to the. e1act node ohich we want
hen check cohether
t o d e l e l e

, 5 is havinP np
MAe D MOE
one
has only one eole thal s he node tu
T s
be deleted , then Set head = oLL E delete tmp1

f lis Cohtains mceltpe modes, then check heho


Seep8 tep1tep 1 s the fhst node in the List (fempl==head)

he fast mae then m o v e had to the


Step9 - sf temp1s
nece nodle ( h ead = head nexk) and delete knp1

first de then check hethes


steploI temp1 s
not

at Rs a s t noole in the i s t Cenp1 n e t = =voLL)

I f ¬emp1 s 1aSE node then se tempa > next =NOLL


Step
delete tenmp1
S net fost Mode G natlast nole then
Step12If femp 1
Set temp2 ment =
temp1 mext G delete temp1

eavchcng- ement in
list neans Eo fnl ootícula
Semching a

hd st- t s

Thae are two oulcme for Seaach?ng one (s mode


aods and other ls NULL
ented tn the ust then it
The given ke elern ent is pres
c is TetPoaente
wl debuah h e . node acbreSs,
then t sil nekron ULL

Sap

You might also like