KEMBAR78
Introduction to Data Structure | PPTX
DATA
STRUCTURE
Er. A. P. Chougule
Syllabus
Unit 1
◦ Basic Terminology, Elementary data structure organization, Classification
of data structure,
◦ Operations on data structures-Traversing, Inserting, deleting, Searching,
sorting, merging
◦ Different Approaches to designing an algorithm ·
Top-Down approach · Bottom-up approach
◦ Complexity -Time complexity ,Space complexity , Big ‘O’ Notation
Basic Terminology
Data
◦ Data can be defined as an elementary value or the collection of values.
◦ Ex.- student's name and its id are the data about the student.
Record
◦ Collection of data or data items those are related to each other is
known as Record. The elements of records are usually called fields or
members.
Computer is an electronic machine which is used for data processing and
manipulation.
When programmer collects such type of data for processing, he would require to store
all of them in computers main memory.
In order to make how computer work we need to know
Representation of data in computer.
Accessing of data.
How to solve problem step by step.
For doing all of this task we used Data Structure
What is Data
Structure
 A data structure is a
specialized format for
organizing, processing,
retrieving and storing data.
 In computer programming, a
data structure may be
selected or designed to store
data for the purpose of
working on it with various
algorithms.
Data Structure can be defined as the group of data elements which provides an efficient
way of storing and organizing data in the computer so that it can be used efficiently.
examples of Data Structures are arrays, Linked List, Stack, Queue, etc.
Data Structures are widely used in almost every aspect of Computer Science i.e. Operating
System, Compiler Design, Artificial intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way.
It plays a vital role in enhancing the performance of a software or a program as the main
function of the software is to store and retrieve the user’s data as fast as possible
Data Structure
◦ A data structure is a particular way of organizing data in a computer so that it can be used effectively.
◦ For example, we can store a list of items having the same data-type using the array data structure.
The representation of particular data structure in the main memory of a computer is called as storage
structure.
The storage structure representation in auxiliary memory is called as file structure.
It is define as the way of storing and manipulating data in organized form so that it can be used efficiently
Data Structure mainly specifies the following four things:
1)organization of data 2)accessing method 3)degree of associativity 4) processing alternative for
information
Algorithm + Data Structure = Program
Data Structure study Covers the following points
1) Amount of memory require to store
2) Amount of time require to process
3) Representation of data in memory
4) Operations performs on data
Elementary data structure organization
Data
Structure
Primitive
Integer Float Character Pointer
Non
Primitive
Linear
Array Stack Queue
Linked
List
Non
Linear
Tree Graph
Primitive Data Structure
◦ Primitive Data Structure are basic structure and directly operated upon by machine instructions.
◦ Primitive data structures have different representations on different computers.
◦ Integers, floats, character and pointers are example of primitive data structures.
◦ These data types are available in most programming languages as built in type.
Integer: It is a data type which allows all values without fraction part. We can used it for whole
numbers.
Float: It is a data type which is use for storing fraction numbers.
Character: It is a data type which is used for character values.
Pointer: A variable that hold memory address of another variable are called pointer.
Non Primitive Data Structure
◦ Non-primitive data structures are those data structures which are created using
primitive data structures.
◦ Examples of non-primitive data structures are linked lists, stacks, trees, and
graphs.
◦ A non – primitive data type is further divided into
1. Linear data structure
2. non – Linear data structure.
Linear Data Structure
A data structure is said to be linear if its elements form a sequence or a linear list.
There are basically two ways of representing such linear structure in memory.
1. One way is to have the linear relationships between the elements represented by
means of sequential memory location. These linear structures are called arrays.
2. The other way is to have the linear relationship between the elements represented
by means of pointers or links. These linear structures are called linked lists. The
common examples of linear data structure are Arrays, Queues, Stacks, Linked lists
Non-linear Data Structure:
◦ A data structure is said to be non-linear if the data are not arranged in sequence or a
linear.
◦ The insertion and deletion of data is not possible in linear fashion. This structure is
mainly used to represent data containing a hierarchical relationship between
elements.
◦ Trees and graphs are the examples of non-linear data structure.
Operations on data structures
◦ The data appearing in our data structures are processed by means of certain operations. The
use of those data structure operations are as follows:
1) Traversing :
◦ This operation leads to accessing each record/node exactly once so that certain items in the
record may be processed. (This accessing and processing is sometimes called “visiting” the
record.)
2) Searching :
◦ In this operation We can find the location of the desired node with a given key value, or finding
the locations of all such nodes which satisfy one or more conditions.
3) Inserting :
◦ It leads to add a new record to the structure.
4) Deleting :
◦ It leads to removing a record from the structure.
5) Sorting :
◦ Sort items or nodes in data structure according to the value of the field or set of fields.
6) Merging :
◦ This operation is used to merge two structures or more to constituting one structure.
Array
◦ Arrays are defined as the collection of similar type of data items stored at contiguous memory
locations.
◦ Arrays are the derived data type in C programming language which can store the primitive type
of data such as int, char, double, float, etc.
◦ Array is the simplest data structure where each data element can be randomly accessed by
using its index number.
◦ For example, if we want to store the marks of a student in 6 subjects, then we don't need to
define different variable for the marks in different subject. instead of that, we can define an array
which can store the marks in each subject at a the contiguous memory locations.
The array marks[10] defines the marks of the student in 10 different subjects where each subject
marks are located at a particular subscript in the array i.e. marks[0] denotes the marks in first
subject, marks[1] denotes the marks in 2nd subject and so on.
//Traversing of 1D Array
int main()
{
int a[1000],i,n;
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
return 0;
}
2D array
◦ 2D array can be defined as an array of arrays. The 2D array is organized as matrices
which can be represented as the collection of rows and columns.
◦ syntax of declaring two dimensional array
int arr[max_rows][max_columns];
Initializing 2D Arrays
int arr[2][2] = {0,1,2,3}; 0 1
2 3
arr[0][0]=0
arr[0][1]=1
arr[1][0]=2
arr[1][1]=3
//Traversing od 2D array
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("n printing the elements ....n");
for(i=0;i<3;i++)
{
printf("n");
for (j=0;j<3;j++)
{
printf("%dt",arr[i][j]);
} } }
#include <stdio.h>
#include<conio.h>
void main()
{
int array[50], position, i, n, value;
clrscr();
printf("Enter number of elements in the arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Please enter the location where you want to insert an new elementn");
scanf("%d", &position);
printf("Please enter the valuen");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i--)
{
array[i+1] = array[i];
}
array[position-1] = value;
printf("Resultant array isn");
for (i = 0; i <= n; i++)
{
printf("%dn", array[i]);
}
getch();
}
#include <stdio.h>
#include <conio.h>
int main()
{
int array[100], position, i, n;
clrscr();
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( i = 0 ; i < n ; i++ )
{
scanf("%d", &array[i]);
}
printf("Enter the location where you wish to delete elementn");
scanf("%d", &position);
if ( position >= n+1 )
{
printf("Deletion not possible.n");
}
else
{
for ( i = position - 1 ; i < n - 1 ; i++ )
array[i] = array[i+1];
printf("Resultant array isn");
for( i = 0 ; i < n - 1 ; i++ )
printf("%dn", array[i]);
}
getch();
}
Algorithm
◦ Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output.
◦ An algorithm can be implemented in more than one programming language.
Efficient Algorithm should have
following Characteristics
1. It should be easy and simple in language.
2. It should not repeat task again and again.
3. Every step must be accurate & complete in itself.
4. It can be easily modified if necessary.
5. It should be easily understandable to others & steps should be clear & concise.
6. It should be economical in use of computer time, computer storage and peripherals.
7. It should be correct for clearly defined situations.
Different Approaches to designing an algorithm
Top-Down approach-
◦ This approach divides the problem into manageable Segments.
◦ It is an approach to a problem that begins at highest level and ends with the low-level implementation.
Bottom-up approach-
o Solves the fundamental low-level problem and integrates them into a larger one.
o Bottom-up approach begins with elementary modules and then combine them further.
Top-Down approach
◦ In top down approach, we use following approach to solve any problem.
1.First we will make a high level design of a problem statement.
2.After that we will write the main function.
3.From the main function we will call the sub functions.
Generally, the large program is divided into small small sub functions(each function will do a
specific task) which improves the modularity of a program.
Using that we can easily debug the program.
4.Later we will implement(code) all the sub functions based on requirements.
◦ Ex.- simple calculator.
Bottom-up approach
◦ Bottom-Up Model is a system design approach where parts of the system are
defined in details.
◦ Once these parts are designed and developed, then these parts or components
are linked together to prepare a bigger component.
◦ This approach is repeated until the complete system is built.
◦ Ex.- Student Management System
1. Divide and Conquer Approach
o It is a top-down approach.
o The algorithms which follow the divide & conquer techniques involve three steps:
Divide the original problem into a set of subproblems.
Solve every subproblem individually, recursively.
Combine the solution of the subproblems (top level) into a solution of the whole original problem.
Ex.- Merge Sort
2. Greedy Technique:
 Greedy method is used to solve the optimization problem.
 An optimization problem is one in which we are given a set of input values, which are required either to be
