DS Soln
DS Soln
DS Soln
III
[CO/CM/IF/CW]
Data Structures Using ‘C’
Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70
Q.1(b) Explain the concept of information, Next, Null pointer and empty [2]
list with respect to link list.
Ans.: Node: Each data element in a linked list is represented as a node. Node
contains two partsone is info (data) and other is next pointer (address). Info
part stores data and next pointer stores address of next node.
Next pointer: It is a pointer that holds address of next node in the list i.e.
next pointer points to next node in the list
Null pointer: It is a pointer that does not hold any memory address i.e. it is
pointing to nothing. It is used to specify end of the list. The last element of
list contains NULL pointer to specify end of list.
Empty list: Each linked list has a header node. When header node contains
NULL value, then that list is said to be empty list.
-1-
Vidyalankar : S.Y. Diploma DS
Q.1(e) Define the terms: Linear data structure and non-linear data [2]
structure.
Ans.: Linear Data Structure: A data structure in which all data elements are
stored in a particular sequence is known as linear data structure.
-2-
Prelim Question Paper Solution
Non-Linear data structure: A data structure in which all data elements are
not stored in any particular sequence is known as nonlinear data structure.
Example: graph and tree.
Time Complexity
While measuring the time of an algorithm, we concentrate on developing only
the frequency count for all key statements (statements that are important
and are the basic instructions of an algorithm). This is because, it is often
difficult to get reliable timing figure because of clock limitations and the
multiprogramming or the sharing environment.
Consider the algorithm given below:
ALGORITHM a=a+1
A
for x = 1 to n step 1
ALGORITHM a=a+1
B Loop
for x = 1 to n step 1
ALGORITHM for y = 1 to n step 1
C a=a+1
Loop
In the second algorithm, i.e. B, the key statement out of three statements is
the assignment operation a = a + 1. Because this statement is contained
-3-
Vidyalankar : S.Y. Diploma DS
within a loop, the number of times it is executed is n, as the loop runs for n
times. The frequency count for this algorithm is n.
According to the third algorithm, the frequency count for the statement
a = a + 1 is n2 as the inner loop runs n times, each time the outer loop runs,
the outer loop also runs for n times. n2 is said to be different in increasing
order of magnitude just like 1, 10, 100 depending upon the n.
Space Complexity
The space needed by the program is the sum of following components:
-4-
Prelim Question Paper Solution
with the search element. If a match is found then the search process ends
otherwise divide the i/p list into 2 parts. First part contains all the numbers
less than mid position element and second part contains all the numbers
greater than mid position element. Then select one of the part depending on
search element is less or greater than mid position element and calculate mid
position for selected part. Again compare mid position element with search
element. The binary search performs comparison and division task the
element is found or division of list gives one element for comparison.
Example:
Consider Input list 0, 1, 2, 9, 10, 11, 15, 20, 46, 72
Search element: 11
Iteration 1
Lower = 0 Upper = 9 mid = (lower + upper)/2 = (0 + 9/2) = 4.5
Iteration 2
Lower = 5 Upper = 9 mid = (Lower + Upper)/2 = (5 + 9) /2 = 7
Iternation 3
Lower = 5 Upper = 6 mid = (Lower + Upper)/2 = (5 + 6)/2 = 5.5
-5-
Vidyalankar : S.Y. Diploma DS
Q.2(b) Explain stack overflow and underflow conditions with example. [4]
Ans.: Stack Overflow: Sometimes when a new data is to be inserted into a stack
but there is no available space, then this situation is known as Stack
Overflow.
Example: If a stack has maximum capacity of 3 elements, and these three
are already occupied and the programmer tries to push in a fourth element,
then it will lead to overflow.
Stack Underflow: Sometimes when one wants to delete data from a stack
but the stack is already empty, then this situation is known as Stack
Underflow.
Example: If a stack is empty and a programmer tries to execute the POP
operation on it, then it will cause the Underflow error.
Q.2(c) Describe the concept of linked list with the terminologies : node, [4]
next pointer, null pointer and empty list.
Ans.: Node: Each data element in a linked list is represented as a node. Node
contains two parts one is info (data) and other is next pointer (address).
Info part stores data and next pointer stores address of next node.
Next pointer: It is a pointer that holds address of next node in the list i.e.
next pointer points to next node in the list
Null pointer: It is a pointer that does not hold any memory address i.e. it is
pointing to nothing. It is used to specify end of the list. The last element of
list contains NULL pointer to specify end of list.
Empty list: Each linked list has a header node. When header node contains
NULL value, then that list is said to be empty list.
-6-
Prelim Question Paper Solution
-7-
Vidyalankar : S.Y. Diploma DS
Adjacency Matrix:
A B C D
A 0 1 0 0
B 0 0 0 1
C 1 0 0 1
D 1 0 0 0
-8-
Prelim Question Paper Solution
Q.3(d) Write ‘c’ program for deletion of an element from an array. [4]
Ans.: #include <stdio.h>
int main( )
{
int array[100], position, c, n;
print(“Enter number of elements in array\n”);
scanf(“%d”, &n);
printf(“Enter %d elements\n”, n);
for (c = 0; c< n; c++)
scanf(“%d”, &array[c]);
print(“Enter the location where you wish to delete element\n”);
scanf(“%d”, &position);
if (position > = n + 1)
printf(“Delection not possible.\n”);
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c + 1];
printf(“Resultant array;\n”);
-9-
Vidyalankar : S.Y. Diploma DS
- 10 -
Prelim Question Paper Solution
Front Rear
- 11 -
Vidyalankar : S.Y. Diploma DS
(v) last=NULL; // Set last= NULL if only one node is present in the Circular
Linked List
(vi) End of function
Q.4(e) Describe circular linked list with suitable diagram. Also state [4]
advantage of circular linked list over linear linked list.
Ans.: Circular Linked List
A circular linked list is a variation of linked list in which the last element is
linked to the first element. This forms a circular loop.
For example:
- 12 -
Prelim Question Paper Solution
list. We can maintain a pointer to the last inserted node and front can
always be obtained as next of last.
(iii) Circular lists are useful in applications to repeatedly go around the list.
For example, when multiple applications are running on a PC, it is common
for the operating system to put the running applications on a list and
then to cycle through them, giving each of them a slice of time to
execute, and then making them wait while the CPU is given to another
application. It is convenient for the operating system to use a circular
list so that when it reaches the end of the list it can cycle around to the
front of the list.
(iv) Circular Doubly Linked Lists are used for implementation of advanced
data structures like Fibonacci Heap.
Q.5(b) Write an algorithm to count numbers of nodes in singly linked list. [6]
Ans.: Step 1: Count = 0. SAVE = FIRST.
Step 2: Repeat step 3 while SAVE != NULL.
Step 3: Count= Count + 1. SAVE=SAVE->LINK.
Step 4: Return Count.
B C
- 13 -
Vidyalankar : S.Y. Diploma DS
Ans.:
A D
B C
Adjacency Matrix
A B C D A D C B
A 0 1 0 1 A 0 1 0 1
A = B 1 0 0 0 OR D 0 0 0 0
C 1 1 0 1 C 1 1 0 1
D 0 0 0 0 B 1 0 0 0
A X
B X
C X
D X X
Step 2:
- 14 -
Prelim Question Paper Solution
Step 3:
Step 4:
Result: Value was found in the linked list. The pointer points to the node
having the value.
Q.6(b) Describe breadth first search traversal in a graph with example. [6]
Ans.: Algorithm for BFS:
(i) Initialize all nodes to ready state.
(ii) Insert starting node in a queue and change its state to waiting state.
(iii) Repeat steps 4 to 6 till the queue becomes empty.
(iv) Remove front node N from queue 2 change its status to visit.
(v) Insert all adjacent nodes of N at the rear end of the queue and change
their status to ‘waiting state’.
(vi) From the origin find path from source node to destination node or from
the queue element list find all nodes that are reachable.
(vii) Stop.
Example :
Front of the queue is set to ‘0’
Rear of the queue is set to ‘0’
Queue is used to indicate elements of the A
graph which are visited.
F C B
Origin is used to keep track of origin of each node.
D E G
J K
- 15 -
Vidyalankar : S.Y. Diploma DS
4) Remove front element C and insert its adjacent nodes in the queue
(F is already visited)
B D
Queue=A, F, C, B, D front=3
Origin=O, A, A, A, F rear=4
9) Remove J
Queue = A, F, C, B, D, G, E, J
All nodes readable from A-A, F, C, B, D, G, E, J
Origin= O, A, A, A, F, B, G, E, J
- 16 -
Prelim Question Paper Solution
Q.6(c) Create a singly linked list using data fields 90, 25, 46, 39, 56. [6]
Search a node 40 from a SLL and show procedure step-by-step
with the help of diagram from start to end.
Ans.: (i) With given data fields, singly linked list is created as follows.
- 17 -