Sie sind auf Seite 1von 7

Solution Mid Term Examination

CSE 205:Data structures

Name:Sandeep Kumar Section:C2901 Roll Number:A-12

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
–Part A

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Ans1(a): The Complexity of an algorithm M is the function f(n) which gives the running time
and/or storagespace requirement of the algorithm in terms of the size n of the input data.

Ans1(b):It is as easier to implement the operations on a circular header linked list as in ordinary
linked list. The easyness in the circular header linked list than ordinary linked list is not much
significant. But it comes from the fact that it is easier for the computer to look or search for a
valid address than a NULL. In case of header circular list we have to check for the starting
address which is a valid address while in case of ordinary list we look for NULL. If a large
number of operations or repetitive steps are to be performed then it will be easier in circular
header list than the ordinary linked list.

Ans1(c):Various types of queues are: -

1) Single-ended-Queue:A queue in which deletions can take place at one end called front and
insertions can take place only at the other end called the rear.

2)Double-ended-Queue:A queue in which elements ccan be added or removed at either end but
not at the middle also called as a deque.

3)Priority Queue:A queue such that every element in it is assigned a priority and elements are
added and deleted such that :

a) An element of higher priority is processed first than with lower priority.

b) Two elements of the same priority are processed according to the order in which they were
added to the queue.

Ans1(d): The properties that a recursive procedure must have is :

1)There must be a certain criteria(base criteria),for which the procedure does not calls itself.

2)With each call the procedure proceeds closer to the base criteria.
Ans1(e):Stacks are used in those situations in which there is a postpontment of a decision.For
example, stacks are used in process schedulling,implementation of the towers of hannoi,binary
trees etc.

Ans2(a):1)Number of elements in an array is found using the formula:

N=upperbound-lowerbound+1

a)So, for A(5:50): N=50-5+1=46

b)So ,for B(-5:10):N=10-(-5)+1=16

c)So,for C(18):Since no lower bound is specified we take the default value i.e. 1

so, N=18-1+1=18

2)Location of a memory address of an element of an array is found using the formula :

LOC(LA[K])=Base(LA)+w(K-1)

Base (LA)=300

W=4

So,the address of A[15] will be :

LOC(LA[15])=300+4X(15-1)=356

So,the address of A[35] will be :

LOC(LA[15])=300+4X(35-1)=436

Since the array A contains only 46 elements so A[55] is not an element of A and hence will point
to any location in the memory.

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
–Part B

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Ans3:

Algorithm 1:INSERT(INFO,LINK,START,AVAIL,ITEM)

This algorithm inserts ITEM in the list in a sorted manner if it is not empty otherwise creates the
list and then inserts ITEM into it
1) IF(AVAIL=NULL) then Write:Overflow

Exit

Else

Set NEW:=AVAIL

Set AVAIL:=LINK[AVAIL]

Set INFO[NEW]:=ITEM

Set LINK[NEW]:=NULL

[End of If structure]

2) IF(START=NULL)

START=NEW

[End of if structure]

3) PTR:=START

4) PTR2:=PTR

5) Repeat While(ITEM>INFO[PTR])

6) IF (LINK[PTR]=NULL)

LINK[PTR]=NEW

[End of if structure]

PTR2:=PTR

PTR:=LINK[PTR]

[End of step 5 loop]

7) If(PTR=START)

LINK[NEW]:=START

START:=NEW

Else

LINK[PTR2]=NEW
LINK[NEW]=PTR

[End of if structure]

8) Exit

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
–Part C

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Ans4:

Algorithm 2:HANNOI(N,BEG,AUX,END)

This procedure is a recursive solution to the Tower of Hannoi problem for N number of disks.

Step 1: if N=1,then

a) Write:BEGEND

b) Return

[End of if structure]

Step2:[Move N-1 disks from BEG to AUX]

Call HANNOI(N-1,BEG,END,AUX)

Step3: Write:BEGEND.

Step4: [Move N-1 disks from AUX to END]

Call HANNOI(N-1,AUX,BEG,END)

Step5:Return.

Suppose BEG or stating peg is ‘L’ and END peg is’C’ and ‘R’ is the auxillary peg AUX , then
the solution of tower of hannoi problem for number of disks N=6 is :

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C


Move the disk 3 from L to R

Move the disk 1 from C to L

Move the disk 2 from C to R

Move the disk 1 from L to R

Move the disk 4 from L to C

Move the disk 1 from R to C

Move the disk 2 from R to L

Move the disk 1 from C to L

Move the disk 3 from R to C

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C

Move the disk 5 from L to R

Move the disk 1 from C to L

Move the disk 2 from C to R

Move the disk 1 from L to R

Move the disk 3 from C to L

Move the disk 1 from R to C

Move the disk 2 from R to L

Move the disk 1 from C to L

Move the disk 4 from C to R

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C

Move the disk 3 from L to R


Move the disk 1 from C to L

Move the disk 2 from C to R

Move the disk 1 from L to R

Move the disk 6 from L to C

Move the disk 1 from R to C

Move the disk 2 from R to L

Move the disk 1 from C to L

Move the disk 3 from R to C

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C

Move the disk 4 from R to L

Move the disk 1 from C to L

Move the disk 2 from C to R

Move the disk 1 from L to R

Move the disk 3 from C to L

Move the disk 1 from R to C

Move the disk 2 from R to L

Move the disk 1 from C to L

Move the disk 5 from R to C

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C

Move the disk 3 from L to R

Move the disk 1 from C to L


Move the disk 2 from C to R

Move the disk 1 from L to R

Move the disk 4 from L to C

Move the disk 1 from R to C

Move the disk 2 from R to L

Move the disk 1 from C to L

Move the disk 3 from R to C

Move the disk 1 from L to R

Move the disk 2 from L to C

Move the disk 1 from R to C

Das könnte Ihnen auch gefallen