Upload
programming-passion
View
350
Download
28
Embed Size (px)
Citation preview
Air University Multan Campus
Mid Term Exam
Course Title: Data Structures and Algorithms Course Code: CS-213 Class: BSCS III - Fall 2013
Total Marks: 35 Time Allowed: 2 hrs.
SOLUTION
Note: All questions are mandatory. While attempting the exam, please ensure that you don’t change the sequence
number of any question attempted out of the given order. Also attempt one question as a whole not in parts.
Q 1. (A) What are advantages and disadvantages of representing a group of items as an array versus a linear linked list?
Array Linked List
Advantage: Direct access via index in O(1) time Space is required only to store data: no
pointers required
Advantage: Sequential access via pointers in O(n) time Dynamic data structure: can grow
dynamically/variable sized Insertions are deletions are O(1) time
Disadvantage: Static structure: fixed size – in case data items
are lesser than the array size, rest of the space is wasted similarly if input size is larger than the allocated memory space, array cannot accommodate surplus data items.
Insertions and deletions can be O(n) in worst case if readjustments are required
Disadvantage: Space is required store data as well as pointers
to the next and previous nodes Insertions and deletions complex operations
since they require re-adjustment of pointers
(B) What is Garbage? How it is created in memory and what is its disadvantage?
Garbage is an area/container in memory that no more have a reference. The memory exists but no longer in use. Garbage is created in memory when a pointer variable pointing to the allocated memory area is somehow lost without de-allocating the memory area. For example,
Student * s1 = new Student();
Student * s2 = new Student();
s1 = s2;
Consequently, the memory is wasted being marked as allocated but unusable. If a program continues creating garbage in the memory, the system memory will eventually exhaust terminating the program abnormally.
Avoidance: Delete the memory previously pointed to by the pointer s1 for de-allocating memory space using :
Delete s1;
[2+2=4]
Memory area before s1 = s2;
Student object
Student object
*s1
*s2
Memory area after s1 = s2;
Student object
Student object
*s1
*s2
Garbage object
Q 2. (A) Implement an array based LIFO Stack data structure (which you may call UndoStack) such that
once the stack gets full, the new items are inserted overwriting the previously inserted items at the
bottom of the stack.
[Note that you need to implement the complete stack class with an updated push operation]. class UndoStack
{
private:
int MAXSIZE;
char* arr;
int size, top;
public:
UndoStack (int size) {
MAXSIZE = size;
arr = new char [MAXSIZE];
size = 0;
top = -1;
}
void Push (char ch) {
top = top + 1;
if (top == MAXSIZE)
top = 0;
arr[top] = ch;
if (size != MAXSIZE)
size++;
}
bool Pop (char& chr) {
if (size==0)
return false;
else {
chr = arr[top];
if (top==0)
top = MAXSIZE - 1;
else
top = top – 1;
size = size – 1;
return true;
}
}
};
(B) Consider the following pseudo code:
declare a stack of characters
while ( there are more characters in the word to read )
{
read a character
push the character on the stack
}
while ( the stack is not empty )
{
write the stack's top character to the screen
pop a character off the stack
}
What is written on the screen for the input "carpets"?
Output: steprac
[4+2=6]
Q 3. Convert the following infix expressions into postfix notation using the stack based algorithm:
i. ( A – B ) * ( D / E )
Symbol Stack Postfix Expression
( (
A ( A
- ( - A
B ( - A B
) A B -
* * A B -
( * ( A B -
D * ( A B - D
/ *( / A B – D
E *( / A B – D E
) * A B – D E /
EOS A B – D E / *
ii. A + ( B * C - ( D / E ↑ F ) * G ) * H )
Symbol Scanned STACK Postfix Expression
A A
+ + A
( +( A
B +( AB
* +(* AB
C +(* ABC
- +(- ABC*
( +(-( ABC*D
D +(-( ABC*D
/ +(-(/ ABC*DE
E +(-(/ ABC*DE
^ +(-(/^ ABC*DE
F +(-(/^ ABC*DEF
) +(- ABC*DEF^/
* +(-* ABC*DEF^/
G +(-* ABC*DEF^/G
) + ABC*DEF^/G*
* +* ABC*DEF^/G*-
H +* ABC*DEF^/G*-H
EOS ABC*DEF^/G*-H*+
[2+2=4]
Q 4. Evaluate the following postfix expression using stack:
20 2 * 9 + 14 7 / - 5 3 * +
Answer: 62 (Students need to show the detailed process)
[2]
Q 5. Given the following circular doubly linked list (without a dummy header node), with the head pointer
pointing to the first node in the list. After each of the following assignments, indicate changes made in
the list by re-drawing the whole linked list (clearly showing the links which have been re-adjusted).
(Assume that the next pointer is declared public in the Node class and we can access/modify it from
outside the class directly without using an accessor/mutator functions).
Each assignment statement should make changes in the list modified by the previous assignment.
i. head->next->next->next = head->prev;
ii. head->prev->prev->prev = head->next->next->next->prev;
iii. head->next->next->next->prev = head->prev->prev->prev;
iv. head->next = head->next->next;
v. head->next->prev->next = head->next->next->next;
[6]
Q 6. Consider the following queue of characters, where QUEUE is a circular array which is allocated 6
memory cells:
Front = 2, Rear = 4
1 2 3 4 5 6
Current Status of Queue:
A C D
Front
Rear
Note that the array is indexed starting from 1.
Describe the queue as the following operations take place (by redrawing the queue picture as above
and indicating the value of Front and Rear):
i. ‘F’ is added to the queue
Front = 2, Rear = 5, QUEUE: ___, A, C, D,F, ___
ii. Two items are removed
Front = 4, Rear = 5, QUEUE: ___, ___, ___, D,F, ___
iii. ‘K’, ‘L’ and ‘M’ are added
Front = 4, Rear = 2, QUEUE: L, M, ___, D,F, K
iv. Two letters are deleted
Front = 6, Rear = 2, QUEUE: L, M, ___, ___,___, K
v. ‘R’ is added to the queue
Front = 6, Rear = 3, QUEUE: L, M, R, ___,___, K
[5]
Q 7. Suppose the following list of letters is inserted in the given order into an empty binary search tree:
J, R, D, G, T, E, M, H, P, A, F, Q
i. Find the final tree [1]
The depth of a node is its distance from the root The depth of a binary tree is the depth of its deepest node
ii. What is the depth of the tree? 4 [0.5]
iii. What is the depth of node ‘J’? 0 [0.5]
iv. What is the depth of node ‘H’? 3 [0.5]
[8]
v. Delete node ‘M’ [0.5]
vi. Delete node ‘D’ [0.5]
Find the in-order, pre-order and post-order traversal [1.5]
INORDER: A E F G H J P Q R T
PREORDER: J E A G F H R P Q T
POSTORDER: A F H G E Q P T R J
vii. Is it a complete tree? NO [0.5]
viii. Is it a full tree? NO [0.5]