Professional Documents
Culture Documents
• Module 1 – Introduction
• Module 2 - Stacks
• Module 3 - Linked Lists
• Module 4 - Trees
• Module 5 - Graphs
Introduction to data structures
MODULE 1
⚫ Textbooks:
⚫ 1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data
Structures in C, 2nd Ed, Universities Press,2014.
⚫ 2. Seymour Lipschutz, Data Structures Schaum's Outlines,
Revised 1st Ed, McGraw Hill, 2014.
⚫ Reference Books:
⚫ Gilberg & Forouzan, Data Structures: A Pseudo-code approach
with C, 2Ed, Cengage Learning,2014.
⚫ Reema Thareja, Data Structures using C, 3rd Ed, Oxford
press, 2012.
⚫ Jean-Paul Tremblay & Paul G. Sorenson, An Introduction to
Data Structures with Applications, 2nd Ed, McGraw Hill, 2013.
⚫ A M Tenenbaum, Data Structures using C, PHI, 1989
⚫ Robert Kruse, Data Structures and Program Design in C, 2nd
Ed, PHI, 1996.
Outline
⚫ Introduction to Data Structures
⚫ Classification of Data Structures
⚫ Data structure Operations
⚫ Review of Arrays, Structures, Self
Referential Structures and Unions
⚫ Pointers and Dynamic Memory
Management Functions
⚫ Arrays
⚫ Strings
⚫ Programming Examples
Outline Basic Terminology:
Elementary Data Organization
⚫ Data: Data are simply values or sets of
values.
⚫ Data items: Data items refers to a single
unit of values.
Data structure Operations
⚫ Data items that are divided into sub-items
are called Group items. Ex: An Employee
Name may be divided into three sub
items- first name, middle name, and last
name.
⚫ Data items that are not able to divide into
sub-items are called Elementary items. Ex:
SSN
⚫ Ex: Attributes- Names, Age, Sex,
SSN
⚫ Values- Rohland Gail, 34, F,
134-34-5533
⚫ Field: is a single elementary unit of
information representing
an attribute of an entity.
⚫ Record is the collection of field values of a
given entity.
⚫ File is the collection of records of the entities
in a given entity
set.
⚫ In variable-length records file records may
contain different lengths.
⚫ Example: Student records have variable
lengths, since different students take
different numbers of courses.
Variable-length records have a minimum
and a maximum length.
⚫ What is data type?
⚫ A data type is a collection of objects (data)
and set of operations that act on those
objects
⚫ For example : if data is integer the
operations may be add, subtract, multiply,
divide and any other operations
appropriate for the data
⚫ What is data structures?
⚫ Data structure is a method for storing and
organizing data in the computer.
⚫ Data Structure is a way of collecting and
Classification of data structures
DATA
STRUCTURES
10 20 30 40 50
What
0 1 2 3 4 need to
5 be done
…..?????
Algorithm
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 until J >=0
5. Set LA[ J ]=LA[ J-1 ]
6. Set J = J-1
7. Set LA[ 0 ] = ITEM
8. Stop
INSERT(LA, N, K, ITEM)
•1. [Initalize the counter]. Set J=N.
•2. repeat steps 3 and 4 while J>=K
•3. [move jth element downward] set LA[J+1]=LA[J].
•4. [Decrease the counter] set J=J-1 [end of step 2 loop]
•5. [insert element] set LA[K]A=ITEM
•6. [reset N] set N=N+1
•7. Exit
1. Start
2. Set J = N
3. Set N = N+1
4. Set LA [ J ] = ITEM
5. Stop
Insertion at a given index
199
10 20 30 40 50
0 1 2 3 4
5 What
need to be
done
…..?????
Algorithm
⚫INSERT(LA, N, K, ITEM)
⚫ HERE LA is a linear array with N elements and K is a
positive integer such that K<=N. this algorithm inserts an
element ITEM into Kth position in LA.
1.Start
2.Set J = N
N –no of elements in
3.Set N = N+1 the array
K –index at which new
4.Repeat steps 5 and 6 until J >=ITEM
K will be inserted
5.Set LA[ J ]=LA[ J-1 ]
6.Set J = J-1
7.Set LA[ K ] = ITEM
8.Stop
Deletion
⚫ Element can be deleted –
⚫ From the beginning
⚫ At the end or
⚫ At any given index of array.
Deletion : front of the array
10 20 30 40 50
0 1 2 3 4
5
What need to
be done
…..?????
Deletion : at the particular
position
⚫ DELETE(LA, N, K, ITEM)
⚫ HERE LA is a linear array with N elements
and K is a positive integer such that K<=N.
this algorithm deletes an element ITEM
into Kth position in LA.
⚫ 1. Set ITEM=LA[K]
⚫ 2. repeat for J=K to N-1
⚫ 3.[move j+1st element upward] set
LA[ J]=LA[ J+1].
⚫ 4.[end of step 2 loop]
⚫ 5.[reset N] set N=N-1
⚫ 6.Exit
Deletion : at given index
10 20 30 40 50
0 1 2 3 4
5
What need to
be done
…..?????
Merge
10 30 20 40
Sort
20 > 10 40 30
One Dimensional Array
30
10 20 30 40 50
0 1 2 3 4
5
How to
search…..?????
Linear search
⚫ #include<stdio.h>
⚫ int main()
⚫ {
⚫ int a[20],i,x,n;
⚫ printf("How many elements?");
⚫ scanf("%d",&n);
⚫ printf("Enter array elements:n");
⚫ for(i=0;i<n;++i)
⚫ scanf("%d",&a[i]);
⚫ printf("nEnter element to search:");
⚫ scanf("%d",&x);
⚫ for(i=0;i<n;i++)
⚫ if(a[i]==x)
Abstract data type(ADT)
⚫ Abstract Data type (ADT) is a type (or class) for
objects whose behaviour is defined by a set of
value and a set of operations. ... So a user only
needs to know what a data type can do, but not
how it will be implemented.
⚫ EX: Abstract data type is a mathematical model
of a set of data values and the set of operations
that are allowed that is independent of the
implementation.
ADT ARRAY
Objects: A set of pairs<index, value > where for each of index there is a value from
the set item. Index is a finite ordered set of one or more dimensions, for example
{0,…,n-1} for one dimension,{(0,0 ),(0,1),(0.2),(1,0),(1,2),(1,1),(2,1),(2,2),(2,0))}
for two dimensions etc.
Functions:
For all A ∈ Array, I ∈ index, x ∈ item, j, size ∈ integer
1. Array create (j, list) = return an array of j dimensions where list is a j-tuple
whose ith element is the size of ith dimension. Items are undefined.
2. Item retrieve (A, i) = if (I ∈ index) return the item associated with index value i
in array A else return error.
3. Array store (A, i, x) = if (i in index) return an array that is identical to array A
expect the new pair <i,x> has been inserted else return error.
TWO DIMENSIONAL ARRAYS
(1,1) 10 1000
(2,1) 20 1002
(3,1) 50 1004
(1,2) 60 1006
(2,2) 90 1008
(3,2) 40 1010
(2,3) 80 1014
(3,3) 75 1016
(1,4) 55 1018
(2,4) 65 1020
(3,4) 79 1022
⚫ Suppose we have to find the location of A
[3, 2]. The required values are:
⚫ Base (A) : 1000
⚫ w : 2 (because an integer takes 2
bytes in memory)
⚫ M : 3
⚫ J : 3
⚫ K : 2
⚫ Now put these values in the given formula
as below:
⚫ LOC (A [3, 2]) = 1000 + 2 [3 (2-1) + (3-1)]
⚫ = 1000 + 2 [3 (1) + 2]
⚫ = 1000 + 2 [3 + 2]
⚫ = 1000 + 2 [5]
⚫ = 1000 + 10 = 1010
Row Major Order
⚫ The formula is:
⚫ LOC (A [ J, K]) = Base (A) + w [N ( J-1) + (K-1)]
⚫ LOC (A [ J, K]) : is the location of the element
in
the Jth row and Kth
column.
⚫ Base (A) : is the base address of the array
A.
⚫ w : is the number of bytes required
to store single element
of the array A.
⚫ N : is the total number of columns in
⚫ A 3 x 4 integer array A is as below:
(1,1) 10 1000
(1,2) 60 1002
(1,3) 30 1004
(1,4) 55 1006
(2,1) 20 1008
(2,2) 90 1010
(2,3) 80 1012
(2,4) 65 1014
(3,1) 50 1016
(3,2) 40 1018
(3,3) 75 1020
(3,4) 79 1022
⚫ Suppose we have to find the location of A
[3, 2]. The required values are:
⚫ Base (A) : 1000
⚫ w : 2 (because an integer takes 2
bytes in memory)
⚫ N : 4
⚫ J : 3
⚫ K : 2
⚫ Now put these values in the given formula
as below:
⚫ LOC (A [3, 2]) = 1000 + 2 [4 (3-1) + (2-1)]
⚫ = 1000 + 2 [4 (2) + 1]
⚫ = 1000 + 2 [8 + 1]
⚫ = 1000 + 2 [9]
⚫ = 1000 + 18
⚫ = 1018
Program for multiplication of
two matrices
⚫ #include<stdio.h>
⚫ #include<conio.h>
⚫ int main()
⚫ {
⚫ int i,j,k;
⚫ int row1,col1,row2,col2,row3,col3;
⚫ int mat1[5][5], mat2[5][5], mat3[5][5];
⚫ clrscr();
4. The address of each member will be in 4. The address is same for all the members
ascending order. This indicates that of a union. This indicates that every member
memory for each member will start at begins at the same offset value.
different offset values.
5 Altering the value of a member will not 5. Altering the value of any of the member
affect other members of the structure. will alter other member values.
⚫ “calloc” or “contiguous allocation” method
in C is used to dynamically allocate the
specified number of blocks of memory of
the specified type. It initializes each block
with a default value ‘0’.
⚫ Syntax: ptr = (cast-type*)calloc(n,
element-size);
⚫ Example: ptr = (float*) calloc(25,
sizeof(float));
⚫ This statement allocates contiguous space in
memory for 25 elements each with the size of the
float.
calloc()
realloc()
⚫ “realloc” or “re-allocation” method in C is
used to dynamically change the memory
allocation of a previously allocated
memory.
⚫ In other words, if the memory previously
allocated with the help of malloc or calloc
is insufficient, realloc can be used
to dynamically re-allocate memory.
⚫ Re-allocation of memory maintains the
already present value and new blocks will
be initialized with default garbage value.
⚫ Syntax: ptr = realloc(ptr, newSize);
⚫ where ptr is reallocated with new size
free()
⚫ “free” method in C is used to
dynamically de-allocate the memory.
⚫ The memory allocated using functions
malloc() and calloc() is not de-allocated on
their own.
⚫ Hence the free() method is used, whenever
the dynamic memory allocation takes
place.
⚫ It helps to reduce wastage of memory by
freeing it.
⚫ Syntax: free(ptr);
Difference between malloc() and
calloc()
malloc() calloc()
The name malloc stands for The name calloc stands for
memory allocation. contiguous allocation.
⚫ if(p==NULL)
illustration of use of dynamic
array
⚫ #include<stdio.h>
⚫ #include<stdlib.h>
⚫ void main()
⚫ {
⚫ int i,n;
⚫ int *ptr;
⚫ printf(“\n enter the number of
elements\n”);
⚫ scanf(“%d”,&n);
⚫ ptr=(int*)malloc(sizeof(int)*n);
⚫ if(ptr==NULL)
⚫ printf(“\n enter the n elements”);
⚫ for(i=0;<n;i++)
⚫ scanf(“%d”,(ptr+i));
⚫ printf(“the given array elements are \n”);
⚫ for(i=0;i<n;i++)
⚫ printf(“%d”,*(ptr+i));
⚫ getch();
⚫ }
allocating memory dynamically
for 2D array
⚫ #include<stdio.h>
⚫ #include<conio.h>
⚫ #include<alloc.h>
⚫ void main()
⚫ {
⚫ int **a;
⚫ int p,q;
⚫ int **make2darray();
⚫ printf(“\n enter the no of rows”);
⚫ scanf(“%d”,&p);
⚫ printf(“\n enter the no of cols”);
⚫ int **make2darray(int rows,int cols)
⚫ {
⚫ int **x,i;
⚫ x=malloc(rows*sizeof(*x));
⚫ for(i=0;i<rows;i++)
⚫ x[i]=malloc(cols*sizeof(**x));
⚫ return x;
⚫ }
APPLICATIONS OF ARRAYS
⚫ The two major applications of the arrays
are polynomial and sparse matrix .
⚫ 1. POLYNOMIALS
⚫ Polynomial is a sum of terms where each
term has a form axe, where x is the variable,
a is the coefficient and e is the exponent.
⚫ Examples
⚫ A(x) = 3x20+2x5+4
⚫ B(x) = x4+10x3+3x2+1
⚫ The largest exponent of a polynomial is
called its degree.
⚫ In the above example,degree of first
polynomial is 20 and for the second
polynomial it is 4.
⚫ Operations on polynomials
⚫ Addition
⚫ Subtraction
⚫ Multiplication
⚫ But division is not allowed.
⚫ Polynomial addition :
A(x)=∑aixi and B(x)=∑bjxi then
⚫ Polynomial multiplication
A(x).B(x)=∑ A(x)=∑aixi.(∑bjxi))
Polynomial representation
⚫ #define MAX_DEGREE 101
⚫ typedef struct
⚫ {
⚫ int degree;
⚫ float coef[MAX_DEGREE];
⚫ }polynomial;
polynomial a;
ADT polynomial
Polynomial addition
char *strncat (char *dest, char *src, int Concatenate dest and n characters
n) from src; return result in dest
int strncmp (char *str1, char *str2, int Compare first n characters;
n) return < 0 if str1<str2;
0 if str1 = str2;
>0 if str1 > str2
char *strcpy (char *dest, char *src) Copy src into dest; return dest
char *strncpy (char *dest, char Copy n characters from src into
*src, int n) dest; return dest
size_t strlen (char *s) Return the length of ‘s’
⚫ do
⚫ if (text letter == pattern letter)
⚫ compare next letter of pattern to next
⚫ letter of text
⚫ else
⚫ move pattern down text by one letter
⚫ while (entire pattern found or end of text);
PATTERN MATCHING BY
CHECKING END INDICES FIRST
⚫ int nfind(char *string, char *pat)
⚫ {
⚫ int i , j , start = 0;
⚫ int lasts = strlen(string)-1;
⚫ int lastp = strlen(pat)-1;
⚫ int endmatch=lastp;
⚫ for( i = 0; endmatch<=lasts ; endmatch++,
start++)
⚫ {
⚫ if( string[endmatch] == pat[lastp])
⚫ for( j = 0 , i = start; j< lastp && string[i] ==
pat[ j]; i++, j++)
KNUTH MORRIS PRATT STRING
MATCHING ALGORITHMS
⚫ int pmatch(char *string,char *pat)
⚫ {
⚫ int i = 0,j = 0;
⚫ int lens = strlen(string);
⚫ int lenp = strlen(pat);
⚫ while( i < lens && j < lenp)
⚫ {
⚫ if( string[i] == pat[ j])
⚫ {
⚫ i++,j++;
⚫ }