DS Assignmnet
DS Assignmnet
DS Assignmnet
Q1.Calculate the time complexity of the following with each step as shown in class. (10 Marks)
Q3. Write a function to swap nodes in a Doubly linked list without swapping data. (You have to
make insertion function and print function extra to complete this question) (15 Marks)
int main()
{
// Array to be converted
int arr[] = {1,2,3,4,5};
int s = sizeof(arr) / sizeof(arr[0]);
createLL(arr, n);
return 0;
}
Q5.Many computing applications produce matrices of numbers – integers – in which most of
the numbers are 0. Such matrices are called Sparse Matrices.
In an NxN matrix, the total number of entries is N2. So it requires O(N2) space. However, in
a Sparse Matrix most of these entries are 0. It is possible that only O(N) entries are non-zero.
So, if we stored only the non-zero entries, we could store the entire matrix in only O(N)
space. Such a matrix can be stored in only O(N) space by using a scheme which only stores
the non-zero elements of each row in a separate linked list (one linked list per row). The
following diagram shows the scheme: (100 Marks)
The structure of each node is shown above. It contains the value of the matrix entry, its column
number and a pointer to the next node in the row. Note that the size of each linked list may be
different, depending upon how many non-zero entries exist in that row.
Also pay attention to the Row List which contains the head pointers of all the lists. In our
implementation, we will use the class vector to maintain these head pointers. The following code
shows the basic definition of the class SparseMatrix and the struct Node. Integers M and N
contain the number of rows and columns of the matrix respectively. (You will use File handling
In this question.)
Note: you absolutely must not store the entire M x N matrix in a 2D array at any time in your
program.
You are required to add the following public methods to this class:
1. read(string filename):
This method should be able to read an M x N matrix from a text file. Each line in the file
contains all the entries in that row (including the zeros) separated by spaces. There is one
row per line. Following is how the matrix shown in the previous picture will be stored in
the file:
00304
00570
00000
02600
This file contains 4 lines. The characters in each line are separated by a space. Your
function should be able to load this matrix as an object of SparseMatrix, as described
above.
2. SparseMatrix(string filename)
Constructor. Simply uses read to read the matrix from the specified file.
5. ~SparseMatrix()
Adds two Sparse Matrices of same dimensions and returns the result in a third Sparse
Matrix
Multiplies two Sparse Matrices with compatible dimensions for multiplication; say, M1
X N1 and N1 X L1, and returns their M1xL1 resultant product Sparse Matrix.
Returns true if the matrix obj is completely contained in the current matrix.
Returns false otherwise.
For example, the matrix [ ] is a sub-matrix of [ ] .
Again, note that your matrices are stored in the space efficient manner described
above and must never be reconstructed into MxN arrays at any point in your
program.
Plagiarism will be checked strictly and if caught, all the assignments of that
student will be marked 0.