Data structures and Algorithm
Analysis Questions Group 1
Q1) Let a non-empty Queue of size N, write the steps to count how many times that same
value appear in adjacent location?
Answer:
1. [Set variable count to 0]
count ← 0
2. [Check is there’s only one element]
If R = 1 output “Queue has only one element only” and exit
3. [Set pointer p = front]
p ← front
4. [Loop while p ≠ rear and check if there’s two adjacent values that are equal]
While p ≠ rear {
if queue(p) = queue(p+1) then
count ← count + 1
p ← p + 1
}
5. [Exit] output value of count and finish
Q2) A- write the suf x and pre x forms equivalent to the expressions Below:
I. (A / 4 + D) ↑1 (Z / R) ↑2 H
II. D * (C − H) / F + G
Answer:
I. A 4 / D + Z R / H ↑2 ↑1 (Post x)
↑1 + / A 4 D ↑2 / R Z H (Pre x)
II. D C H − * F / G + (Post x)
+ / * D − C H F G (Pre x)
Q2) B- What is meant by Dynamic Allocation, what are the advantages and disadvantages
of that representation.
Answer:
Dynamic Allocation representation means that a given data structure is represented
dynamically without any random(direct access), this means the dynamic data structure
can expand and shrink in size while the program is executing, but there are disadvantages
that include the need for more space to store pointers and the access for elements that
are represented dynamically is sequential(NOT direct or random).
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