0% found this document useful (0 votes)
17 views

DSA MST Solution

Tiet DSA course MST solution

Uploaded by

hprasadbe21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DSA MST Solution

Tiet DSA course MST solution

Uploaded by

hprasadbe21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1

Ques on: Create a node for doubly linked list and circular linked list. Show the inser on of an
element at the end of the both lists, either by code or by algorithm. Also men on the advantages
and applica ons of both types of linked lists?

Solu on: Node for doubly linked list:

struct node {
int data;
struct node *next;
struct node *prev;
};

Node for circular linked list:

struct node {
int data;
struct node *next;
};

Inser on of element at the end of the doubly linked list:

• Traverse the doubly linked list with temporary pointer temp, un ll temp->next ≠ NULL.

• New node right (next) pointer points to NULL and le (prev) pointer points to the temp
variable which is the end of the list.

• Update right (next) pointer of temp (earlier last) node to point to new node.

Algo:

insertatendDLL(struct node* temp, struct node* head, struct node


new_node)
temp = *head;
while(temp->next ≠ NULL)
temp = temp->next;
temp->next = new_node;
new_data->prev = temp;
new_data->next = NULL;

Inser on of element at the end of the circular linked list:

• Traverse the doubly linked list with temporary pointer temp, un ll temp->next ≠ Head.

• New node right (next) pointer points to Head (or temp->next) and le (prev) pointer points
to the temp variable which is the end of the list.

• Update right (next) pointer of temp node to point to new node.

Algo:

insertatendCLL(struct node* temp, struct node* head, struct node


new_node)
ti
ti
ti
ti
ti
ft
ti
ti
ft
ti
ti
temp = *head;
while(temp->next ≠ head)
temp = temp->next;
temp->next = new_node;
new_data->prev = temp;
new_data->next = head;

Advantages and applica ons of the doubly linked list:

• The advantage of a doubly linked list is that it can be traversed in both direc ons (forward
and backward). Therefore, a node can be deleted or inserted in both direc ons and lost
pointers can be easily recovered.

• Undo/redo opera ons and data structures like tree, hash, etc. can be implemented through
doubly linked list. Other applica ons may include webpage naviga on.

Advantages and applica ons of the circular linked list:

• Allows any node to be a head node. Therefore, opera ons like shi ing or rota on are easy.

• Applicability of circular linked list is in situa ons where several processes are using the same
computer resource (CPU) for the same amount of me, we have to assure that no process
accesses the resource before all other processes do (round robin algorithm).
ti
ti
ti
ti
ti
ti
ti
ft
ti
ti
ti
ti
Q.4 (i) Consider a 2-D array A[-5…..20] [20….70]. The base address of
array in memory is 1020. Size of element is 8 bytes. Find the location of
A[0][30] in both column major order & row major order. [4
Marks]

m = no. of rows = 20 – (-5) +1 = 26,


n = no. of columns = 70 - 20 +1 = 51
Location in column major order-
Address(a[i][j]) = BA + {(j-LBj)*m)+(i-LBi)}*Size
A[0][30] = 1020 + {(30-20) * 26 + (0-(-5))} * 8
= 1020 + 2120
= 3140 bytes
Location in row major order-
Address(a[i][j]) = B. A. + {(i- LBi)* n + (j-LBj)} * size
A[0][30] = 1020 + {(0-(-5)) * 51 + (30- 20))} * 8
= 1020 + 2120
= 3140 bytes

(ii) What is the output printed by the following program? [5


Marks]
#include<stdio.h>
int f(int n, int k)
{
if(n==0) return 0;
else if(n%2) return f(n/2, 2*k)+k;
else return f(n/2, 2*k)-k;
}
int main()
{
printf("%d",f(20,1));
return 0;
}
(iii) Consider a standard Circular Queue 'q' implementation (which has
the same condition for Queue Full and Queue Empty) whose size is 11
and the elements of the queue are q[0], q[1], q[2].....,q[10]. The front and
rear pointers are initialized to point at q[2]. Identify the position, at
which the ninth element will be added?
[1 Marks]

Answer: At q0.

You might also like