Data Structures Unit I Notes
Data Structures Unit I Notes
NOTES
UNIT I
DATA STRUCTURES
UNIT I
Data structures:
A data structure is basically a group of data elements that are put together
under one name, and which defines a particular way of storing and organizing
data in a computer so that it can be used efficiently.
An abstract data type (ADT) is a set of operations. Abstract data types are
mathematical abstractions; nowhere in an ADT's definition is there any
mention of how the set of operations is implemented. This can be viewed as
an extension of modular design.
An abstract data type (ADT) is the way we look at a data structure, focusing
on what it does and ignoring how it does its job. For example, stacks and
queues are perfect examples of an ADT. We can implement both these ADTs
using an array or a linked list. This demonstrates the ‘abstract’ nature of
stacks and queues. The word ‘abstract’ in the context of data structures
means considered apart from the detailed specifications or implementation.
LIST ADT
List is an abstract data type that represents a countable number of
ordered values, where the same value may occur more than once. Operations
performed on the list ADT are:
o Create
o Insert
o Delete
o Search
o Display
o Sort
• Each element can be accessed via its index. For example, we can fetch an element
at index 6 as 27.
Basic Operations
Algorithm
o Create 1. Start
o Insert 2. Set J = N
3. Set N = N+1
o Delete 4. Repeat steps 5 and 6 while J
o Search >=
5.
K
Set LA[J+1] = LA[J]
o Display 6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
b
b[0] b[1] b[2] b[3] b[..] b[max]
b
b[0] b[1] b[2] b[3] b[4] b[..]
void insert()
{
cout<<"\n Enter the position to insert:";
cin>>pos;
if(pos>=n)
{
cout<<"\n invalid Location::");
}
else
{
for(i=n-1;i>=pos;i--)
{
b[i+1]=b[i];
}
cout<<"\n Enter the element to insert:\n";
cin>>p;
b[pos]=p;
n++;
}
b
b[0] b[1] b[2] b[3] b[4] b[..]
b
b[0] b[1] b[2] b[3] b[4] b[..]
void deletion()
{
cout<<"\n Enter the position u want to delete:";
cin>>pos;
if(pos>=n)
{
cout<<"\n Invalid Location:";
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
cout<<"\n The Elements after deletion";
for(i=0;i<n;i++)
{
cout<<"\t"<<b[i];
}
}
b
b[0] b[1] b[2] b[3] b[..] b[max]
To search a particular data item from a list, the user enters the item to besearched.
The List ADT should provide the position of the item if the item is available or it
should display that the item is not available in the list.
For example:
Enter the element to be searched : 34
The element is available in position 3.
void search()
{
cout<<"\n Enter the Element to be searched:";
cin>>e;
for(i=0;i<n;i++)
{
if(b[i]==e)
{
cout<<"The element is in the position"<<i+1;
}
}
}
Formula-based representation
A formula-based representation uses an array to represent the instances of an object. Each position
of the array, called a cell or a node, holds one element that makes up an instance of that object.
Individual elements of an instance are located in the array, based on a mathematical formula, e.g.,
a simple and often used formula is
Location(i) = i − 1,
which says the ith element of the list is in position i − 1.
We also need two more variables, length and MaxSize, to completely characterize the list type.
Max-1
b
b[0] b[1] b[2] b[3] b[4] b[..]
Indirect addressing
This approach combines the formula-based approach and that of the linked representation. As a
result, we can not only get access to elements in Θ(1) times, but also have the storage flexibility,
elements will not be physically moved during insertion and/or deletion.
In indirect addressing, we use a table of pointers to get access to a list of elements, as shown in
the following figure.
.
Simulated pointers
Linked Lists
A linked list, in simple terms, is a linear collection of data elements. These
data elements are called nodes. Linked list is a data structure which in turn
can be used to implement other data structures.
In a linked list every node contains two parts, an integer and a pointer to the next
node. The left part of the node which contains data may include a simple data type,
an array, or a structure. The right part of the node contains address of the next node
In order to form a linked list, we need a structure called node which has two fields,
DATA and NEXT. DATA will store the information part and NEXT will store the address
of the next node in sequence.
data_type array_name[row_size][column_size];
Therefore, a two-dimensional m x n array is an array that contains m x n data
elements and each element is accessed using two subscripts, i and j, where
i<=m and j<=n.
For example, if we want to store the marks obtained by three students in five
different subjects, we can declare a two dimensional array as:
int marks[3][5];
The pictorial form of a two-dimensional array is shown
Note that the initialization of a two-dimensional array is done row by row. The
above statement can also be written as:
marks[1][2] = 79;
or
marks[1][2] = marks[1][1] + 10;
Accessing the elements of the 2D array
program to print the elements of a 2D array.
#include <iostream.h>
#include <conio.h>
int main()
{
int arr[2][2] = {12, 34, 56,32};
int i, j;
for(i=0;i<2;i++)
{
cout<<"\n";
for(j=0;j<2;j++)
{
cout<<”\t"<< arr[i][j];
}
}
getch();
return 0;
}
Output
12 34
56 32
OPERATIONS ON TWO-DIMENSIONAL ARRAYS (MATRICES)
Two-dimensional arrays can be used to implement the mathematical concept
of matrices. In mathematics, a matrix is a grid of numbers, arranged in rows
and columns. Thus, using two dimensional arrays, we can perform the
following operations on an m×n matrix:
Sum Two matrices that are compatible with each other can be added
together, storing the result in the third matrix. Two matrices are said to be
compatible when they have the same number of rows and columns. The
elements of two matrices can be added by writing:
Product Two matrices can be multiplied with each other if the number of
columns in the first matrix is equal to the number of rows in the second
matrix. Therefore, m x n matrix A can be multiplied with a p x q matrix B if
n=p. The dimension of the product matrix is m x q. The elements of two
matrices can be multiplied by writing:
#include <iostream.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3];
clrscr();
cout<<"\n Enter the elements of the matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[i][j];
}
}
cout<<"\n The elements of the matrix are \n";
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<"\t"<<mat[i][j];
}
}
getch();
return 0;
}
#include <iostream.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3], transposed_mat[3][3];
clrscr();
cout<<"\n Enter the elements of the matrix ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[i][j];
}
}
cout<<"\n The elements of the matrix are ";
for(i=0;i<3;i++)
{
cout<<”\n”;
for(j=0;j<3;j++)
cout<<"\t", mat[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
transposed_mat[i][j] = mat[j][i];
}
cout<<"\n The elements of the transposed matrix are ";
for(i=0;i<3;i++)
{
cout<<”\n”;
for(j=0;j<3;j++)
cout<<”\t”<<transposed_ mat[i][j];
}
getch();
return 0;
}
Sparse Matrix
Here we will see what is the sparse matrix and how we can represent them
in memory. So a matrix will be a sparse matrix if most of the elements of it
is 0. Another definition is, a matrix with a maximum of 1/3 non-zero elements
(roughly 30% of m x n) is known as sparse matrix.
We can store the information about the sparse matrices using table structure.
Suppose we will create a table called X like below −
Here the first column is holding the Row number, and second one is holding
the column number. The third one is holding the data present at M[row, col].
Each row of this table is called triplets, as there are three parameters. The
first triplet is holding the size information of the matrix. Row = 6 and Column
= 6 is indicating that the matrix M is 6 x 6 matrix. The value field is denoting
the number of non-zero elements present in the array.