Data Structures and Algorithms analysis Midterm
Questions
Q1)A: Use the stack to convert the expression below into suf x form, show the state of
the stack at each step.
(S − A) / (D + Y ↑ 2) + R * K
Answer:
Current Char Stack content Output Rank
( #( _ _
S #(S _ _
- #(- S 1
A #(-A S _
) # SA- 1
/ #/ SA- 1
( #/( SA- 1
D #/(D SA- _
+ #/(+ SA-D 2
Y #/(+Y SA-D 1
↑ #/(+ ↑ SA-DY _
) #/ SA-DY ↑ +
1
+ #+ SA-DY ↑ +/
1
R #+R SA-DY ↑ +/
_
* #+* SA-DY ↑ +/R
2
K #+*K SA-DY ↑ +/R
1
⏎ # SA-DY ↑ +/RK*+
1
1
Q1)B: Evaluate the expressions below then write their original forms:
a) 4 5 8 * + 12 3 / −
b) 10 8 − 5 27 3 / * +
Answer:
a) 4 5 8 * + 12 3 / -
i) 4 40 + 12 3 / -
ii) 44 12 3 / -
iii) 44 4 -
iv) 40
Its original form is:
4 + 5 * 8 − 12 / 3
b) 10 8 - 5 27 3 / * +
i) 2 5 27 3 / * +
ii) 2 5 9 * +
iii) 2 45 +
Its original form is:
10 - 8 + 5 * 27 / 3
Q2)A: Brie y explain the main characteristics and properties of an array
Answer:
An array is a set of elements that are homogeneous, which means of the same type.
Ordered, which means there is a first, second, third …. Elements. Finite, which means there
must be a last element. Fixed size, which means that the size must be defined when the
program is written and cannot be changed while executing and the insertion and deletion
operations are not allowed, but changing values of the elements is permitted. The array
takes consecutive memory locations.
Q2)B: Assume that 8 bytes are required to hold each element of the array with 5 rows and
6 columns.
1. Compute the total area needed to store the array
2. Use the row major formula to nd the location of the element before the last one
Answer:
1. Size of the array = Rows * Columns
Size of the array = 5 * 6 = 30
Area needed = size * cells
Area needed = 30 * 8 = 240 bytes required
2
2. The element before the last one is (5, 5)
Loc(A(i, j)) = Loc(A(1, 1)) + [(i - b1) * (u2 - b2 + 1) + (j - b2)] * 8
Loc(A(5, 5)) = Loc(A(1, 1)) + [(5 - 1) * (6 - 1 + 1) + (5 - 1)] * 8
Loc(A(5, 5)) = Loc(A(1, 1)) + [4 * 6 + 4] * 8
Loc(A(5, 5)) = Loc(A(1, 1)) + 224
Q3) Write a steps to create a singly linked linear list represented as a Queue, then state
the steps to perform insertion and deletion operations
Answer:
To create a linear linked list:
1. [Check availability list]
If (avail = null) output underflow in availability list and exit
else
newNode ← avail
avail ← link(avail)
2. [Use the new node to create the first node in our linked list]
We need two two pointers since we are representing our list as a queue
We will call them front and rear
link(newNode) ← Null
Front ← newNode
Rear ← newNode
3. [finished] exit
To insert to the linked list that is represented as a Queue, we insert from the Rear:
1. [Check availability list and bring a new node if possible]
If (avail = null) output underflow in availability list and exit
else
newNode ← avail
avail ← link(avail)
2. [Insert the new node at the end(since it’s a queue)]
link(newNode) ← Null
link(Front) ← newNode
Rear ← newNode
3. [Finished] exit
3
To delete to the linked list that is represented as a Queue, we delete from the Front:
1. [Delete the node at the front and store it at a variable (deletedNode)]
deletedNode ← Front
Front ← link(Front)
link(deletedNode) ← avail
avail ← deletedNode
2. [Return the deletedNode to the availability list]
link(deletedNode) ← avail
avail ← deletedNode
3. [finished] exit
Q4) A: State all the cases to count number of elements reside in Circular Queue
Answer:
Q4) B: De ne then compare between Static and Dynamic Variables
Answer:
Static Variables: They are the variables that are fixed in size, which means after defining
them their size cannot be changed and the compiler has to know their size initially, which
means insertion and deletion operations are not permitted. All the primitive data
types(Integer, Real, Characters and arrays) are static.
Dynamic Variables: They are variables that can be dynamically resized while executing the
program, which means insertion and deletion operations are permitted and this means
they can grow and shrink in size. List and files(batch) are dynamic.