Data Structures and Algorithm Analysis Questions Group 1
Data Structures and Algorithm Analysis Questions Group 1
1
Q3) A- State the column major formula that is used to compute the location of an
element in two Dimensional Array
Answer:
LOC(Ai, j ) = LOC(A0, 0 ) + ((j − b2 ) * (U 1 − B 1 + 1) + (i − b1 )) * C
Q3) B- Write a complete algorithm to:
1. Insert a new element to a Circular Queue of size N
2. Delete an element from the stack of size M.
Answer:
1. A
[We have a Circular Queue (CQ) which is of size N, element is the element we want
Insert, Front and Rear are pointers that points to our front and rear]
a. [Check overflow]
If Rear = Front and Rear ≠ 0 then output overflow and exit
b. [Increase the value of Rear]
If Rear ≥ N then
Rear ← 1
Else
Rear ← Rear + 1
c. [Insert new element to the Circular Queue]
CQ(Rear) ← element
d. [Check if the element inserted is the first in the Circular Queue]
If Front = 0 then Front ← 1
e. [finished] exit
2. [We have a stack called Stack, of size M, element is the deleted element, Top is a
pointer that points to the top of the stack]
a. [Check underflow]
If Top ≤ 0 then output “underflow” and return
b. [Delete the element that Top points to]
Element ← Stack(Top)
c. [Decrease Top pointer]
Top ← Top - 1
d. [finished] return element
2
Q4) Write the steps to nd the smallest value in a singly linked linear list its head given
by pointer variable (First).
Answer:
1. [Assume that smallest is a variable that holds the smallest value]
2. [Set smallest to the value of First]
smallest ← info(First)
3. [Assume that P is a pointer that we will use to loop through the linked list and set
its value to First]
P ← First
4. [Loop until the end of the linked list, testing the value of smallest against info(P)]
While link(P) ≠ Null
{
If info(P) < smallest then
smallest = info(P)
P ← link(P)
}
5. End