Docsity

Data Structures and Algorithms: Exercises and Questions for University Students, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
&
Paper:
Course
UNIVERSITY OF THE PUNJAB
B.S. 4 Years Program / Fourth Semester -2019
Data Structure and Algorithm
Gode: ,T-207 I lT-22408 Part- ll
aaaaoaaaaaaaaaaotaaaa
a' a
i Roll No. ................ :
taaa a aa a aaaaoaaaaao o! a
Time:2 Hns.45 Min. Marks:50
i-. ..
Ouec{on*2.ShortQueetlonr: . - ., ,, f4x5:20ma4pl
al what are the tlme comFlexities of each of the follo$ring code cegmenB? Wrhe tDur answerc in
Blg Oh notation only.
tror(l.nt i s 1i 1 .(- n, ,.++)
(for(int J - 0; J (- 10000; j++)
tt.lttlt++r'
l
l
b) Apply the Qulck rort algorlthm on thls array and show the contents after g[g-ElB!ig!
cl Write down the p6t-fix forms of the fotlowing infix expresslcns {keeping ln mlnd the op€rator
precedence rules of C++):
t+c+'llD-&.rlg
d) Write brlef answers to the followlnE two questiont:
o l{ame one advantage of a llnked llst over an array.
o Name one advantage of an array over a linkad list.
Questlon # 3. Lonq Gluetttons . ,, J2 x {5 --30 marftel
al Given the following declaration of a Orcular.Doubly-Unksd.U* (wlth a dummy heeder node| which
stores integers in unsorted order:
Y vold inrrtltEnd(int Trl,
CDlinkcdLisr class. This firnction inscrB'Vtf" at the cnd of thc tinled list. Th€ tinr courBlexity of this
function should bc constant i.s. O(1).
bt Write a rccurJve C++ function to count all occurrences of e gfucn number t3 in a given array of
integers. The prototype ofytur function shouH ba:
Lnt eount (trat* :rr, int rtaat, trtrt .nd, int Ic)
tntheaboveprototype,rrrirthearray,!t ttandrndarestartintrndendlnglndicesofthearraY,
and k is the number whose occurrences are to be counted.
For example, if the array rrr contaiffi th6e 10 ktttte6 i2rl,?,5,7,?nQ,7,8rQ and we call
count(lrrrOrgr?) thenthisfunctlonshouldretum4(becau:ethenumberToccurc4tlm€sin
the above array).
Note: Do NOT change thcftnction protatype. Do NOT usc qry globaAstaic vafiabl*. 77td
lmplementarion musr. * rciursive. No Markslor itaratlve implamcntation
rhis array once|. Use the first number th€
12 2l5 lr|, 2B t5{8 6 0
{ frirnd clul CD&iakrdllrt;
I}riv.t.:
irlt drtr;
DNo&r tl.xt,
DNod.r !,Rsy;
);
@I"ialedlirt
prlvrt :
DHod. lrmdi
publia!
void inrrtAtDnd (int vall t
l;

Partial preview of the text

Download Data Structures and Algorithms: Exercises and Questions for University Students and more Exams Data Structures and Algorithms in PDF only on Docsity!

Paper:

Course

UNIVERSITY OF THE PUNJAB

B.S. 4 Years Program /^ Fourth Semester^ -

Data Structure and Algorithm Gode: ,T-207 I lT-22408 Part- ll

aaaaoaaaaaaaaaaotaaaaa' a

itaaa Roll No.^ ................^ :

a aa a aaaaoaaaaao o!^ a

Time:2 Hns.45^ Min.^ Marks:

i-. ..

Ouec{on*2.ShortQueetlonr: . - ., ,, f4x5:20ma4pl

al what are the tlme comFlexities of each^ of^ the^ follo$ring^ code cegmenB?^ Wrhe^ tDur^ answerc^ in Blg Oh^ notation only. tror(l.nt i s 1i 1 .(- n, ,.++) ( for(int (^) J - 0; (^) J (-^ 10000; j++) t t.lttlt++r' l l b) (^) Apply the (^) Qulck rort algorlthm on thls array and show the contents after^ g[g-ElB!ig!

cl Write down the p6t-fix^ forms of the fotlowing infix^ expresslcns {keeping ln^ mlnd^ the op€rator precedence rules of C++): t+c+'llD-&.rlg d) Write brlef answers to the followlnE^ two^ questiont: o l{ame^ one^ advantage^ of^ a^ llnked^ llst^ over^ an^ array. o Name one advantage of an^ array over^ a^ linkad list.

Questlon # 3. Lonq Gluetttons^. ,, J2 x^ {5^ --30 marftel

al Given the following declaration of a Orcular.Doubly-Unksd.U* (wlth^ a^ dummy^ heeder node| which

stores integers in^ unsorted order:

Y vold inrrtltEnd(int^ Trl, CDlinkcdLisr class.^ This firnction^ inscrB'Vtf"^ at the^ cnd^ of^ thc^ tinled^ list. Th€^ tinr^ courBlexity^ of^ this function should^ bc^ constant^ i.s. O(1).

bt Write^ a rccurJve^ C++ function^ to^ count all^ occurrences^ of^ e gfucn^ number^ t3^ in^ a^ given^ array^ of integers. The prototype^ ofytur^ function^ shouH^ ba: Lnt eount (trat* :rr,^ int^ rtaat,^ trtrt^ .nd, int^ Ic) tntheaboveprototype,rrrirthearray,!t ttandrndarestartintrndendlnglndicesofthearraY, and k is the number whose occurrences^ are^ to^ be^ counted. For (^) example, if (^) the array rrr contaiffi th6e^10 ktttte6^ i2rl,?,5,7,?nQ,7,8rQ and^ we^ call count(lrrrOrgr?) thenthisfunctlonshouldretum4(becau:ethenumberToccurc4tlm€sin the above array). Note: Do NOT change thcftnction protatype.^ Do^ NOT^ usc^ qry^ globaAstaic^ vafiabl*.^ 77td lmplementarion musr.^ *^ rciursive.^ No^ Markslor^ itaratlve^ implamcntation

rhis array once|. Use the first^ number^ th€

12 2 l5 lr|,^ 2B^ t^5 {8^6

{ frirnd^ clul^ CD&iakrdllrt; I}riv.t.: irlt drtr; DNo&r tl.xt, DNod.r !,Rsy; );

@I"ialedlirt

prlvrt : DHod. lrmdi publia! void inrrtAtDnd (int^ vall t l;