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

Dsa Important

The document discusses different types of data structures including linear data structures like arrays and linked lists, non-linear data structures like trees and graphs, and provides examples of transpose of a matrix and sparse matrix. It also compares linear and non-linear data structures and lists some common data structures and algorithms.

Uploaded by

xenaj90745
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Dsa Important

The document discusses different types of data structures including linear data structures like arrays and linked lists, non-linear data structures like trees and graphs, and provides examples of transpose of a matrix and sparse matrix. It also compares linear and non-linear data structures and lists some common data structures and algorithms.

Uploaded by

xenaj90745
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Transpose of matrix
a. #include <iostream>
using namespace std;

int main(){
int row,column;
cout<<"Enter row count : ";
cin>>row;
cout<<"Enter column count : ";
cin>>column;
int arr[row][column];
int transpose[row][column];
cout<<"Enter element for array\n\n";
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout<<"Element no "<<i<<j<<": ";
cin>>arr[i][j];
}

}
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
transpose[j][i] = arr[i][j];
}
}
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout<<transpose[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
2. Sparse matrix
What is a sparse matrix? A sparse matrix is a special case of
a matrix in which the number of zero elements is much
higher than the number of non-zero elements. As a rule of
thumb, if 2/3 of the total elements in a matrix are zeros, it can
be called a sparse matrix.
#include <iostream>

using namespace std;

int main()
{
int row, column;
cout << "Enter row count : ";
cin >> row;
cout << "Enter column count : ";
cin >> column;
int arr[row][column];
cout << "Enter element for array\n\n";
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout << "Element no " << i << j << ": ";
cin >> arr[i][j];
}
}
int zero_count = 0;
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
if (arr[i][j] == 0)
{
zero_count++;
}
}
}
if (zero_count > row * column)
{
cout<<"This is sparse matrix";
}
else {
cout<<"This is not sparse matrix";
}
}
3. Data structure as linear and nor-linear

S.NO Linear Data Structure Non-linear Data Structure

In a linear data structure, data


elements are arranged in a linear
order where each and every In a non-linear data structure,
element is attached to its data elements are attached in
1. previous and next adjacent. hierarchically manner.

Whereas in non-linear data


In linear data structure, single structure, multiple levels are
2. level is involved. involved.

Its implementation is easy in While its implementation is


comparison to non-linear data complex in comparison to linear
3. structure. data structure.

In linear data structure, data While in non-linear data


elements can be traversed in a structure, data elements can’t be
4. single run only. traversed in a single run only.

While in a non-linear data


structure, memory is utilized in
In a linear data structure, memory an efficient way.
5. is not utilized in an efficient way.

Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.

Applications of linear data Applications of non-linear data


structures are mainly in structures are in Artificial
application software Intelligence and image
7. development. processing.

Non-linear data structures are


useful for representing complex
relationships and data
Linear data structures are useful hierarchies, such as in social
for simple data storage and networks, file systems, or
8. manipulation. computer networks.
S.NO Linear Data Structure Non-linear Data Structure

Performance is usually good for


simple operations like adding or Performance can vary depending
removing at the ends, but slower on the structure and the
for operations like searching or operation, but can be optimized
9. removing elements in the middle. for specific operations.

4. Five types of data structure


5 Types of Data Structures and Algorithms Computer
Scientists Must Know
Linear Data Structures. There are two types of computer
science data structures: linear and nonlinear. ...
Nonlinear Data Structures. ...
Sorting Algorithms. ...
Searching Algorithms. ...
Graph Traversal Algorithms.

You might also like