maximized or minimized (known as objective), i.e. some constraints or conditions.
 Greedy Algorithm always makes the choice (greedy criteria) looks best at the moment, to optimize a given
objective.
 The greedy algorithm doesn't always guarantee the optimal solution however it generally produces a solution
that is very close in value to the optimal.
Ex.- Minimal Spanning Tree Algorithm
3. Dynamic Programming:
Dynamic Programming is a bottom-up approach
Solve all possible small problems and then combine them to obtain solutions
for bigger problems.
Ex.- Shortest Path algorithms
Complexity
◦ Complexity of an algorithm is a measure of the amount of time and/or
space required by an algorithm for an input of a given size.
Time complexity
◦ Time Complexity of an algorithm is the representation of the amount of
time required by the algorithm to execute to completion.
◦ Time requirements can be denoted or defined as a numerical function
t(N), where t(N) can be measured as the number of steps, provided
each step takes constant time.
Space complexity
◦ Space complexity of an algorithm represents the amount of memory space needed
the algorithm in its life cycle.
◦ Space needed by an algorithm is equal to the sum of the following two components
◦ A fixed part that is a space required to store certain data and variables (i.e. simple
variables and constants, program size etc.), that are not dependent of the size of the
problem.
◦ A variable part is a space required by variables, whose size is totally dependent on
the size of the problem. For example, recursion stack space, dynamic memory
allocation etc.
Big ‘O’ Notation
Introduction to Data Structure

