Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
AUTUMN MID SEMESTER EXAMINATION-2023
School of Computer Engineering
Kalinga Institute of Industrial Technology, Deemed to be University
Data Structures
[CS 21001]
Time: 1 1/2 Hours Full Mark: 40
Answer Any four Questions including Question No. 1 which is compulsory.
The figures in the margin indicate full marks. Candidates are required to give their answers in their own
words as far as practicable and all parts of a question should be answered at one place only.
1. Answer all the questions. [2×5]
a) Consider a two-dimensional array with dimensions m × n, where m represents the number of
rows and n represents the number of columns. What is the time complexity to access an
arbitrary element in this array?
A) O(1) B) O(log n) C) O(m) D) O(m + n) E) None of these
b) Given an array arr[1...10][1...15] with base address 100 and the size of each element of the
array 2 bytes in memory. Find the address of arr[8][6] in column-major order.
Ans:
BA + W[R(J – Lc) + (I – Lr)]
Number of row given in the matrix M = Upper Bound – Lower Bound + 1 = 10-1+1=10
Address of A[8][6] = 100 + 2 * ((6 – 1) * 10 + (8 – 1))
= 100 + 2 * ((5) * 10 + (7)) = 100 + 2 * (57)= 214
c) What is the output of the following function in which head is pointing to the first node of the
given linked list: 10->20->30->40->50?
void disp(struct node* head) {
if(head == NULL)
return;
printf("%d ", head->info);
if(head->next != NULL )
disp(head->next->next);
printf("%d ", head->info);
}
Ans:
10->30->50->50->30->10
d) What does the following code segment do on a doubly linked list. mid is the address of some
middle node and new is the address of a newly created node.
new->prev = mid;
new->next = mid->next;
new->prev->next = new;
new->next->prev = new;
Where does the new node insert to?
A) Right to mid B) Left to mid C) Right Most D) Left Most E) None of these
e) A stack of integers initially contain three elements 5, 7, 3 from bottom to top. The size of the
Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
stack is 6. Mention the output of the operations push (4), pop (), push (10), push (8), push (0),
push (9), pop (), pop () on the stack.
Ans:
Operation Status of Stack Output
573
Push (4) 5734
Pop() 573 4
Push(10) 5 7 3 10
Push (8) 5 7 3 10 8
Push (0) 5 7 3 10 8 0
Push(9) 5 7 3 10 8 0 No Insert (Full)
Pop () 5 7 3 10 8 0
Pop () 5 7 3 10 8
2. [5×2]
a) What is the approximate return value (in terms of n) of the following function? Explain each
loop to compute the value of q.
int func(int n) {
int i, j, k, p, q=0;
for(i=1; i<n; i++) {
p=0;
for(j=n; j>1; j=j/2)
p++;
for(k=1; k<p;; k=k*2)
q++;
}
return q;
}
Ans:
The loop: for(j=n; j > 1; j=j/2) runs Θ(log n) times. So, p = Θ(log n).
The loop: for(k=1; k < p; k=k*2) runs Θ(Log p) times, which log(log n).
So, q = Θ(log(log n)).
The first loop: for(i = 1; i < n; ++i) runs O(n) times.
So, T(n) = n log(log n)
b) Write a C function of O(n) order for removing all odd numbers from a given array. Example,
if the array contains 10, 2, 3, 7, 8, 6, and 11. The output should be 10, 2, 8, and 6.
Ans:
void removeOdds(int arr[], int n) {
int i, k=0;
for(i=0, i< n; i++) {
if(arr[i] %2 == 0) {
arr[k]=arr[i];
k++;
}
}
for(i=0, i< k; i++)
printf(“%d ”, arr[i]);
Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
}
3. [5×2]
a) Write an algorithm/ C function to reverse a singly linked list, where the pointer variable head
is holding the address of the first node of the linked list and this head will be passed as an
argument to the function.
Ans:
void reverse(struct Node** head) {
struct Node* prev = NULL;
struct Node* curr = *head;
struct Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
b) Write the an algorithm/ C function to insert a node after a specific position in a doubly linked
list, where previous pointer is named as prev and the next pointer is named as next. head is a
pointer variable holding the address of the first node of the list, which passed as the argument
to the function.
Ans:
void insertAtPosition(struct Node* head, int pos, int val) {
struct Node* newNode, *curr;
curr = head;
if(head == NULL) {
printf("\nEmpty list...");
return;
}
if(pos == 1) {
insertAtBeginning();
return;
}
while(i< pos-1 && curr!=NULL) {
curr = curr->next;
i++;
}
if(curr->next == NULL) {
insertAtEnd();
return;
}
if(curr != NULL) {
newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = curr->next;
newNode->prev = curr;
Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
if(curr->next != NULL) {
curr->next->prev = newNode;
curr->next = newNode;
}
}
else
printf("Invalid position...\n");
}
4. [5×2]
a) Convert the infix expression: M^(N*P)/Q+R-S^T/((U/V)/W)-X to postfix expression using
stack. Explain each step showing the status of the stack as well as the partial postfix
expression.
Ans:
Sl Symbol Stack Postfix Expression
1 M M
2 ^ ^ M
3 ( ^( M
4 N ^( MN
5 * ^(* MN
6 P ^(* MNP
7 ) ^ MNP*
8 / / MNP*^
9 Q / MNP*^Q
10 + + MNP*^Q/
11 R + MNP*^Q/R
12 - - MNP*^Q/R+
13 S - MNP*^Q/R+S
14 ^ -^ MNP*^Q/R+S
15 T -^ MNP*^Q/R+ST
16 / -/ MNP*^Q/R+ST^
17 ( -/( MNP*^Q/R+ST^
18 ( -/(( MNP*^Q/R+ST^
19 U -/(( MNP*^Q/R+ST^U
20 / -/((/ MNP*^Q/R+ST^U
21 V -/((/ MNP*^Q/R+ST^UV
22 ) -/( MNP*^Q/R+ST^UV/
23 / -/(/ MNP*^Q/R+ST^UV/
24 W -/(/ MNP*^Q/R+ST^UV/W
25 ) -/ MNP*^Q/R+ST^UV/W/
26 - - MNP*^Q/R+ST^UV/W//-
27 X - MNP*^Q/R+ST^UV/W//-X-
b) Write a recursive function to display elements of stack using the operations push() and pop()
without violating the LIFO concept.
Ans:
void display() {
Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
int x;
if(top == -1)
return;
else {
x=pop()
display();
printf("%d", x);
push(x);
}
}
5. [5×2]
a) Given a stack of integers, write the pseudo code/ C function to display all elements in the
order in which they have been inserted using another temporary stack without violating LIFO
concept.
Ans:
void displayStack(Stack s1) {
int x;
if (empty(s1))
return;
while(!empty(s1)) {
x = pop(s1);
push(s2, x);
}
while(!empty(s2)) {
x = pop(s2);
printf("%d ", x);
}
}
b) Write functions (preferably in C) to implement two queues using a single static array, where
queue 1 is to be implemented considering the beginning of the array and queue 2 is to be
implemented considering the end of the array. Both queues are growing towards each other.
The solution has to capture enqueue() and dequeue() functions for each queue.
Ans:
void enqueue1(int item) {
if(rear1 = = rear2-1 || rear2 = = 0) {
printf(“Queue1 Overflow”);
return;
}
if(rear1 = = -1)
rear1 = front1 = 0;
else
rear1++;
queue[rear1] = item;
}
void enqueue2(int item) {
if(rear1 = = rear2-1 || rear1 = = n-1) {
Semester: 3rd
Semester: 4th
SubjectSub
Name: Data
& Code: OSStructures & Code: CS 21001
& CS - 2009
BranchBranch
(s): SCE
(s): CSE, IT, CSSE, CSCE
printf(“Queue2 Overflow”);
return;
}
if(rear2 = = n)
rear2 = front2 = n-1;
else
rear2--;
queue[rear2] = item;
}
int dequeue1() {
int item;
if(front1 == -1) {
printf(“Queue1 Underflow”);
return INT_MIN;
}
item = queue[front1];
if(front1 = = rear1)
rear1 = front1 = -1;
else
front1++;
return item;
}
int dequeue2() {
int item;
if(front2 = = n) {
printf(“Queue2 Underflow”);
return INT_MIN;
}
item = queue[front2];
if(front2 = = rear2)
rear2 = front2 = n;
else
front2 --;
return item;
}
*** Best of Luck ***