You are on page 1of 5

Student ID #: ____________________

Sukkur IBA University


Mid Term I Examinations
Spring 2020

Course: Data Structures and Applications Class: BS (CS/SE) – III


Date: 14th Feb, 2020 Time: 2 hours
Instructor: Saif Hassan Marks: 20 Marks

General Instructions:
Attempt all questions on Question Paper.

Question 01 [1+2+3 marks] Linked Lists [CLO 2, GA3]


Assume that LL is a DOUBLY linked list with the head node and at least one other internal node
M which is not the last node. Write few lines of code to accomplish the following. You may
assume that each node has a next pointer and prev pointer. You may NOT swap data to
accomplish any of the following operations. For each operation, assume the original list as
described above. You are encouraged to draw pictures to justify your code. Note that for each
operation, you need to manipulate at least two pointers, next and prev. All tasks should be done
in O (1)
a. Delete the head node.

b. Insert a node P immediately after M.


Student ID #: ____________________

c. Swap head and the node M (you may not swap data).

Question 02 [1+1+1 Marks] Recursion [CLO 2, GA1]


Consider the following recursive method.
public int foo(int a, int b)
{
if (a%b == 0)
return b;
else
return foo(b, a%b);
}

Task Answer

a) Output given by foo (17, 3)?

b) Output given by foo (3, 9)?

c) Purpose of the foo function. (example: it is


finding the max of x and y)
Student ID #: ____________________

Question 03 [5 marks] Analyzing [CLO 3, GA3]

a) Evaluate the big-O complexity of following code fragment. (1.5 marks)

int count = 0;
for (int i = 0; i < N; i++){
for (int j = i+1; j <= N; j=j*2){
for (int k = N-1; k < 1; k=k/2){
count++;
}
}
}

b) Application regarding shopkeeper and customer. After every 10 seconds a new customer will
come & add himself in a stack. The shopkeeper serves the customer once every 15 seconds
using the LIFO fashion. Identify issues and give solution. (1 marks)

c) When I used some code on an array of 2500 integers on my slow old computer at home, it
took 5.0 seconds (I tried it several times--5.0 seconds every time). Approximately how long
will this code take to run if N is 10000 (assuming I use the same computer)? Justify your
answer. Big-O for the code is O (1). (1 marks)
Student ID #: ____________________

d) List the following functions by increasing asymptotic growth rate. If two functions have
the same asymptotic growth rate, state that fact. No justification is needed. (1.5 marks)

lg 𝑛 1.1𝑛 𝑛 lg 𝑛 𝑛( lg 𝑛)2 3 lg 𝑛 25 𝑛34

Question 04 [6 marks] [CLO 2, GA2]


Consider the following interface to stacks, as introduced in class.
class Stack;
stack(); /* O(1); create new, empty stack */
bool empty(stack S); /* O(1); check if stack is empty */
void push(int x); /* O(1); push element onto stack */
int pop(); /* O(1); pop element from stack */

In these problem you do not need to write annotations, but you are free to do so if you wish.
You may assume that all function arguments of type stack are non-NULL.
a) Write a function rev (Stack S, Stack D). We require that D is originally empty.
When rev returns, D should contain the elements of S in reverse order, and S should be
empty. (3 Marks)
Student ID #: ____________________

b) An alternative to terminating lists with NULL is to terminate them with a self-loop. We call
such a list a sloop. For example, the following is a sloop of length 3.

Write a function is_self_loop (LinkedList list) to test if list is a self-loop, that is, a
linked list terminated by a self-loop. You should assume that there are no other cycles in the
list. (3 Marks)

ALL THE BEST

You might also like