Introduction to Data Structure

  • 1.
  • 2.
    Syllabus Unit 1 ◦ BasicTerminology, Elementary data structure organization, Classification of data structure, ◦ Operations on data structures-Traversing, Inserting, deleting, Searching, sorting, merging ◦ Different Approaches to designing an algorithm · Top-Down approach · Bottom-up approach ◦ Complexity -Time complexity ,Space complexity , Big ‘O’ Notation
  • 3.
    Basic Terminology Data ◦ Datacan be defined as an elementary value or the collection of values. ◦ Ex.- student's name and its id are the data about the student. Record ◦ Collection of data or data items those are related to each other is known as Record. The elements of records are usually called fields or members.
  • 5.
    Computer is anelectronic machine which is used for data processing and manipulation. When programmer collects such type of data for processing, he would require to store all of them in computers main memory. In order to make how computer work we need to know Representation of data in computer. Accessing of data. How to solve problem step by step. For doing all of this task we used Data Structure
  • 6.
    What is Data Structure A data structure is a specialized format for organizing, processing, retrieving and storing data.  In computer programming, a data structure may be selected or designed to store data for the purpose of working on it with various algorithms.
  • 7.
    Data Structure canbe defined as the group of data elements which provides an efficient way of storing and organizing data in the computer so that it can be used efficiently. examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are widely used in almost every aspect of Computer Science i.e. Operating System, Compiler Design, Artificial intelligence, Graphics and many more. Data Structures are the main part of many computer science algorithms as they enable the programmers to handle the data in an efficient way. It plays a vital role in enhancing the performance of a software or a program as the main function of the software is to store and retrieve the user’s data as fast as possible
  • 8.
    Data Structure ◦ Adata structure is a particular way of organizing data in a computer so that it can be used effectively. ◦ For example, we can store a list of items having the same data-type using the array data structure.
  • 9.
    The representation ofparticular data structure in the main memory of a computer is called as storage structure. The storage structure representation in auxiliary memory is called as file structure. It is define as the way of storing and manipulating data in organized form so that it can be used efficiently Data Structure mainly specifies the following four things: 1)organization of data 2)accessing method 3)degree of associativity 4) processing alternative for information Algorithm + Data Structure = Program Data Structure study Covers the following points 1) Amount of memory require to store 2) Amount of time require to process 3) Representation of data in memory 4) Operations performs on data
  • 10.
    Elementary data structureorganization Data Structure Primitive Integer Float Character Pointer Non Primitive Linear Array Stack Queue Linked List Non Linear Tree Graph
  • 11.
    Primitive Data Structure ◦Primitive Data Structure are basic structure and directly operated upon by machine instructions. ◦ Primitive data structures have different representations on different computers. ◦ Integers, floats, character and pointers are example of primitive data structures. ◦ These data types are available in most programming languages as built in type. Integer: It is a data type which allows all values without fraction part. We can used it for whole numbers. Float: It is a data type which is use for storing fraction numbers. Character: It is a data type which is used for character values. Pointer: A variable that hold memory address of another variable are called pointer.
  • 12.
    Non Primitive DataStructure ◦ Non-primitive data structures are those data structures which are created using primitive data structures. ◦ Examples of non-primitive data structures are linked lists, stacks, trees, and graphs. ◦ A non – primitive data type is further divided into 1. Linear data structure 2. non – Linear data structure.
  • 13.
    Linear Data Structure Adata structure is said to be linear if its elements form a sequence or a linear list. There are basically two ways of representing such linear structure in memory. 1. One way is to have the linear relationships between the elements represented by means of sequential memory location. These linear structures are called arrays. 2. The other way is to have the linear relationship between the elements represented by means of pointers or links. These linear structures are called linked lists. The common examples of linear data structure are Arrays, Queues, Stacks, Linked lists
  • 14.
    Non-linear Data Structure: ◦A data structure is said to be non-linear if the data are not arranged in sequence or a linear. ◦ The insertion and deletion of data is not possible in linear fashion. This structure is mainly used to represent data containing a hierarchical relationship between elements. ◦ Trees and graphs are the examples of non-linear data structure.
  • 15.
    Operations on datastructures ◦ The data appearing in our data structures are processed by means of certain operations. The use of those data structure operations are as follows: 1) Traversing : ◦ This operation leads to accessing each record/node exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called “visiting” the record.) 2) Searching : ◦ In this operation We can find the location of the desired node with a given key value, or finding the locations of all such nodes which satisfy one or more conditions. 3) Inserting : ◦ It leads to add a new record to the structure.
  • 16.
    4) Deleting : ◦It leads to removing a record from the structure. 5) Sorting : ◦ Sort items or nodes in data structure according to the value of the field or set of fields. 6) Merging : ◦ This operation is used to merge two structures or more to constituting one structure.
  • 17.
    Array ◦ Arrays aredefined as the collection of similar type of data items stored at contiguous memory locations. ◦ Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. ◦ Array is the simplest data structure where each data element can be randomly accessed by using its index number. ◦ For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variable for the marks in different subject. instead of that, we can define an array which can store the marks in each subject at a the contiguous memory locations. The array marks[10] defines the marks of the student in 10 different subjects where each subject marks are located at a particular subscript in the array i.e. marks[0] denotes the marks in first subject, marks[1] denotes the marks in 2nd subject and so on.
  • 18.
    //Traversing of 1DArray int main() { int a[1000],i,n; printf("Enter size of array: "); scanf("%d",&n); printf("Enter %d elements in the array : ", n); for(i=0;i<n;i++) { scanf("%d", &a[i]); } printf("nElements in array are: "); for(i=0;i<n;i++) { printf("%d ", a[i]); } return 0; }
  • 19.
    2D array ◦ 2Darray can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. ◦ syntax of declaring two dimensional array int arr[max_rows][max_columns]; Initializing 2D Arrays int arr[2][2] = {0,1,2,3}; 0 1 2 3 arr[0][0]=0 arr[0][1]=1 arr[1][0]=2 arr[1][1]=3
  • 20.
    //Traversing od 2Darray #include <stdio.h> void main () { int arr[3][3],i,j; for (i=0;i<3;i++) { for (j=0;j<3;j++) { printf("Enter a[%d][%d]: ",i,j); scanf("%d",&arr[i][j]); } } printf("n printing the elements ....n"); for(i=0;i<3;i++) { printf("n"); for (j=0;j<3;j++) { printf("%dt",arr[i][j]); } } }
  • 22.
    #include <stdio.h> #include<conio.h> void main() { intarray[50], position, i, n, value; clrscr(); printf("Enter number of elements in the arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (i = 0; i < n; i++) { scanf("%d", &array[i]); } printf("Please enter the location where you want to insert an new elementn"); scanf("%d", &position);
  • 23.
    printf("Please enter thevaluen"); scanf("%d", &value); for (i = n - 1; i >= position - 1; i--) { array[i+1] = array[i]; } array[position-1] = value; printf("Resultant array isn"); for (i = 0; i <= n; i++) { printf("%dn", array[i]); } getch(); }
  • 25.
    #include <stdio.h> #include <conio.h> intmain() { int array[100], position, i, n; clrscr(); printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for ( i = 0 ; i < n ; i++ ) { scanf("%d", &array[i]); } printf("Enter the location where you wish to delete elementn"); scanf("%d", &position);
  • 26.
    if ( position>= n+1 ) { printf("Deletion not possible.n"); } else { for ( i = position - 1 ; i < n - 1 ; i++ ) array[i] = array[i+1]; printf("Resultant array isn"); for( i = 0 ; i < n - 1 ; i++ ) printf("%dn", array[i]); } getch(); }
  • 27.
    Algorithm ◦ Algorithm isa step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. ◦ An algorithm can be implemented in more than one programming language.
  • 28.
    Efficient Algorithm shouldhave following Characteristics 1. It should be easy and simple in language. 2. It should not repeat task again and again. 3. Every step must be accurate & complete in itself. 4. It can be easily modified if necessary. 5. It should be easily understandable to others & steps should be clear & concise. 6. It should be economical in use of computer time, computer storage and peripherals. 7. It should be correct for clearly defined situations.
  • 29.
    Different Approaches todesigning an algorithm Top-Down approach- ◦ This approach divides the problem into manageable Segments. ◦ It is an approach to a problem that begins at highest level and ends with the low-level implementation. Bottom-up approach- o Solves the fundamental low-level problem and integrates them into a larger one. o Bottom-up approach begins with elementary modules and then combine them further.
  • 30.
    Top-Down approach ◦ Intop down approach, we use following approach to solve any problem. 1.First we will make a high level design of a problem statement. 2.After that we will write the main function. 3.From the main function we will call the sub functions. Generally, the large program is divided into small small sub functions(each function will do a specific task) which improves the modularity of a program. Using that we can easily debug the program. 4.Later we will implement(code) all the sub functions based on requirements. ◦ Ex.- simple calculator.
  • 31.
    Bottom-up approach ◦ Bottom-UpModel is a system design approach where parts of the system are defined in details. ◦ Once these parts are designed and developed, then these parts or components are linked together to prepare a bigger component. ◦ This approach is repeated until the complete system is built. ◦ Ex.- Student Management System
  • 32.
    1. Divide andConquer Approach o It is a top-down approach. o The algorithms which follow the divide & conquer techniques involve three steps: Divide the original problem into a set of subproblems. Solve every subproblem individually, recursively. Combine the solution of the subproblems (top level) into a solution of the whole original problem. Ex.- Merge Sort 2. Greedy Technique:  Greedy method is used to solve the optimization problem.  An optimization problem is one in which we are given a set of input values, which are required either to be maximized or minimized (known as objective), i.e. some constraints or conditions.  Greedy Algorithm always makes the choice (greedy criteria) looks best at the moment, to optimize a given objective.  The greedy algorithm doesn't always guarantee the optimal solution however it generally produces a solution that is very close in value to the optimal. Ex.- Minimal Spanning Tree Algorithm
  • 33.
    3. Dynamic Programming: DynamicProgramming is a bottom-up approach Solve all possible small problems and then combine them to obtain solutions for bigger problems. Ex.- Shortest Path algorithms
  • 34.
    Complexity ◦ Complexity ofan algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size.
  • 35.
    Time complexity ◦ TimeComplexity of an algorithm is the representation of the amount of time required by the algorithm to execute to completion. ◦ Time requirements can be denoted or defined as a numerical function t(N), where t(N) can be measured as the number of steps, provided each step takes constant time.
  • 36.
    Space complexity ◦ Spacecomplexity of an algorithm represents the amount of memory space needed the algorithm in its life cycle. ◦ Space needed by an algorithm is equal to the sum of the following two components ◦ A fixed part that is a space required to store certain data and variables (i.e. simple variables and constants, program size etc.), that are not dependent of the size of the problem. ◦ A variable part is a space required by variables, whose size is totally dependent on the size of the problem. For example, recursion stack space, dynamic memory allocation etc.
  • 37.