Uploaded by SURAJ

DSA Quiz1 soln

advertisement
Name:
Roll No:
CS201- Data Structures and Algorithms QUIZ#1
Total Marks: 15
Duration: 55 minutes
Q1) Consider the following infix expressions and write their corresponding postfix expression?
[2 Mark]
(a) 2 * (3 + (4 + 5) * (6 + 7) / 8) * 9
(b) 2 + 3 * 4 / (5 – 6) ^ 7 ^ 8
(c) 2 / 3 * 4 – 5 + 6 / 7 / (8 + 9)
(d) sin ( max ( 2, 3) / 3 * π)
[Note#1: Consider both sin and max functions as a same precedence and highest precedence
when compared with others]
[Note#2: Operator precedence order (from highest to the least): ^, * ≤ /, + ≤ - ]
Answer:
(a)
(b)
(c)
(d)
2345+67+*8/+*9*
234*56–7^8^/+
23/4*5–67/89+/+
2 3 , max 3 / π * sin
Q2) Let S be a Stack, and Q is a Queue. Assume that S and Q are initially empty. What are the final
two printf values printed by the following code after successfully executing the given code?
[Note#1: Dequeue () – returns an element from the Q by following the Queue operation rules]
[Note#2: Enqueue (Q, num) – insert a number (num) into the queue Q
[Note#3: Push(S, num) – insert a number into the stack S
[Note#4: PoP () – returns an element from the S by following the Stack operation rules]
[Note#5: Queue (Q) and Stack (S) sizes are not fixed]
Enqueue (Q, 2); Enqueue (Q, 3); Push (S, 5); Push (S, 7);
for (i=0;i<4; i++) {
printf (“%d”, Dequeue (i));
printf (“%d”, PoP(S));
Enqueue (Q, i+2);
[1 Mark]
Push (S, i*5);
}
(i)
What is the final iteration first printf value after the above code execution
________________
(ii)
What is the final iteration second printf value after the above code execution
_______________
(i)
What is the final iteration first printf value after the above code execution
_______3_________
(ii)
What is the final iteration second printf value after the above code execution
_______10________
Answer:
Q3) struct node {
int data;
struct node *next;
};
[Note#1: Please consider that the following elements are already inserted in the Linked List.]
[1 Mark]
struct node *p, *q;
i) p = head;
ii) q = head;
iii) while (p → next!=NULL){
p = p → next;
}
iv) q → next → next → next = head → next → next ;
v) q → next → next = p → next;
Draw the final complete linked list after performing the operations from (i) – (v) (follow the
given order)?
[Note#2: The pointers pointing to each node should be visible and clear in drawing the final
linked list]
Answer:
Final representation
Q4) Which data structure would be most appropriate to implement a collection of values with the
following 3 characteristics
[1 Mark]
(i) Items are retrieved and removed from the collection in FIFO order.
(ii) There is no a priori limit on the number of items in the collection.
(iii) The size of an item is large relative to the storage required for a memory address
a. Singly link list with head and tail pointer
b. Doubly link list with only head pointer
c. Array
d. None
Answer:
a. Singly link list with head and tail pointer
Q5) Consider the following function CS2x1 which takes a head pointer as a parameter in the given
Singly Linked List.
struct node {
int data;
struct node *next;
};
[2 Mark]
void CS2x1 (struct node *head) {
if(head == NULL)
return;
printf(“%d”, head → data);
if(head → next != NULL)
CS2x1(head → next → next)
printf(“%d”, head → data)
}
Write the order output sequence after successfully executing the above function call for the given
linked list?
Answer:
23 15 89 65
65 89 15 23
Q6) Given a circular queue with size 10. What are the final values in the Circular Queue, after the
following set of operations and code are execute: (assume that the circular queue array start at
0)
[Note: If you DeQueue an element, please consider that you replaced it with -1 in case you don’t
perform any EnQueue operation at that index].
Initially, please consider that the following seven EnQueue operations are performed on the Circular
Queue (read from left to right):
EnQueue(13) , EnQueue(15), EnQueue(31), EnQueue(37), EnQueue(47), EnQueue(49), EnQueue(13)
Please consider that the following code is executed after the seven EnQueue operations:
for (int k = 1; k <=7; k++) {
int delete; //to store the dequeued/deleted element
delete=DeQueue();
EnQueue(delete);
}
What are the final values in the Circular Queue?
[1 Mark]
Answer:
Index
0
1
2
3
4
5
6
7
8
9
Value
37
47
49
13
-1
-1
-1
13
15
31
Q7) Assume that the Queue operations are implemented by using Stack operations:
Enqueue (x) and Dequeue ( ) are Queue operations, whereas Push(x) and Pop ( ) are
stack operations:
Consider the following code, where S1 and S2 are two stacks:
Enqueue (S1, x) {
Push (S1, x);
}
Dequeue (S1, S2) { if
(!IsEmptyStack (S2) )
return Pop (S2); else{
while (!IsEmptyStack (S1) )
B1;
return B2;
}//end-of-the-else-statement;
}//end-of-Dequeue-function;
Fill in the missing statements B1 and B2 to perform the Dequeue operation by using the Stack
Operations only
B1: _____________________________
B2:______________________________
[1 Mark]
Answer:
B1: _____Push(S2, Pop(S1))_________________
B2:______Pop(S2)________________________
Q8) Consider the queues Q1 containing four elements and Q2 containing none (shown as the
Initial State in the figure). The only operations allowed on these two queues are Enqueue (Q,
element) and Dequeue (Q). The minimum number of Enqueue operations on Q1 required to place
the elements of Q1 into Q2 in reverse order (shown as the Final State in the figure) without using
any additional storage is _____________________________________________________
[1 Mark]
[Note: The Head represented in the following figure corresponds to front pointer of Queue].
Answer:
If Queue has just 2 elements we can reverse it using simple Enqueue() and Dequeue() operations.
Step 1:
Q1: Dequeue();Dequeue()
Q2: Enqueue(Q2, 1); Enqueue(Q2, 2)
Step 2:
Q2: Dequeue(); Enqueue (Q2, 1)
Step 3:
Q1: Dequeue(); Q2: Enqueue (Q2, 3)
Step 4:
Q2: Dequeue(); Enqueue(Q2, 2); Dequeue(); Enqueue(Q2, 1)
Step 5:
Q1: Dequeue(); Q2: Enqueue(Q2, 4)
Step 6:
Q2: Dequeue(); Enqueue(Q2, 3); Dequeue(); Enqueue(Q2, 2); Dequeue(); Enqueue(Q2, 1)
Minimum number of Enqueue() operations required on Q1 is 0
Q9) State whether the following statements are True/False about Circular Linked List Data
Structures?
[
1 Mark]
a) Random access is not allowed in a typical implementation of Linked Lists ___________
b) Non-Linear in accessing, and Linear in storing the data in the memory ___________
c) The node next pointer never points to the NULL in the Circular Linked List! ___________
d) Deletion of an element should be done at the first node in the Circular Linked List! ___________
Answer:
a)
b)
c)
d)
Random access is not allowed in a typical implementation of Linked Lists __True__
Non-Linear in accessing, and Linear in storing the data in the memory __False___
The node next pointer never points to the NULL in the Circular Linked List! __True___
Deletion of an element should be done at the first node in the Circular Linked List! __False___
Q10)
struct node{
int data;
struct node *next;
};
[1 Mark]
[Note: Please consider that the following elements are already inserted in the Linked List.]
struct node *p, *q; p=head;
(i) q=p → next → next ;
(ii) p = p→ next;
(iii) printf ("%d", q → next → next → data);
(iv) q → next → next → next = p → next;
(v) printf (“%d”, q → data);
(vi) printf (“%d”, p → next → next → next → data);
(vii)printf (“%d”, q → next → next → next → data);
What is the output of the above code [write the first printf result in first line and so on]
_____, ______, ______, _______
Answer:
50, 30, 50, 30
Q11) For each of the following scenarios choose the “best” data structure from the following list
or a combination of data structures: an unsorted array, single linked list, double linked list, circular
linked list, stack, queue.
[1 Mark]
a. Suppose that a grocery store decided that customers who come first will be served first
b. A list must be maintained so that any element can be accessed randomly
c. A program needs to remember operations it performed in opposite order
d. A multiplayer game switches between the players in a loop (e.g., Ludo)
Answer:
a. Suppose that a grocery store decided that customers who come first will be served first 
Queue
b. A list must be maintained so that any element can be accessed randomly  Array
c. A program needs to remember operations it performed in opposite order  Stack
d. A multiplayer game switches between the players in a loop (e.g., Ludo)  Circular linked list
Q12) Fill the following table with the number of pointer operations that need to be changed for
each circular doubly linked list (CDLL) and write down a list of pointer operations to be performed
as discussed in the class.
[Note#1: For example, to delete the last element from the CDLL:
prev→ prev → next = head;
head → prev = head → prev → prev;
The number of pointer operations to be changed → 2]
Operations
Begin
[2 Marks]
Middle (given position)
Insert
Delete
Answer:
Insert at the beginning:
(1)
(2)
(3)
(4)
(5)
begin  next = newnode;
newnode  prev = begin;
head  prev = newnode;
newnode  next = head;
head = newnode
Insert at the given position:
(1)
(2)
(3)
(4)
newnode  prev = position;
newnode  next = position  next;
position  next  prev = newnode;
position  next = newnode;
Delete at the begin:
(1)
(2)
(3)
(4)
head  next  prev = head  prev;
head  prev  next = head  next;
head = head  next;
free (delbegin)
Delete at the given position:
(1) position  prev  next = position  next;
(2) position  next  prev = position  prev;
(3) free (deltail)
Operations
Begin
Middle (given position)
Insert
5
4
Delete
4
3
Download