Elementary of
Data Structure &
Algorithms
B. MANDAL
Basic Terminology
A good program is that
 Runs correctly
 Easy to read & understand
 Easy to debug
 Easy to modify
A program should undoubtedly
 Gives correct result &
 Efficiently
To write efficient programs – we need to
enforce certain data management concepts
 A group of data elements that put together under one
name.
 Data Structure is a way of storing and organizing
data in computer system.
 Logically &
 Mathematically
 Purpose :– For efficient access of stored data
Data Structure
 Data: a value or set of values
 Record: collection of data items
 E.g. name, address & subject for a particular student
 File: collection of related records [Dictionary]
 Key: one or more data item(s) which will uniquely
identify each records form a file.
Data Structure – Elementary
 Primitive: atomic, fundamental data types (e.g. integer,
real, Boolean)
 Non-primitive: derived from primitive DS (e.g. array,
structure)
 Linear: elements are stored in linear or sequential manner.
 Linear memory allocations
 Linear relationship between elements
 E.g. Array, linked list, Stack, Queue …
 Non-Linear: elements are not stored in linear manner.
 E.g. Graph, Tree
Classification of Data Structure
 Traversing: Accessing each data item exactly once
 Searching: Finding location of one/more data items
 Inserting: Add new data items
 Deleting: Removing data
 Sorting: Arranging data
 Merging: Combining set of data
Operations on Data Structure
 Way we look at a Data Structure
 Focusing on what it dose
 Ignoring implementation details
 E.g. List, Stack, Queue …
 Real world problem solving
Abstract Data Type (ADT)
 Any well-defined computational procedure
 Takes some value, or set of values, as input
 produces some value, or set of values, as output
An algorithm is a sequence of computational steps that
transform the input into the output.
 For a particular problem – we may have several
algorithms
 One algorithm may not fit for other problems
Algorithm
 Ex. Searching an element from a list
Algo: Search(A, key, N)
{
For i=0 to N
if( A[i] == key )
RETURN (i)
RETURN (-1)
}
 Ex. GCD of two number
Al go rith m: G CD(a, b)
{
wh il e (a ≠ b)
{
if a > b
a ← a – b
el se
b ← b – a
}
R ETU R N (a)
}
 Ex. Factorial of positive integer
Algorithm: Factorial(n)
{
Fact ← 1
If n = 1
return (1)
else
Fact ← n * Factorial(n - 1)
return (Fact)
}
 Collection of similar data item
 Referred by a common name
 Contiguous memory allocation
 E.g. int Student[10];
Array
2-Dimentional Array
 E.g. char matrix[5] [4];

 E.g. char matrix[6] [12];
 Will not fit

 2-dimensional array mapped to 1-dimensional array
 Special way to access
 E.g.
char A[3][4];
A ⇒ 0 1 2 3
0
1
2
A´ ⇒
Representation in Memory
a b c d
e f g h
i j k l
a b c d e f g h i j k l
0 1 2 3 4 5 6 7 8 9 10 11
A [row][col] = A´ [row * m + col]
m  # columns
Dynamic Memory Allocation
 Calloc – contiguous memory allocation
 Arr = (int *) calloc(n, sizeof(int))
 Malloc – block of memory allocation
 Arr = (int *) malloc(n * sizeof(int))
 Free: release memory
 free(Arr)
 **Arr is a pointer to integer
Thank You 

Elementary data structure

  • 1.
    Elementary of Data Structure& Algorithms B. MANDAL
  • 2.
    Basic Terminology A goodprogram is that  Runs correctly  Easy to read & understand  Easy to debug  Easy to modify A program should undoubtedly  Gives correct result &  Efficiently To write efficient programs – we need to enforce certain data management concepts
  • 3.
     A groupof data elements that put together under one name.  Data Structure is a way of storing and organizing data in computer system.  Logically &  Mathematically  Purpose :– For efficient access of stored data Data Structure
  • 4.
     Data: avalue or set of values  Record: collection of data items  E.g. name, address & subject for a particular student  File: collection of related records [Dictionary]  Key: one or more data item(s) which will uniquely identify each records form a file. Data Structure – Elementary
  • 5.
     Primitive: atomic,fundamental data types (e.g. integer, real, Boolean)  Non-primitive: derived from primitive DS (e.g. array, structure)  Linear: elements are stored in linear or sequential manner.  Linear memory allocations  Linear relationship between elements  E.g. Array, linked list, Stack, Queue …  Non-Linear: elements are not stored in linear manner.  E.g. Graph, Tree Classification of Data Structure
  • 6.
     Traversing: Accessingeach data item exactly once  Searching: Finding location of one/more data items  Inserting: Add new data items  Deleting: Removing data  Sorting: Arranging data  Merging: Combining set of data Operations on Data Structure
  • 7.
     Way welook at a Data Structure  Focusing on what it dose  Ignoring implementation details  E.g. List, Stack, Queue …  Real world problem solving Abstract Data Type (ADT)
  • 8.
     Any well-definedcomputational procedure  Takes some value, or set of values, as input  produces some value, or set of values, as output An algorithm is a sequence of computational steps that transform the input into the output.  For a particular problem – we may have several algorithms  One algorithm may not fit for other problems Algorithm
  • 9.
     Ex. Searchingan element from a list Algo: Search(A, key, N) { For i=0 to N if( A[i] == key ) RETURN (i) RETURN (-1) }
  • 10.
     Ex. GCDof two number Al go rith m: G CD(a, b) { wh il e (a ≠ b) { if a > b a ← a – b el se b ← b – a } R ETU R N (a) }
  • 11.
     Ex. Factorialof positive integer Algorithm: Factorial(n) { Fact ← 1 If n = 1 return (1) else Fact ← n * Factorial(n - 1) return (Fact) }
  • 12.
     Collection ofsimilar data item  Referred by a common name  Contiguous memory allocation  E.g. int Student[10]; Array
  • 13.
    2-Dimentional Array  E.g.char matrix[5] [4]; 
  • 14.
     E.g. charmatrix[6] [12];  Will not fit 
  • 15.
     2-dimensional arraymapped to 1-dimensional array  Special way to access  E.g. char A[3][4]; A ⇒ 0 1 2 3 0 1 2 A´ ⇒ Representation in Memory a b c d e f g h i j k l a b c d e f g h i j k l 0 1 2 3 4 5 6 7 8 9 10 11 A [row][col] = A´ [row * m + col] m  # columns
  • 16.
    Dynamic Memory Allocation Calloc – contiguous memory allocation  Arr = (int *) calloc(n, sizeof(int))  Malloc – block of memory allocation  Arr = (int *) malloc(n * sizeof(int))  Free: release memory  free(Arr)  **Arr is a pointer to integer
  • 17.