0% found this document useful (0 votes)
563 views16 pages

Data Structures Unit I Notes

The document provides information about different data structures. It discusses linear data structures like arrays, linked lists, stacks and queues which store elements in a sequential order. Non-linear data structures like trees and graphs where elements are not stored sequentially are also mentioned. It defines an abstract data type as a set of operations without implementation details. Common operations on list abstract data type like create, insert, delete etc are listed. Implementation of list using arrays and linked lists are described along with examples. Concepts of one dimensional and two dimensional arrays are explained.

Uploaded by

bala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
563 views16 pages

Data Structures Unit I Notes

The document provides information about different data structures. It discusses linear data structures like arrays, linked lists, stacks and queues which store elements in a sequential order. Non-linear data structures like trees and graphs where elements are not stored sequentially are also mentioned. It defines an abstract data type as a set of operations without implementation details. Common operations on list abstract data type like create, insert, delete etc are listed. Implementation of list using arrays and linked lists are described along with examples. Concepts of one dimensional and two dimensional arrays are explained.

Uploaded by

bala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

DATA STRUCTURES

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.

Types of data structures


o Linear Data structures
If the elements of a data structure are stored in a linear or sequential
order, then it is a linear data structure. Linear data structures can
be represented in memory in two different ways. One way is to have to
a linear relationship between elements by means of sequential memory
locations. The other way is to have a linear relationship between
elements by means of links. Examples:
o Array
o Linked list
o Stacks
o Queues

o Non – Linear Data structures


If the elements of a data structure are not stored in a sequential order,
then it is a non-linear data structure. The relationship of adjacency is
not maintained between elements of a non-linear data structure.
Examples include trees and graphs.
o Trees
o Graphs

ABSTRACT DATA TYPE

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

A list ADT can be implemented using Array and Linked list.

Array Implementation of List ADT

Arrays can be declared in various ways in different languages. For


illustration, let's take C array declaration.

As per the above illustration, following are the important points to be


considered.

• Index starts with 0.

• Array length is 10 which means it can store 10 elements.

• 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

Creating a List ADT


create()
{
int b[20];
cout<<”Enter the number of nodes";
cin>>n;
for(i=0;i<n;i++)
{
cout<<”\n Enter the Element:"<<i+1;
cin>>b[i];
}
cout<<"\n The Elements of The list ADT are:";
for(i=0;i<n;i++)
{
cout<<"\n"<<b[i];
}
}

Inserting a new element

Existing list with four elements

b
b[0] b[1] b[2] b[3] b[..] b[max]

A new element 57 is inserted in the position 3, After insertion


the list elements are shown below

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++;
}

Deleting an element from the list

Existing list with 5 elements

b
b[0] b[1] b[2] b[3] b[4] b[..]

An element 57 is to be deleted, after deletion the list elements are shown


below

b
b[0] b[1] b[2] b[3] b[4] b[..]

After deletion the number of elements in the list becomes 4.

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];
}
}

Searching an element in the list


Existing list with four elements

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.

struct node NODE


{ Data Next
int data;
struct node *next; Data Field Address Field
} *start ;

There are different types of linked lists


• Singly Linked List
• Circularly Linked List
• Doubly Linked List

Singly Linked List


A singly linked list is the simplest type of linked list in which every node contains
some data and address of the next node of the same data type. A singly linked list
allows traversal of data only in one way.
Arrays
An array is a collection of similar data elements. These data elements have
the same data type. The elements of the array are stored in consecutive
memory locations and are referenced by an index (also known as the
subscript). The subscript is an ordinal number which is used to identify an
element of the array.

An array must be declared before being used. Declaring an array means


specifying the following:
• Data type—the kind of values it can store, for example, int, char, float,
double.
• Name—to identify the array.
• Size—the maximum number of values that the array can hold.
Arrays are declared using the following
syntax: type name[size];
The type can be either int, float, double, char, or any other valid data type.
For example, if we write,
int marks[10];
then the statement declares marks to be an array containing 10 elements.
The memory representation of array is given below.

Two dimensional Arrays


A two-dimensional array is specified using two subscripts where the first
subscript denotes the row and the second denotes the column. The C compiler
treats a two-dimensional array as an array of one-dimensional arrays. Figure
below shows a two-dimensional array which can be viewed as an array of
arrays
Declaring two dimensional Array

A two-dimensional array is declared as:

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

A two-dimensional array is initialized in the same way as a one-dimensional


array is initialized.

For example, int marks[2][3]={90, 87, 78, 68, 62, 71};

Note that the initialization of a two-dimensional array is done row by row. The
above statement can also be written as:

int marks[2][3]={{90,87,78},{68, 62, 71}};


The above two-dimensional array has two rows and three columns. First, the
elements in the first row are initialized and then the elements of the second
row are initialized.

The individual elements of a two-dimensional array can be initialized using


the assignment operator as shown here.

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:

Transpose: Transpose of an m x n matrix A is given as a n x m matrix B,


where Bi,j = Aj,i.

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:

Ci,j = Ai,j + Bi,j


Difference Two matrices that are compatible with each other can be
subtracted, 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 subtracted by writing:

Ci,j = Ai,j – Bi,j

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:

Ci,j = ∑ Ai,k Bk,j for k = 1 to n

program to read and display a 3 x 3 matrix.

#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;
}

program to transpose a 3 X 3 matrix

#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 use matrices in computers memory to do some operations in an efficient


way. But if the matrices are sparse in nature, it may help us to do operations
efficiently, but it will take larger space in memory. That spaces have no
purpose. So we will follow some other kind of structures to store sparse
matrices.

Suppose we have a sparse matrix like below −


As we can see that there are 8 non-zero elements, and 28 zero-values. This
matrix is taking 6*6 = 36 memory spaces. If the size of the matrix is larger,
the wastage of space will be increased.

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.

This table is also taking 9 * 3 = 27 amount of space, so where is the gain?


Well if the matrix size is 8*8 = 64, but only 8 elements are present, then
also the table X will take 27 unit of space. We can see that there are fixed 3
columns, the number of rows varies with the number of non-zero elements.

You might also like