Professional Documents
Culture Documents
1
B- What is meant by linked storage representation, what are the advantages and
disadvantages of this representation??
Answer:
Linked Storage Representation means that the order of the elements is determined by an
explicit link field in each element rather than sequential position, which means the logical
order is not equivalent to the physical management. The advantage is that this allows us
to change the size of the storage(By shrinking or expanding) while the program is
executing, while the disadvantages are that it takes more space to hold the link field and
the access is not random(direct)
Q3) A- State the row major formula that is used to compute the location of an element in
a two dimensional arrays.
Answer:
LOC(Ai, j ) = LOC(A0, 0 ) + ((i − b1 ) * (U 2 − B 2 + 1) + (j − b2 )) * C
2
B- Write a complete algorithm to:
1. Delete an element from a circular Queue of size N
2. Insert a new element to a stack of size M
Answer:
1. [We have a Circular Queue called CQ, of size N, element is a variable that we will
use to store our deleted value, Front and Rear are pointers]
I. [Check underflow]
If Front ≤ 0 output “underflow” and return
II. [Delete Element]
element ← CQ[F]
III. [Is Queue empty?]
If Front = Rear then set Front = Rear = 0, and return
IV. [Increment Front]
If Front = N then
Front ← 1
Else
Front ← Front + 1
V. [Finished] return
2. [We have a stack called Stack of size M, element is a variable that holds the
element we want to insert, Top is a pointer that points to the top of the stack]
I. [Check overflow]
If Top ≥ M output overflow and exit
II. [Increment Top]
Top ← Top + 1
III. [Insert new element]
Stack[Top] ← element
IV. [Finished] exit
3
Q4) Write steps to search for a given value in a singly linked linear list its head given by
pointer variable (First), and whether it exists or not?
Answer:
1. [Define variables]
value: which holds the value we are searching for
p: which is a pointer that we will use to search the list
found: which a boolean
2. [Set their values]
value ← the value we need to find
p ← First
found ← False
3. [Loop through the list until p = Null]
while link(p) ≠ Null
{
If info(p) = value then
found ← True
break
Else
p ← link(p)
}
4. [Check if value exists in the list]
If found = True then
Output “Value is in the list”
Else
Output “Value is not in the list”
5. [End]