You are on page 1of 22

INTRODUCTION TO DATA STRUCTURES

ARRAYS
An array is a fnite collection of similar elements stored in adjacent memory
locations.
Types of Arrays
1. Single dimensional array.
2. Multidimensional array(more than one dimension)( Eg: 2D array(to
dimensional)!"D array(three)!#D array etc).
Examples
1. arr$1%&( a one dimensional array consisting of 1% elements).
2. arr$2&$#&(a to dimensional array ith 2 ros and # columns holding '
elements).
". arr$2&$"&$#&(a three dimensional array ith 2 2D arrays ha(ing " ros and #
columns holding totally 2# elements).
Operations that can be performed on arrays
Operation Description
Traersal !rocessin" each element in the
array#
Search $indin" location of an element %ith
"ien al&e#
Insertion Addin" ne% element to an array#
Deletion Remoin" an element from the
array#
Sortin" Or"ani'in" elements in some order#
(er"in" Combinin" t%o arrays into sin"le
array#
Reersin" Reersin" elements of an array#
!ointer
)t is a normal (aria*le that holds the memory address of a (aria*le#
Example)
int a*+p,
p-.a,
a p
/0
/0
0
(alue
/00 100
Array of !ointers
An array of +ointers is sim+ly a collection of addresses of array elements.
Example
a234
(emory representation of array of elements

/0 10 50 60 30
/00 /01 /06 /07 /08
(emory representation of array of pointers
/00 /01 /06 /07 /08
800 801 806 807 808
(&ltidimensional arrays
Example
a254264214-9
9
9/*1:*
95*6:*
93*7:*
9;*8:
:*
9
9<*/0:*
9//*/1:*
9/5*/6:*
9/3*/7:
:*
9
9/;*/8:*
9/<*10:*
91/*11:*
915*16:
:
:,
Address of (aria*le(a)
Representation of (&ltidimensional arrays in memory
Strin"s
An array of characters are called strings.
Representation of strin"s in memory

R A = I >0
1%% 1%1 1%2 1%" 1%#
Operations that can be performed on Strin"s
Operation Description
?en"th $inds the len"th of strin"
Copy Copies the content of one strin" to
another strin"
Concatenate Appends one strin" at the end of
another strin"
Compare Compares t%o strin"s and @nds
%hether they are identical or not#
T%o dimensional array of strin"s
char str$,&$1%&-
.he frst dimension s+ecifes the num*er of strings and second dimension s+ecifes
the length of strings.
Representation of t%o dimensional array of strin"s in memory
Rai>
0
Srin&>
0
Anil>0 AAay>
0
=iAay>
0
1%% 11% 12% 1"% 1#%
E(enthough 1%*ytes are reser(ed for storing the name /0a(i1 it occu+ies ,*ytes.
.hus , *ytes are asted. So!this +ro*lem can *e a(oided *y using conce+t called
/array of +ointers to strings1
1 2 " # , 2 3 ' 4 1
%
1
1
1
2
1
"
1
#
1
,
1
2
1
3
1
'
1
4
2
%
2
1
2
2
2
"
2
#
%
th
25D array 1
st
25D array 2
nd
25D array
Array of !ointers to Strin"s
6har 7names$&89
/0a(i1!
/Srinu1!
/Anil1!
/Ajay1!
/:ijay1
;-
Representation of array of pointers to strin"s in memory
Rai>0
/00

,%%
/00 300 600 100 ;00
1%%% 1%%2 1%%# 1%%2 1%%'
DiBerence bet%een Data str&ct&re and ADTCAbstract data typeD
Data Str&ct&re ADT
It is physical implementation of ADT#
It means the act&al representation
of data and al"orithms to
manip&late the data
It is lo"ical implementation of
data#It means the lo"ical ie% of
data and operations to manip&late
the data
It tells ho% the tasE is to be done It tells %hat is the tasE to be done#
Srinu<%
Anil<%
Ajay<%
:ijay<%
#%%
3%%
2%%
Adanta"es of Data str&ct&res)
/# EFciency)
!roper choice of data str&ct&re maEes pro"ram eFcient#
1# Re&sability)
Data str&ct&res are re&sable i#e#* a partic&lar data
str&ct&re can be &sed in any other place dependin" on the
reG&irement# Implementation of data str&ct&res can be compiled
into libraries %hich can be &sed by diBerent clients#
5# Abstraction)
A data str&ct&re is speci@ed by ADT %hich proides the
leel of abstraction# The client pro"ram &ses the data str&ct&re
thro&"h the interface only %itho&t "ettin" into implementation
details#
Static and Non static Data str&ct&re
In static data str&ct&re the memory is allocated for data str&ct&re
at compile time# Static data str&ct&re allo%s fast access to elements b&t
insertion and deletion is expensie# Array is an example of static data
str&ct&re#
In Non static data Str&ct&re* the memory is allocated for data
str&ct&re at r&ntime# # Static data str&ct&re allo%s slo% access to
elements b&t insertion and deletion is fast# ?inEed lists is an example of
non static data str&ct&re#
!ro"ram for Representation of !olynomials
#include<stdio.h>
#include<conio.h>
#define max_size 10
//declaring a polynomial having coefficient and exponent
representPolynomialint!int!int"#
displayPolynomialint"#
struct polynomial
$
int coeff#
int exp#
%poly&max_size'#
void main"
$
int coeff!exp!noofterms!i#
clrscr"#
printf()nter the num*er of terms to *e represented for the polynomial("#
scanf(+d(!,noofterms"#
//loop through num*er of terms enter coefficient and exponent
fori-1#i<-noofterms#i.."
$
printf()nter the coefficient and exponentfor the +d th term(!i"#
scanf(+d+d(!,coeff!,exp"#
representPolynomialcoeff!exp!i"#
%
displayPolynomialnoofterms"#
%
representPolynomialint coeff!int exp!int index"
$
poly&index'.coeff-coeff#
poly&index'.exp-exp#
%
displayPolynomialint noofterms"
$
int i-1#
/hilei<-noofterms"
$
ifpoly&i'.exp0-0"
$
printf(+d(!poly&i'.coeff"#
printf(x1("#
printf(+d(!poly&i'.exp"#
i-i.1#
%
ifi<-noofterms"
$
printf(.("#
ifpoly&i'.exp--0"
$
printf(+d(!poly&i'.coeff"#
i-i.1#
%
%
%
%
!ro"ram for !olynomial Addition
#include<stdio.h>
#include<conio.h>
#define max_size 10
struct polynomial
$
int coeff#
int exp#
%poly1&max_size'!poly2&max_size'!poly3&max_size'#
//declaring a polynomial having coefficient and exponent for first polynomial
and second polynomial
represent4irstPolynomialint!int!int"#
represent5econdPolynomialint!int!int"#
addPolynomialstruct polynomial&'!struct polynomial&'"#
displayPolynomial"#
int noofterms1!noofterms2!maxterm!6-1#
void main"
$
int coeff!exp!i#
clrscr"#
printf()nter the num*er of terms to *e represented for the first
polynomial("#
scanf(+d(!,noofterms1"#
printf()nter the num*er of terms to *e represented for the second
polynomial("#
scanf(+d(!,noofterms2"#
//loop through num*er of terms enter coefficient and exponent for the first
polynomial
fori-1#i<-noofterms1#i.."
$
printf()nter the coefficient and exponent of first polynomial for the +d
th term(!i"#
scanf(+d+d(!,coeff!,exp"#
represent4irstPolynomialcoeff!exp!i"#
%
//loop through num*er of terms to enter coefficient and exponent for the
second polynomial
fori-1#i<-noofterms2#i.."
$
printf()nter the coefficient and exponent of second polynomial for the +d
th term(!i"#
scanf(+d+d(!,coeff!,exp"#
represent5econdPolynomialcoeff!exp!i"#
%
addPolynomialpoly1!poly2"#
displayPolynomial"#
%
represent4irstPolynomialint coeff!int exp!int index"
$
poly1&index'.coeff-coeff#
poly1&index'.exp-exp#
%
represent5econdPolynomialint coeff!int exp!int index"
$
poly2&index'.coeff-coeff#
poly2&index'.exp-exp#
%
addPolynomialstruct polynomial poly1&'!struct polynomial poly2&'"
$
//get the max term from *oth polynomials and loop through it
maxterm-noofterms1>noofterms27noofterms18noofterms2#
int i-1!9-1#
/hile1"
$
ifpoly1&i'.coeff--0 ,, poly2&9'.coeff--0"
*rea6#
//chec6 if *oth exponents are e:ual or greater.if e:ual add
coefficients.other/ise 9ust copy first coefficient to resultant.
// poly 3 is used for holding results of adding 2 polynomials
ifpoly1&i'.exp>-poly2&9'.exp"
$
ifpoly1&i'.exp--poly2&9'.exp"
$
poly3&6'.coeff-poly1&i'.coeff.poly2&9'.coeff#
poly3&6'.exp-poly1&i'.exp#
i-i.1#
9-9.1#
6-6.1#
%
else
$
poly3&6'.coeff-poly1&i'.coeff#
poly3&6'.exp-poly1&i'.exp#
i-i.1#
6-6.1#
%
%
else
$
poly3&6'.coeff-poly2&9'.coeff#
poly3&6'.exp-poly2&9'.exp#
9-9.1#
6-6.1#
%
%
%
displayPolynomial"
$
int i-1#
//display first polynomial
/hilei<-noofterms1"
$
ifpoly1&i'.exp0-0"
$
printf(+d(!poly1&i'.coeff"#
printf(x1("#
printf(+d(!poly1&i'.exp"#
i-i.1#
%
ifi<-noofterms1"
$
printf(.("#
ifpoly1&i'.exp--0"
$
printf(+d(!poly1&i'.coeff"#
i-i.1#
%
%
%
printf(;n("#
i-1#
//display second polynomial
/hilei<-noofterms2"
$
ifpoly2&i'.exp0-0"
$
printf(+d(!poly2&i'.coeff"#
printf(x1("#
printf(+d(!poly2&i'.exp"#
i-i.1#
%
ifi<-noofterms2"
$
printf(.("#
ifpoly2&i'.exp--0"
$
printf(+d(!poly2&i'.coeff"#
i-i.1#
%
%
%
printf(;n("#
//display resultant
i-1#
/hilei<-6<1"
$
ifpoly3&i'.exp0-0"
$
printf(+d(!poly3&i'.coeff"#
printf(x1("#
printf(+d(!poly3&i'.exp"#
i-i.1#
%
ifi<-6<1"
$
printf(.("#
ifpoly3&i'.exp--0"
$
printf(+d(!poly3&i'.coeff"#
i-i.1#
%
%
%
%
!ro"ram for sparse matrix manip&lation
// 5P=>5) ?=@>AB
// if the matrix contains so many zero elements then that matrix is called
sparse matrix.
// Co/ the tas6 is to respresent sparse matrix containing non zero elements
and add 2 sparse matrices
// )g for sparse matrix containing so many zero elements no of ro/s-3 and no
of columns-3"
// 0 0 3
// 0 2 0
// D 0 <2
// 5ince the matrix contains so many zero elements there must *e alternative
/ay to store non zero elements to save memory space.
// Co/ the sparse matrix containing non zero elements is
// 3 3 D 3-no of ro/s!3-no of columns!D-count of non zero elements in sparse
matrix"
// 0 2 33-non zero element present at 0th ro/ and 2nd column"
// 1 1 22-non zero element present at 1st ro/ and 1st column"
//2 0 DD-non zero element present at 2th ro/ and 0th column"
// 2 2 <2<2-non zero element present at 2nd ro/ and 2nd column"
#include<stdio.h>
#include<conio.h>
#define max1 3// ro/s
#define max2 3// columns
// declare sparse matrices 1 and 2 containing zero elements
// declare sparse matrices 1 and 2 containing non zero elements
// declare result array *y adding sparse matrices 1 and 2 containing non zero
elements
//
Ant
sparse_zeroarray1&max1Emax2'!sparse_zeroarray2&max1Emax2'!sparse_nonzeroarray1
&100'!sparse_nonzeroarray2&100'!sparse_result&100'!i!n!count1-0!count2-0#
read5parseFero=rray1int&'"#// Anput sparse matrices 1 and 2 containing so
many zero elements
read5parseFero=rray2int&'"#
countConFero)lements"#// count non zero elements in sparse matrices 1 and 2
containing zero elements
// >epresent sparse matrix 1 and 2 containing non zero elements.
create5parseConFero?atrix1int&'"#
create5parseConFero?atrix2int&'"#
// add sparse matrices 1 and 2 containing non zero elements.
add5parse?atrixint&'!int&'"#
display5parse>esult=rray"#
main"
$
clrscr"#
read5parseFero=rray1sparse_zeroarray1"#
read5parseFero=rray2sparse_zeroarray2"#
countConFero)lements"#
create5parseConFero?atrix1sparse_zeroarray1"#
create5parseConFero?atrix2sparse_zeroarray2"#
add5parse?atrixsparse_nonzeroarray1!sparse_nonzeroarray2"#
display5parse>esult=rray"#
%
read5parseFero=rray1int sparse_zeroarray1&'"
$
printf()nter sparse zero matrix1("#
fori-0#i<max1Emax2"#i.."
$
printf()nter element +d(!i"#
scanf(+d(!,n"#
sparse_zeroarray1&i'-n#
%
%
read5parseFero=rray2int sparse_zeroarray2&'"
$
printf()nter sparse zero matrix 2("#
fori-0#i<max1Emax2"#i.."
$
printf()nter element +d(!i"#
scanf(+d(!,n"#
sparse_zeroarray2&i'-n#
%
%
countConFero)lements"
$
fori-0#i<max1Emax2"#i.."
$
ifsparse_zeroarray1&i'0-0"
count1-count1.1#
%
fori-0#i<max1Emax2"#i.."
$
ifsparse_zeroarray2&i'0-0"
count2-count2.1#
%
%
create5parseConFero?atrix1int sparse_zeroarray1&'"
$
int ro/-0!col-<1!index-2#
// store no of ro/s!no of columns and count of non zero elements at the
index 0!1!2
sparse_nonzeroarray1&0'-max1#
sparse_nonzeroarray1&1'-max2#
sparse_nonzeroarray1&2'-count1#
// loop through total no of elements in sparse matrix containing zero elements
fori-0#i<max1Emax2"#i.."
$
// Ancrement the column to get the position of non zero element position
col-col.1#
// if GiG has reached the columns then increment the ro/ *y 1 and column
is initialized to zero
ifi+max2"--0" ,, i0-0"
$
ro/-ro/.1#
col-0#
%
// increment the index and store the ro/!column!nonzeroelement in sparse
matrix containing non zero elements
ifsparse_zeroarray1&i'0-0"
$
index-index.1#
sparse_nonzeroarray1&index'-ro/#
index-index.1#
sparse_nonzeroarray1&index'-col#
index-index.1#
sparse_nonzeroarray1&index'-sparse_zeroarray1&i'#
%
%
%
create5parseConFero?atrix2int sparse_zeroarray2&'"
$
int ro/-0!col-<1!index-2#
sparse_nonzeroarray2&0'-max1#
sparse_nonzeroarray2&1'-max2#
sparse_nonzeroarray2&2'-count2#
fori-0#i<max1Emax2"#i.."
$
col-col.1#
ifi+max2"--0" ,, i0-0"
$
ro/-ro/.1#
col-0#
%
ifsparse_zeroarray2&i'0-0"
$
index-index.1#
sparse_nonzeroarray2&index'-ro/#
index-index.1#
sparse_nonzeroarray2&index'-col#
index-index.1#
sparse_nonzeroarray2&index'-sparse_zeroarray2&i'#
%
%
%
// sparse matrix 1 containing zero elements is
// 1 0 2
// 0 3 0
// 0 0 0
// sparse matrix 1 containing non zero elements is
// 3 3 3<ro/ 0
// i is pointing to next ro/ and ro/a and cola value changes from time to
time
// 0 0 1<1 here ro/a-0 cola-0 vala-1
// 0 2 2<2
// 1 1 3<3
//
// sparse matrix 2 containing zero elements is
// 0 0 0
// 1 0 2
// 0 H 0
// sparse matrix 2 containing non zero elements is
// 3 3 3<ro/ 0
// 9 is pointing to next ro/ and ro/* and col* value changes from time to
time
// 1 0 1<1 here ro/*-1 col*-0 val*-1
// 1 2 2<2
// 2 1 H<3
// >ules8
// 1" if ro/a and ro/*!cola and col* are e:ual then store ro/a!cola and
vala.val*" into result array and increment i and 9
// 2" if ro/a and ro/* are e:ual then compare cola and col*
// a"if cola is less than col* then store ro/a!cola!vala into result array
and increment i
// *" if cola is greater than col* then store ro/*!col*!val* into result
array and increment 9
// 3" if ro/a is less than ro/* then store ro/a!cola!vala into result array
and increment i
// D" if ro/a is greater than ro/* then store ro/*!col*!val* into result array
and increment 9
// >esultant
// 3 3 I
// 0 0 1
// 0 2 2
// 1 0 1
// 1 1 3
// 1 2 2
// 2 1 H
add5parse?atrixint sparse_nonzeroarray1&'!int sparse_nonzeroarray2&'"
$
int ro/a!ro/*!vala!cola!col*!val*!nonFeroJount!9-1!6-1#
i-1#
nonFeroJount-count1.count2#
int ind-1#
/hileind<-nonFeroJount"
$
ifi<-count1"
$
ro/a-sparse_nonzeroarray1&iE3".0'#
cola-sparse_nonzeroarray1&iE3".1'#
vala-sparse_nonzeroarray1&iE3".2'#
%
else
ro/a-cola-K0#
if9<-count2"
$
ro/*-sparse_nonzeroarray2&9E3".0'#
col*-sparse_nonzeroarray2&9E3".1'#
val*-sparse_nonzeroarray2&9E3".2'#
%
else
ro/*-col*-K0#
ifro/a--ro/*"
$
ifcola--col*"
$
sparse_result&6E3.0'-ro/a#
sparse_result&6E3.1'-cola#
sparse_result&6E3.2'-vala.val*#
i-i.1#
9-9.1#
//nonFeroJount-nonFeroJount<1#
%
ifcola<col*"
$
sparse_result&6E3.0'-ro/a#
sparse_result&6E3.1'-cola#
sparse_result&6E3.2'-vala#
i-i.1#
%
ifcol*<cola"
$
sparse_result&6E3.0'-ro/*#
sparse_result&6E3.1'-col*#
sparse_result&6E3.2'-val*#
9-9.1#
%
6-6.1#
%
ifro/a<ro/*"
$
sparse_result&6E3.0'-ro/a#
sparse_result&6E3.1'-cola#
sparse_result&6E3.2'-vala#
i-i.1#
6-6.1#
%
ifro/*<ro/a"
$
sparse_result&6E3.0'-ro/*#
sparse_result&6E3.1'-col*#
sparse_result&6E3.2'-val*#
9-9.1#
6-6.1#
%
ind-ind.1#
%
sparse_result&0'-max1#
sparse_result&1'-max2#
sparse_result&2'-nonFeroJount#
%
display5parse>esult=rray"
$
printf(@he sparse array contents are("#
fori-0#i<sparse_result&2'.1"Emax1#i.."
$
ifi+max1--0"
printf(;n("#
printf(+d (!sparse_result&i'"#
%
%
ROH (AIOR RE!RESENTATION O$ THO DI(ENSIONA? ARRAY int A254254

CO?U(N (AIOR RE!RESENTATION O$ THO DI(ENSIONA? ARRAY int A254
254
A204204
A2042/4
A204214
A2/4204
A2/42/4
A2/4214
A214204
A2142/4
A214214
A204204
A2/4204
A2/4204
A2042/4
A2/42/4
A2142/4
A204214
A2/4214
A214214
0o1
0o2
6ol1
6ol2
JASE ADDRESS CA?CU?ATION O$ int A204214
Su++ose the starting address of the array is ,%%(A$%&$%&)
=ase address of A$i&$j&8(i7r>j)>=ase address of A$%&$%&
r s+ecifes the num*er of ste+s to reach from A$%&$%& to A$i&$j&
=ase Address of A$%&$2&8(%7">2)>,%%
8,%%>2(inde? of array)8,%#
Determination of r
A$%&$%&5@1
A2042/4KL1
A204214KL5CrD
ROH (AIOR RE!RESENTATION O$ (U?TI DI(ENSIONA? ARRAY int A2i42A42E4
Mere i speci@es plane n&mber A speci@es ro% and E speci@es col&mn of
array#
ExampleA214254254

CO?U(N (AIOR RE!RESENTATION O$ THO DI(ENSIONA? ARRAY int A2i42A4
2E4
A204204204
A2042042/4
A204204214
A2042/4204
A2042/42/4
A2042/4214
A204214204
A2042142/4
A204214214
A2/4204204
A2/42042/4
A2/4204214
A2/42/4204
A2/42/42/4
A2/42/4214
A2/4214204
A2/42142/4
A2/4214214
0o1
0o %
0o
0o %
0o 1
0o 2
Alane %
Alane 1
Mere i speci@es plane n&mber A speci@es ro% and E speci@es col&mn of
array#
ExampleA214254254

JASE ADDRESS CA?CU?ATION O$ (U?TI DI(ENSIONA? ARRAY
A204204204
A2042/4204
A204214204
A2042042/4
A2042/42/4
A2042142/4
A204204214
A2042/4214
A204214214
A2/4204204
A2/42042/4
A2/4204214
A2/42042/4
A2/42/42/4
A2/42142/4
A2/4204214
A2/42/4214
A2/4214214
6ol1
6ol %
6ol 2
6ol%
6ol 1
6ol 2
Alane %
Alane 1
A$i1&$i2&$i"&
Bere the *ase address can *e calculated as
=ase address8=ase address of starting array>(i17r2>i2)>(i27r">i")
Bere r2 s+ecifes num*er of ste+s to reach from i1 to i2 and r" s+ecifes num*er of
ste+s to reach from i2 to i"
Example
Assume starting address of array is ,%%
A$%&$1&$2&
=ase address of A$%&$1&$2&8=ase Address(A)>((%72>1)>(17">2))
8=ase Address>2
AJSTRACT DATA TY!ECADTD
)t is a tool for s+ecifying logical +ro+erties of data ty+e. A data ty+e is a collection of
(alues and set of o+erations on those (alues.
AD. consists of 2 +arts.
1. :alue defnition
2. C+erator defnition.
:alue defnition defnes the collection of (alues for the AD. and consists of 2 +arts.
1. Defnition clause
2. 6ondition clause.
C+erator defnition consists of " +arts.
1. Beader(function name)
2. o+tional +recondition(s+ecifes initial condition)
". +ostcondition.
ADT S!ECI$ICATION $OR RATIONA? NU(JER
NNal&e de@nition
Abstract typedefOint*intLrational,Cspeci@es seG&ence of len"th 1
consistin" of 1 inte"ersD
Crational is the name and int*int are the parametersD
Condition rational2/4P-0,
Cif and b are parameters b sho&ld not be eG&al to 0D
NNOperator de@nition
NN Represent Rational n&mber
Abstract rational representRationalCa*bD
int a*b,
!recondition bP-0,
!ostcondition representRational204-a,
representRational2/4-b,
NNadd Rational n&mbers
Abstract rational addCa*bD
rational a*b,
!ostcondition add204-a204+b2/4Qb204+a2/4,
add2/4-a2/4+b2/4,
Example
a0Nb0Qa/Nb/-a0+b/Qa/+b0NCa/+b/D
NN (&ltiply rational n&mbers
Abstract rational m&lCa*bD
rational a*b,
!ostcondition m&l204-a204+b204,
m&l2/4-a2/4+b2/4,
ADT $OR =ARYINR CMARACTER ?ENRTM STRINRS
NNal&e de@nition
abstract typedefOOcharLL strin",
NNOperator de@nition
abstract len"thCsD
strin" s,
postcondition len"th--lenCsD,
abstract strin" concatCs/*s1D
strin" s/*s1,
postcondition concat--s/Qs1,
abstract strin" s&bstrCs/*i*AD
strin" s/,
int i*A,
precondition iOlenCs/D,
AO-lenCs/DKi,
postcondition s&bstr--s&bCs/*i*AD,

You might also like