DS Module 1 Solutions
DS Module 1 Solutions
DATA S TRUcTURts
. ohat s iy
Disus te upxatation
linou algontm
n
elenent
a iea?
daBahpe aray-nanni (iie-q-aTy))
Ant alst
int als;
The pttodal eprentatipn
a hea tart
0, which jio the lowa bo unddr,
wod
b low bound
Skpi : start
Stp.2: Raad pos, elam.
element
a(i+1]: al:);
4: Dnset the eleent at th
Jfep
a(posj olermj
Skp-Si Tacvcmnt the e the atay
n:nt|j
aliJ aCrtJ;
3 elenent in the ar ay i
'4 Decnment the
St
&. Daota blo Jtatii and Dynamt nenoy alloa
moy alloahbw funcim
MA
Statie N,A Dyrame
o allocated while
Menoy
program Run- me allocat
bme aoat
Can be charigd
. Ex: Arvaiy
DMA Fnchms:
() MALEOCt):
fusneg a blok
his hunchion
tte
block b atoeat roke
Bue
a NUL value.
t utung
allocation then
yntar: maMoc(e);
ptr (data- ype a)
ptr the voálade ty daut
data
dota.pe è any basic
dato ypc
stu ia the umba byte the block
mony
ptx (nt *) maloc(D);
Alocats Hock
) CALLOtc):
locks
This tunebom alotaln space for: mulbpA
menong each and. tnlhial'es. al g
to holo).
yntar si);
ptr- (data-type ) caloclpb.
bohe n: the nembe o locks of munsy to be
aloatid
block
Siee si2e each
esiy
eatbocto the blok nmoy hat
" This
kun c i .
pont o he
when
a NULL value and Jhu old block Aenars unhangad
prsiousky
) FRE EC) alloralod
Thes tunion Re
mallocC, (alloc (
yntar'
ablotato nly
Syator Syntar
iihalie Jnihaliy aloated mumoy
"-It - ddou not
vouo
'Contis
pt malot (ro); allocatid to
pO byteso
vad maih ()
srahgs Coreputn
Stnng Computn deimca and Engiheehy
sha ck
5. Dinu Stack. Disus how to
o data suchue
Sen: AStack s a specia dyp in head
(Lhear S) in wich the lment
st
the barne end. S t ennt to be
this ti last
to be Deled Out.(LIFo)
Ihseited is the
(topSizE-)
SYZE
the aboe
conditio faio, item canbe hsetd
in the stack stack
Dhe londii'on s t a , ten st the
Aneasecd Ae aeallot( kunBon and then the
int te stack.
To t Top mst jnesomaled
SZE++j
s=(nt a) neallor(SIZE «
dene STACK-SI2¬ -5
#
int top
B(top STACE-NZE,-)
ehn
popl)
t
pnH(" Stalk b
thnj
3
void mauh ()
int choia)
top:
break;
ntetetttd
iem popl);
nglyin"
brtat)
Cal-3
dis ptay);
brtat;
dalaut: uit(0;
o'd
4 (top= S12E-)
: s[top;
item- delt1d dutid
tuin itam- ; .
pront("Cortent
woid mah t)
at top :-l}
mt #a3 choia)
torli)
poht("4. fush op\
pont ("rta You Choia:"
Swittehlchoice)
Case I:
prmt (" Entu the item to ba.
breat j
sBem 'pop(ktp s );
item)
Cae 3:
brtab,
3 deluut:
1. Wtt a C prog Tam to cheek for a palindoe
gtacks.
| ihud <stdio.h)
void man C)
the ecorcs
Combm g
Qre
more opyatios
kit ieah tör the, loat?.
a eoeo, hushe must
·the eeord.
LOite a C program fa C lor -te
cpurattu:
n eemnt
In teutng lement
Deleting an
elemens
Diaglay Array
&t.
+ inuole <stdo. h
(pos>Alpos<o)
pnnt[" Invalid porlko ln
a[11J: a
yehn n+lj
etn n-lj
oid man ()
choia, n, tem, pa
pnint("1. Inut
pont"3. Diapayyouu choia\n
tu
&witehlchotu)
eritlo
de<aulti
diaplay(al
n) ),
3!
Cax
breat
n, delee
pa(aC), n:
n"))positioo: the Entu PL("
Case9
break;
po)) 0, Crt,aem inut n:
pokn:
"\r the Etu B"
o be lenent PB"Entu
the
Casel:
blw uttwus and Unin. fhow
kkanpus a both
Stuitwis Unions
Degired by he teyloodl
kyword
unio
Sluct
shae Q comon
Stutues alocats Unions
memoY
spa ca bos al
Ahovagmembs
oreuyy Smalle emoy
Stuetus oecY
dargu mamoy pa membe a wbn
Can acces l
Can be Qceemo at a the.
te embes o the
Stsute t a e
Aame or all
Addrs
Stu c e wion memheS.
(0 Steeus
t mcede Kstdio.hs
Stuet tucent
gtuent sl;
Pnt marts sl.
chal
sl. peacant
() Unioes!
vold mnh C)
t meude <tdo. h
union Sbuoden t shuolent sl
sl, ma ts = 8s
nt markj P"mabs = /.dn' sl. mark
char qrad. :"B"',
ttoat pesant
sl. perent - 85
qrade
Percent S.00
chaatu oluncd by'lob'
t4lmimatd by null
'o' erept
Ths,
stren():
dmy sten(chorstr(3)
tot
int izo)
ittj
ehn i
shpy:
mstry(char stlC], kas' st
vold
vokile (str [] != )
(ti)sttat (): i
strmg ntr ho
the contvt
tunctions cepie
the
joj
stra[i Yo'.
3
Smtar: fat
the conen to
This funchions compas
oith the 'rst chaya ctu. o
The companDin bugihs
the
conhhs h the cRresonding
The compauston
no,
Nsty 0.
tuntion.
strmp(ehar stol), har tt ()
jnt my
'\o')
breat}
ittj
A matix contaningCalled
manysparse matnes.
hon-22ro ntties are
fat ow)
int val}
22
15
witt
The abou matdx alor
The
oe col pofitors
hay as folloer.
Col val
Row
alo) 5 Rowfo]
3 40
fowl
11:
22
als) 3
alc) 3 3
a 4
a3] 3
ontuns all
* hee ow
not pto
Coideled in his epreentat
tund" to traupose pa%e mati
TERN b0) n TE{M The
vold transpo (TERN aC), dato typ
monl)
alo).ow [o). ol
a[o].va, jtt)
addres
rmemong
The to mort mportant oitt the potr
Let oid
?vaue Can be
mtopreid bale
to test
Blp::NULL)
viabe
Ous
1. Reverse the su 4 matix by ntschang
coumns.
bo].ow a[o]-col;
b[o]. col = a[o).ow;
blo]. val alo).al
&
tore eleunb
L[k).
b[]-col
aj]-valj
3. Kepeat slep until al e non- eo elementb n the
mat i
a the nef
Inumunt the yalue to
ay
s. stop
Studunt 4
pelcant, sloat
gracdj chel
masts,
stuet
the allows
typu, data the Q'
mort
grad
picentj toatchor
at
maxts
rtudent stuet
dggunt
wrdua
pmgdada mlsjpl datu
qroptypu
sam vanabls
of
bype mmpe mSt the H
c swsedAncle Stu 0)
enbs: orth
typce. data
nplementation Erglain.
ternal Stet the Ddinn 14.
utea type Explain
044
3, shueue fantw! alloatb
dynamally al
membes o He s t e
and acces th
m ony
stnut Prson AplSon pt (tuet Pescon)
per son pt mallor(si2)
4 Netd Stuctses:
A tmucture . embidded
data
compl
Stuet
nt monthj
int day
geaij
Jdatj
ohar name l
dati dob
3persony
Jntuunal Jmplemetat ion Sthutws:
The
mmbes th stucue.
dufined
Considu the Atuaws.
puon
int ag)
doubu 4alay'i
Jo a: §"RAHUU', 42, 35000} )
The pactonal epresn tatin o how the atoe Valuss ae
helow:
Sine
1
4alary 3 so oo. 00
detohibin
hat h staaaa adce membu name
Adds
Add. agt
Addn stautig adas +
kalay
LOO7 t 4
Add.salay
Jtaek. LIAt and Enplaun Naous opsatibm
stat ovfow ata ck
udonow oncinu.
data
dol?: Astalk a
ohich an leent
and deltd hom th same enel.
last nt wtd
deetud Out (LIFO nethod)
Staek Qpucctions
can be
The vOlbw opacinu that
stacks
) Pah 0puatim:
lument to a Atack is talld push
Tnsing the stack
opuatiori item Can be inseted
mtu
tte
6ne
tu ia meeted Am
the
t a tine and
Jtaek.
ito the ttaCk,
ting an elumt
mst cheek ohethe a staek ww
an elemud into
to set ovfow.
Awulti in stack be
the staek ad top
Let S2e
the top q e Atek.
ben te tack tull loefleu)
:siZE -,
Code flous:
ainglenend
toptIj :top
ovalow'; Stack PC"
-)sI2E topz
) int etop,push(uit void
(optona): Ancton
witig ky
lement the Then
thedemut,
an
t be
must
tn
Ovuftow! ("tak prh
Size-1) Ctopz y
po operatn.
puting an elemunt
peraton.
thu the sack
dement itn is oeltd hn
Bdu dltig an lt m
the stact
eheck vohethe
daate item n an empy tack
stack wndupow.
en the coole
This conditui ke anplemntd
tn he staek is
y (op - )
etunj
the stack, a
To deate defehed and
Valable called
the ttom that
jbem dlitd.
B(Stack Unduflow
3ind itomdeleted
vetan itemdltd,
( Dia play opuátion: the
al t e contnts
ephation,
stach
daplagyd. a stalk, cne must
Beydu diaplaying Conlent enpy oherwi,
check ohethA he stack
Undetlao'
Then tsed
a Back, udl it eah te top valuu
tunction: (ogtionas
voidl display (nt top, nt st0)
top-)
PC"staek wrollo);
3
to conit
functim and
6) Tnyx to Postfx
Step I: Staut
Stap 2: Read i Jo sC), top, symbol
Inikolie top-l j
j0;
Stap 3: for each
s(tttop] sunbalj
(aj4+]9 foquhs
) Zner bpreltr'
). Stast
pet
3. Reves the
streln,
4. tor each
greatu preden
preyex [+tj):s(top-2 than mut synl
4ctual
str rer geg
6. End
Huntn:
char
strv(n
7or(ieo, i ctrlenlnt), itt)
| - yin
ohle (FlsCtoy) >
pron:fp nPop fromgtack .
3strfprt),
sygjtt:s(tp J
strrer prf),
and multt
odo you apent sngle
dinunal
allocat9
Sol": Dhenaional ameysi
one-dienslanal aiay
Nemdy Macco
mallo hunchon,
httqens
wing art dyoaay
allocate menony to
ven belbw:
ng maloc
int
MALLOC-INT(ot )
ecitlo);
3
ntcgtry th funet'on MALa INT)
To allocat memYy kor
s calhd the s t this furet'on co ehund to
a pomka vaable p e code
Junk vae
P: MALLOc- INT(S))
2
" Similaay nemoy Can k alotaled to Koat, cha
and doube voaiablu
voiasw by wsng tloat, chas and double
) To allocate ponter:
Consid an aray oith xt x
3 vOus,thue
alloated to 3
xt]. The be
memoy Can
mallo () folloui
MALLOC ( ows ot); 3rs
ro ws 3
iey MALLOl, 3, t *); x
eech gw wotth spedfcd
( ) To alloeate memoy to
coumn: each
ortk 3 ows,
Consio the same aray
memoy can be
het the
S colum. Thes
haty
allotatd
ound
malloe 0 as follbusi
MALoc(*lo],6, o t
MALLOc (xC1],s, hte);
MAL Loc (x(2J, s mt)
two
maun
vad () vold void
PkC ¢*C)ntt*bnt**a}ot
add.orray mead-array
t9 Entu 3
the tot (0,
Sa **a,
iot Hows,
the **b,
a)\n; bot
mat nt
**G oy
nt
ous
iht
ob)
L
add(a, C: P&l Pl" P"Entu a=
dinplay-read-array
mak Entu Matir mak-
b.c, oayla
arvayrows, th arsay
TowS, A:(a, the
elemento \n"; ows,elnent lo
olo)
rous, b)!,
ob; co)
o co)
&i\n')
Natnk Mati
A:\n");
int 3
stlen );m else
pat.mato fosj0)
utn k
G33n) breatj
int Borhhue itt; i:E)
pl, j< Cprogam
wnt n)jtt)
ttgtet
t) to
seueh
pattn
Veln'p
\a; poihon
tound at
found
\n" not
tk); stnhg:
pas: Itg
t);
pat.matulp.
8,
por(p tert
Pottun
str. the
Entu PC"
tag -etin
C)
man
vord PH"
ot
3