You are on page 1of 3

COMSATS University Islamabad, Wah Campus

Department of Computer Science


Data Structures and Algorithms
1st Sessional Fall 2020
Class: BS(SE)-4 Section: A and B
Total Marks: 30 Instructor: Dr. Mussarat Abdullah
Time Allowed: 2 hours
NOTE: Attempt all the questions:
Q1: CLO1, PLO(a)-1
Q2. CLO2, PLO(b)-1
Q1- a) Each code segment has two errors. Identify and correct them. (6, 2 each)
i) Following code deletes the last node and makes it the start node.
void dellast node()
{
node tmp, q, p;
q=start;
while(q.link.link!=NULL) q=q.link;
q=tmp.link;
start.link=tmp;
q.link=NULL;
start=tmp;
}
ii) Following code makes the linked list of digits of five digit number.
void fivedigit()
{
node q, tmp;
int num, n, i, div;
div=10000;
for(i=0; i<5; i++)
{
num =n/div;
n=n%div;
tmp=new node;
tmp.info=num;
tmp.link=tmp;
if(start==NULL) start=tmp;
else
{
q=start;
while(q.link!=NULL) q=q.link;
tmp.link=q;
}
div=div/10; }}
iii) Following code creates a double circular linked list.
void create(int data)
{
node q,tmp;
tmp=new node;
tmp.info=data;
if(start==NULL)
{
tmp.next=tmp;
start=tmp;
}
else
{
q=start;
while(q.next!=start) q=q.next;
q.next=tmp;
q.prev=tmp;
tmp.next=start;
start.next=tmp;
}
}

b) Write code segments for following.


(Only write the required methods, not the whole program) (6, 3 each)
i) Method to swap the first and last nodes of double linked list.
ii) Method to create two circular linked lists of even and odd numbers.

c) For each of the following algorithms, find the complexity of the algorithm using big O
notation. You must justify your answer with 1-2 lines of explanation. Choose from O(1),
O(n log n), O(n), O(n2 ), O(log n), O(n!). (3, 1 each)
i) Determining all duplicates in a singly LL.
ii) Determines all possible paths between n cities.
iii) Execution of following loops:
for (int i = 0; i < n; i +=2)
for (int j = 1; j < n; j++)
for (int j = 1 ; j < n ; j *= 2)

Q2. a) Write method mergesortedstacks(Stack A, Stack B) in Java that will take two sorted
stacks A and B (min on top) and create one stack that is sorted (min on top). You are allowed
to use only the stack operations such as pop, push, size and top. (4)

b) Draw stack for finding the 4th Fibonacci term with recursive code. (3)
c) Consider the following recursive method. (4, 2 each)
public int foo(int a, int b)
{
if (a%b == 0) return b;
else return foo(b, a%b); }

i) What is the output given by foo(17, 3)?


ii) What is the output given by foo(3, 9) ?

d) Following is an infix expression. (4)


((A ^ B) ^ C ^ M * W / X ) ^ Y ^ Z
i) Convert it into postfix and prefix using stack and verify through binary tree.
ii) Evaluate infix, postfix and prefix with the following values.
A = 2, B = 2, C = 3, M = 1, W = 4, X = 8, Y = 1, Z = 3

You might also like