SlideShare a Scribd company logo
1 of 51
Download to read offline
CHAPTER-4
ARRAYS, RECORDS & POINTERS
4.1 INTRODUCTION
Data Structures
Linear
Linear relationship
between the
elements by means
of sequential
memory locations.
Linear relationship
between the
elements by means
of pointers or links
Non-Linear
Eg. Trees, Graphs
2 ways of representing linear structures
Arrays Linked List
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Operations performed on any Linear structure
 Traversal – Processing each element in the list.
 Search – Finding the location of the element with a given value or key.
 Insertion – Adding a new element to the list.
 Deletion – Removing an element from the list.
 Sorting – Arranging the elements in some type of order.
 Merging – Combining 2 lists into a single list.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
When ARRAYS are preferred over Linked list?
 Since arrays are easy to Traverse, Search & Sort, they are frequently used
to store relatively permanent collections of data.
When ARRAYS may not be useful?
 If the size of the structure and the data in the structure are constantly
changing, then the array may not be as useful as the linked list.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.2 LINEAR ARRAYS
Linear Arrays
 A Linear Arrays is a list of a finite number of n of homogenous data
elements, such that,
 The elements of the array are referenced respectively by an index set consisting
of n consecutive numbers.
 The elements of the array are stored respectively in successive memory
locations.
 The number of elements (n) is called the length or size of the array.
 The index set consists of the integers 1, 2, . . . N.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Linear Arrays – Continued . . .
 The length or the number of data elements of the array can be obtained from the index
set by the formula
Length = UB – LB + 1
where UB is the largest index, called the upper bound and
LB is the smallest index called the lower bound of the array.
 Index
 No.of Elements = 21 – 12 + 1 = 10
12 13 14 15 16 17 18 19 20 21
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Linear Arrays
 The elements of the array A may be denoted by the subscript notation
 A1, A2, A3, . . . , An
 Or by the bracket notation
 A[1], A[2], A[3], . . . , A[n].
 The number K in A[K] is called the subscript or an index, and A[K] is called
a subscripted variable.
 Subscripts allow any element of A to be referenced by its relative position
in A.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Linear Arrays
 Where 0 is considered to be the lowest subscript, the highest subscript is
(size-1).
 Arrays can also be processed using FOR Loop.
 Among all the operators , the subscript of the array must have the highest
precedence.
 Any declaration of array must give the 3 items of information:
1. The name of the array
2. The data type of the array
3. The index set of the array
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.4 REPRESENTATION
OF
LINEAR ARRAYS IN
MEMORY
4.4 REPRESENTATION OF
LINEAR ARRAYS IN MEMORY
 Let X be a linear array in the memory of the computer.
 The memory of the computer is simply a sequence of addressed locations
as given below.
X[1]
X[2]
X[3]
X[4]
.
.
.
.
1000
1001
1002
1003
.
.
.
.
 Location( X[K] ) = Address of the element X[K].
 The elements of X are stored in successive memory
locations.
 The computer does not need to keep track of the
address of every element of X.
 But needs to keep track only of the address of the first
element of X, called the base address of X denoted by:
 Base(X)
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.4 REPRESENTATION OF
LINEAR ARRAYS IN MEMORY
 Using the base address, base(x), the computer calculates the address of
any element of X by the following formula:
 Location(X[k]) = Base(X) + w(K – lower_bound)
X[1]
X[2]
X[3]
X[4]
.
.
.
.
1000
1001
1002
1003
.
.
.
.
 Where w is the number of words per memory cell for the
array.
 the time to calculate X[k] is essentially the same for
any value of k.
 Given any subscript k, one can locate and access
the content of x[k] without scanning any other
element of x.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.5
TRAVERSING LINEAR
ARRAYS
4.5 Traversing Linear Arrays
 Let A be a collection of data elements stored in the memory of the
computer.
 Suppose we want to print the contents of each element of A or we want
to count the number of elements with the given property.
 This can be accomplished by traversing A that is by accessing and
processing each element of A exactly once.
1. [ Initialize counter ] Set K := LB.
2. Repeat step-3 and 4 while K <= UB.
3. [ Visit Element ] Apply PROCESS to A[k].
4. [ Increase Counter ] Set K := K + 1.
[ End of Step 2 Loop ]
5. Exit
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.6
INSERTING
&
DELETING
Inserting
 Inserting refers to a operation of adding another element to the collection A.
 Inserting an element at the “end” of a linear array can be easily done provided the
memory locations allocated for the array is large enough to accommodate the adding
element.
 Inserting an element in the middle of the array requires, part of (half of) the elements
must be moved downward to new locations to accommodate the new element and
keep the order of the other elements.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Inserting
NAME NAME NAME NAME
1 Brown 1 Brown 1 Brown 1 Brown
2 Davis 2 Davis 2 Davis 2 Davis
3 John 3 3 3 Ford
4 Smith 4 John 4 John 4 John
5 Wagner 5 Smith 5 Smith 5 Smith
6 6 Wagner 6 Wagner 6 Wagner
7 7 7 7
8 8 8 8
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Algorithm for Inserting into a Linear Array
 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 the K-th position in LA.
1. [ Initialize Counter ] Set J := N.
2. Repeat Step 3 and 4 while J >= K.
3. [ Move j-th element downward] Set LA[ J+1 ] := LA[ J ]
4. [ Decrease counter ] Set J := J – 1
5. [ Insert Element ] Set LA[ K ] := Item
6. [ Reset N ] Set N := N +1
7. Exit
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Deleting
 Deleting refers to the operation of removing one of the elements from A.
 Deleting an element at the “end” of an array presents no difficulties.
 Deleting an element in the middle of the array requires each subsequent elements
need to be moved one location upward in the order to “fill up” the array gap.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Deleting
NAME NAME NAME NAME
1 Brown 1 Brown 1 Brown 1 Brown
2 Davis 2 Davis 2 2 Ford
3 Ford 3 Ford 3 Ford 3 John
4 John 4 John 4 John 4 Smith
5 Smith 5 Smith 5 Smith 5 Wagner
6 Wagner 6 Wagner 6 Wagner 6
7 7 7 7
8 8 8 8
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Algorithm for Deleting from a Linear Array
 Here LA is a linear array with N elements and K is a positive integer such
that K <= N.
 This algorithm deletes the K-th element from 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 Loop ]
5. [ Reset N ] Set N := N -1
6. Exit
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.10
MULTI DIMENSIONAL
ARRAYS
Multidimensional Arrays
 Arrays where elements are referenced by 2 and 3 subscripts are called as
two-dimensional and three-dimensional arrays.
 Some programming languages allow more than 2 subscripts also.
 These are called as multi-dimensional arrays.
 The length of the dimension is the number of integers in its index set.
 The pair of lengths m x n is called the size of the array.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Two-Dimensional Arrays
 A two0dimensional array is a collection pf m.n elements such that each element is specified
by a pair of integers(such as J,K), called subscripts with the property that 1<= J <= m and 1<= K
<= n.
 The element of A with first subscript j and second subscript k will be denoted by Aj,k or A[ j , k ].
 Two dimensional arrays are called matrices in mathematics and tables in business applications.
 A two-dimensional m x n array A, can be drawn as a rectangular array with m rows and n
columns and the element A[J,K] appears in row J and column K.
Columns 1 2 3 4
Row 1 A[1,1] A[1,2] A[1,3] A[1,4]
2 A[2,1] A[2,2] A[2,3] A[2,4]
3 A[3,1] A[3,2] A[3,3] A[3,4]
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Storage Representations
Row-major Representation
 We can consider a two dimensional array since it has elements with a single dimension.
 As a result of this, a two-dimensional array can be assumed as a single column
with many rows and mapped sequentially.
 This is called a row-major representation.
A[1,1] A[1,2] A[1,3] A[1,4]
A[2,1] A[2,2] A[2,3] A[2,4]
A[3,1] A[3,2] A[3,3] A[3,4]
A[1,1]
A[1,2]
A[1,3]
A[1,4]
A[2,1]
A[2,2]
A[2,3]
A[2,4]
A[3,1]
A[3,2]
A[3,3]
A[3,4]M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Row-major Representation
 We can calculate the address of the element of the m-th row and n-th column in a two dimensional
array using the formula shown below:
 addr[(a[m,n]) =
(Total number of rows present before the m-th row * size of a row )
+
(total number of elements present before the n-th element in the m-th row * size of an element)
 Addr(a[m,n]) = ((m-lb1)*(ub2-lb2+1)*size) + ((n-lb2)*size)
 Size of a row = Total no.of elements in a row * size of an element
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Column-major Representation
 We can also represent a two-dimensional array as one single row of columns and map it
subsequently.
 This is called a column-major representation.
A[1,1] A[1,2] A[1,3] A[1,4]
A[2,1] A[2,2] A[2,3] A[2,4]
A[3,1] A[3,2] A[3,3] A[3,4]
A[1,1] A[2,1] A[3,1] A[1,2] A[2,2] A[3,2] A[1,3] A[2,3] A[3,3] A[1,4] A[2,4] A[3,4]
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Representation of 2-dimensional Arrays in Memory
 The programming languages will store two dimensional arrays either
column-by-column (column major order) or row-by-row (row major order).
 The computer keeps track of Base(A) – the address of the first element
A[1,1] of A.
 It computes the address of A[J,K], where A is m x n array, using the formula
❖ LOC(A[J,K]) = Base(A) + w [ M(k-1) + (J-1) ], if column major order is used.
❖ LOC(A[J,K]) = Base(A) + w [ N(J-1) + (K-1) ], if row major order is followed.
 Here w denotes the number of words per memory location for the array A.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
General Multi-dimensional Arrays
 An n dimensional m1 x m2 x . . . X mn array B is a collection of m1.m2. … mn
data elements in which each element is specified by a list of n integers
such as K1, K2, … , Kn called subscripts, with the property that 1<=K1<=m1,
1<=K2<=m2, … 1<=Kn<=mn.
 The element of B with subscripts K1, K2, … , Kn will be denoted by
Bk1,k2,…,kn
or
B[k1,k2,…,kn]
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
A Three Dimensional Array
A[1,1] A[1,2] A[1,3]
A[2,2]
4,
3,
1
4,
3,
2
4,
3,
3
4,
3,
4
4,
3,
5
Row-1
Row-2
Row-3
Row-4
Row-5
Row-6
Col-1 Col-2 Col-3 Col-4 Col-5
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Representation of Polynomials using Arrays
 We need to evaluate polynomial expressions and perform basic arithmetic
operations like addition, multiplication on them.
 We have a method to represent a polynomial of degree ‘n’, by storing the
coefficient of (n+1) terms of a polynomial in a array.
 An expression x + 4 x2 – 7 x5 is a polynomial of degree 5.
 All the elements of an array has 2 values namely coefficient and
exponent.
 We also assume that the exponent of each successive term is less than
that of the previous term.
 A single dimensional array is used to store a single variable polynomial.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial representation using 1-d array
 3 x4 + 5 x3 + 6 x2 + 10 x – 14 is stored as below:
index Value Meaning
0 -14 – 14
1 10 10 x
2 6 6 x2
3 5 5 x3
4 3 3 x4
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Set ‘i ‘ to point to first term in A
Set ‘j ‘ to point to first term in B
Set ‘k ‘ to point to first term in C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k Add the terms pointed by I & j
into C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Increment ‘i ‘ to point to next
term in A
Increment ‘j ‘ to point to nextt
term in B
Set ‘k ‘ to point to next term in C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k Add the terms pointed by I & j
into C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Increment ‘i ‘ to point to next
term in A
Increment ‘j ‘ to point to nextt
term in B
Set ‘k ‘ to point to next term in C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k Add the terms pointed by I & j
into C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Increment ‘i ‘ to point to next
term in A
Increment ‘j ‘ to point to nextt
term in B
Set ‘k ‘ to point to next term in C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10 5
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k Add the terms pointed by I & j
into C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10 5
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Increment ‘i ‘ to point to next
term in A
Increment ‘j ‘ to point to nextt
term in B
Set ‘k ‘ to point to next term in C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10 5 3
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k Add the terms pointed by I & j
into C
Polynomial B is over
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Polynomial addition
0 1 4 0 0 -7
-14 10 6 5 3
-14 11 10 5 3 -7
-7 x5 + 4 x2 + x
3 x4 + 5 x3 + 6 x2 + 10 x - 14
i
j
k
Add the terms pointed by I & j
into C
Copy remaining A
terms into C
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
4.12 POINTERS & POINTER ARRAYS
 A variable P is called a pointer if P “points” to an element, i.e if P contains
the address of that element.
 An array PTR is called a pointer array if each element of PTR is a pointer.
2002 Data element
Address=2002P
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Example
 Consider an organization which divides its members into 4 groups, where each group contains an
alphabetized list of members living in a certain area. There are 21 people totally, divided into 4 groups
of 4, 9, 2 and 6 people. Group-1 Group-2 Group-3 Group-4
Evans Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Troy
WagnerM.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Example – continued …
 Here, the data can be stored into a 2-dimensional array either in row-major or column-major order.
 The drawback here is the wastage of memory locations. (show in green shades)
Group-1 Group-2 Group-3 Group-4
Evans Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Troy
WagnerM.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Example – continued …
 Another way of storing without wasting space is
shown in the right side.
 Even this model has certain drawbacks.
 The list must be traversed from the beginning in
order to recognize a particular group.
 To correct these issues, we have the concept of
DYNAMIC MEMORY.
 Dynamic memory is a area in memory that is
managed automatically.
 We can request for and release storage as
required.
Evans
Harris
Lewis
Shaw
$$$
Conrad
…
Wagner
$$$
Davis
Segal
$$$
Baker
…
Reed
$$$
Group-1
Group-2
Group-3
Group-4
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
POINTER ARRAYS
 The space efficient data structure can
be easily modified so that the individual
groups can be indexed.
 This is done by using a pointer array,
which contains the locations of the
different groups, or say the locations of
the first element in that group.
Evans
Harris
Lewis
Shaw
Conrad
.
.
.
Wagner
Davis
Segal
Baker
.
.
.
Reed
$$$
1
5
14
16
22
Evans
Harris
Lewis
Shaw
Conrad
.
.
.
Wagner
Davis
Segal
Baker
.
.
.
Reed
$$$
GROUP
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
RECORDS; RECORD STRUCTURES
 Collection of data are frequently organized into a hierarchy of field, records and files.
 A record is a collection of related data items, each of which is called a field or attribute.
 A file is a collection of similar records.
 Each data item itself may be a group item composed of sub-items.
 The items which are indecomposable are called elementary items or atoms or scalars.
 The name given to various data items are called identifiers.
 Record differs from a linear array in the follow ways:
1. A record may be a collection of nonhomogeneous data.
2. The data items in a record are indexed by attribute names.
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar
Example
 A hospital maintains a record
on each new born baby that
contains the following data
items: Name, Sex, Birthday
(Day, Month, Year), Father
(Name, Age), Mother (Name,
Age).
 The above structure can be
described as mentioned:
 The number in the left of
each identifier is called level
number.
 The subitems have greater
level number than the above
items.
1 New-born
2 Name
2 Sex
2 Birthday
3 Day
3 Month
3 Year
2 Father
3 Name
3 Age
2 Mother
3 Name
3 Age
M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College
for Women, Virudhunagar

More Related Content

What's hot (20)

First and follow set
First and follow setFirst and follow set
First and follow set
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Linked List
Linked ListLinked List
Linked List
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Heaps
HeapsHeaps
Heaps
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Data structures
Data structuresData structures
Data structures
 
Linked lists
Linked listsLinked lists
Linked lists
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 

Similar to Data Structures Chapter-4

chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdfstudy material
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxDEEPAK948083
 
ArrayBasics.ppt
ArrayBasics.pptArrayBasics.ppt
ArrayBasics.pptRaselAzam1
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyMalikireddy Bramhananda Reddy
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
SETS USING VENN DIAGRAMS
SETS USING VENN DIAGRAMSSETS USING VENN DIAGRAMS
SETS USING VENN DIAGRAMSSajid Rehman
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARISivaSankari36
 
8074.pdf
8074.pdf8074.pdf
8074.pdfBAna36
 
Tutorial on EM algorithm – Part 1
Tutorial on EM algorithm – Part 1Tutorial on EM algorithm – Part 1
Tutorial on EM algorithm – Part 1Loc Nguyen
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 

Similar to Data Structures Chapter-4 (20)

arrays in data structure.pptx
arrays in data structure.pptxarrays in data structure.pptx
arrays in data structure.pptx
 
Maths
MathsMaths
Maths
 
Data Structures Chapter-2
Data Structures Chapter-2Data Structures Chapter-2
Data Structures Chapter-2
 
chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdf
 
1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
ArrayBasics.ppt
ArrayBasics.pptArrayBasics.ppt
ArrayBasics.ppt
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
PROJECT
PROJECTPROJECT
PROJECT
 
SETS USING VENN DIAGRAMS
SETS USING VENN DIAGRAMSSETS USING VENN DIAGRAMS
SETS USING VENN DIAGRAMS
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARI
 
Fea 2 mark
Fea 2 markFea 2 mark
Fea 2 mark
 
8074.pdf
8074.pdf8074.pdf
8074.pdf
 
Tutorial on EM algorithm – Part 1
Tutorial on EM algorithm – Part 1Tutorial on EM algorithm – Part 1
Tutorial on EM algorithm – Part 1
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Data Structures Chapter-4

  • 3. Data Structures Linear Linear relationship between the elements by means of sequential memory locations. Linear relationship between the elements by means of pointers or links Non-Linear Eg. Trees, Graphs 2 ways of representing linear structures Arrays Linked List M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 4. Operations performed on any Linear structure  Traversal – Processing each element in the list.  Search – Finding the location of the element with a given value or key.  Insertion – Adding a new element to the list.  Deletion – Removing an element from the list.  Sorting – Arranging the elements in some type of order.  Merging – Combining 2 lists into a single list. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 5. When ARRAYS are preferred over Linked list?  Since arrays are easy to Traverse, Search & Sort, they are frequently used to store relatively permanent collections of data. When ARRAYS may not be useful?  If the size of the structure and the data in the structure are constantly changing, then the array may not be as useful as the linked list. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 7. Linear Arrays  A Linear Arrays is a list of a finite number of n of homogenous data elements, such that,  The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.  The elements of the array are stored respectively in successive memory locations.  The number of elements (n) is called the length or size of the array.  The index set consists of the integers 1, 2, . . . N. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 8. Linear Arrays – Continued . . .  The length or the number of data elements of the array can be obtained from the index set by the formula Length = UB – LB + 1 where UB is the largest index, called the upper bound and LB is the smallest index called the lower bound of the array.  Index  No.of Elements = 21 – 12 + 1 = 10 12 13 14 15 16 17 18 19 20 21 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 9. Linear Arrays  The elements of the array A may be denoted by the subscript notation  A1, A2, A3, . . . , An  Or by the bracket notation  A[1], A[2], A[3], . . . , A[n].  The number K in A[K] is called the subscript or an index, and A[K] is called a subscripted variable.  Subscripts allow any element of A to be referenced by its relative position in A. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 10. Linear Arrays  Where 0 is considered to be the lowest subscript, the highest subscript is (size-1).  Arrays can also be processed using FOR Loop.  Among all the operators , the subscript of the array must have the highest precedence.  Any declaration of array must give the 3 items of information: 1. The name of the array 2. The data type of the array 3. The index set of the array M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 12. 4.4 REPRESENTATION OF LINEAR ARRAYS IN MEMORY  Let X be a linear array in the memory of the computer.  The memory of the computer is simply a sequence of addressed locations as given below. X[1] X[2] X[3] X[4] . . . . 1000 1001 1002 1003 . . . .  Location( X[K] ) = Address of the element X[K].  The elements of X are stored in successive memory locations.  The computer does not need to keep track of the address of every element of X.  But needs to keep track only of the address of the first element of X, called the base address of X denoted by:  Base(X) M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 13. 4.4 REPRESENTATION OF LINEAR ARRAYS IN MEMORY  Using the base address, base(x), the computer calculates the address of any element of X by the following formula:  Location(X[k]) = Base(X) + w(K – lower_bound) X[1] X[2] X[3] X[4] . . . . 1000 1001 1002 1003 . . . .  Where w is the number of words per memory cell for the array.  the time to calculate X[k] is essentially the same for any value of k.  Given any subscript k, one can locate and access the content of x[k] without scanning any other element of x. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 15. 4.5 Traversing Linear Arrays  Let A be a collection of data elements stored in the memory of the computer.  Suppose we want to print the contents of each element of A or we want to count the number of elements with the given property.  This can be accomplished by traversing A that is by accessing and processing each element of A exactly once. 1. [ Initialize counter ] Set K := LB. 2. Repeat step-3 and 4 while K <= UB. 3. [ Visit Element ] Apply PROCESS to A[k]. 4. [ Increase Counter ] Set K := K + 1. [ End of Step 2 Loop ] 5. Exit M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 17. Inserting  Inserting refers to a operation of adding another element to the collection A.  Inserting an element at the “end” of a linear array can be easily done provided the memory locations allocated for the array is large enough to accommodate the adding element.  Inserting an element in the middle of the array requires, part of (half of) the elements must be moved downward to new locations to accommodate the new element and keep the order of the other elements. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 18. Inserting NAME NAME NAME NAME 1 Brown 1 Brown 1 Brown 1 Brown 2 Davis 2 Davis 2 Davis 2 Davis 3 John 3 3 3 Ford 4 Smith 4 John 4 John 4 John 5 Wagner 5 Smith 5 Smith 5 Smith 6 6 Wagner 6 Wagner 6 Wagner 7 7 7 7 8 8 8 8 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 19. Algorithm for Inserting into a Linear Array  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 the K-th position in LA. 1. [ Initialize Counter ] Set J := N. 2. Repeat Step 3 and 4 while J >= K. 3. [ Move j-th element downward] Set LA[ J+1 ] := LA[ J ] 4. [ Decrease counter ] Set J := J – 1 5. [ Insert Element ] Set LA[ K ] := Item 6. [ Reset N ] Set N := N +1 7. Exit M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 20. Deleting  Deleting refers to the operation of removing one of the elements from A.  Deleting an element at the “end” of an array presents no difficulties.  Deleting an element in the middle of the array requires each subsequent elements need to be moved one location upward in the order to “fill up” the array gap. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 21. Deleting NAME NAME NAME NAME 1 Brown 1 Brown 1 Brown 1 Brown 2 Davis 2 Davis 2 2 Ford 3 Ford 3 Ford 3 Ford 3 John 4 John 4 John 4 John 4 Smith 5 Smith 5 Smith 5 Smith 5 Wagner 6 Wagner 6 Wagner 6 Wagner 6 7 7 7 7 8 8 8 8 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 22. Algorithm for Deleting from a Linear Array  Here LA is a linear array with N elements and K is a positive integer such that K <= N.  This algorithm deletes the K-th element from 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 Loop ] 5. [ Reset N ] Set N := N -1 6. Exit M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 24. Multidimensional Arrays  Arrays where elements are referenced by 2 and 3 subscripts are called as two-dimensional and three-dimensional arrays.  Some programming languages allow more than 2 subscripts also.  These are called as multi-dimensional arrays.  The length of the dimension is the number of integers in its index set.  The pair of lengths m x n is called the size of the array. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 25. Two-Dimensional Arrays  A two0dimensional array is a collection pf m.n elements such that each element is specified by a pair of integers(such as J,K), called subscripts with the property that 1<= J <= m and 1<= K <= n.  The element of A with first subscript j and second subscript k will be denoted by Aj,k or A[ j , k ].  Two dimensional arrays are called matrices in mathematics and tables in business applications.  A two-dimensional m x n array A, can be drawn as a rectangular array with m rows and n columns and the element A[J,K] appears in row J and column K. Columns 1 2 3 4 Row 1 A[1,1] A[1,2] A[1,3] A[1,4] 2 A[2,1] A[2,2] A[2,3] A[2,4] 3 A[3,1] A[3,2] A[3,3] A[3,4] M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 26. Storage Representations Row-major Representation  We can consider a two dimensional array since it has elements with a single dimension.  As a result of this, a two-dimensional array can be assumed as a single column with many rows and mapped sequentially.  This is called a row-major representation. A[1,1] A[1,2] A[1,3] A[1,4] A[2,1] A[2,2] A[2,3] A[2,4] A[3,1] A[3,2] A[3,3] A[3,4] A[1,1] A[1,2] A[1,3] A[1,4] A[2,1] A[2,2] A[2,3] A[2,4] A[3,1] A[3,2] A[3,3] A[3,4]M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 27. Row-major Representation  We can calculate the address of the element of the m-th row and n-th column in a two dimensional array using the formula shown below:  addr[(a[m,n]) = (Total number of rows present before the m-th row * size of a row ) + (total number of elements present before the n-th element in the m-th row * size of an element)  Addr(a[m,n]) = ((m-lb1)*(ub2-lb2+1)*size) + ((n-lb2)*size)  Size of a row = Total no.of elements in a row * size of an element M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 28. Column-major Representation  We can also represent a two-dimensional array as one single row of columns and map it subsequently.  This is called a column-major representation. A[1,1] A[1,2] A[1,3] A[1,4] A[2,1] A[2,2] A[2,3] A[2,4] A[3,1] A[3,2] A[3,3] A[3,4] A[1,1] A[2,1] A[3,1] A[1,2] A[2,2] A[3,2] A[1,3] A[2,3] A[3,3] A[1,4] A[2,4] A[3,4] M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 29. Representation of 2-dimensional Arrays in Memory  The programming languages will store two dimensional arrays either column-by-column (column major order) or row-by-row (row major order).  The computer keeps track of Base(A) – the address of the first element A[1,1] of A.  It computes the address of A[J,K], where A is m x n array, using the formula ❖ LOC(A[J,K]) = Base(A) + w [ M(k-1) + (J-1) ], if column major order is used. ❖ LOC(A[J,K]) = Base(A) + w [ N(J-1) + (K-1) ], if row major order is followed.  Here w denotes the number of words per memory location for the array A. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 30. General Multi-dimensional Arrays  An n dimensional m1 x m2 x . . . X mn array B is a collection of m1.m2. … mn data elements in which each element is specified by a list of n integers such as K1, K2, … , Kn called subscripts, with the property that 1<=K1<=m1, 1<=K2<=m2, … 1<=Kn<=mn.  The element of B with subscripts K1, K2, … , Kn will be denoted by Bk1,k2,…,kn or B[k1,k2,…,kn] M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 31. A Three Dimensional Array A[1,1] A[1,2] A[1,3] A[2,2] 4, 3, 1 4, 3, 2 4, 3, 3 4, 3, 4 4, 3, 5 Row-1 Row-2 Row-3 Row-4 Row-5 Row-6 Col-1 Col-2 Col-3 Col-4 Col-5 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 32. Representation of Polynomials using Arrays  We need to evaluate polynomial expressions and perform basic arithmetic operations like addition, multiplication on them.  We have a method to represent a polynomial of degree ‘n’, by storing the coefficient of (n+1) terms of a polynomial in a array.  An expression x + 4 x2 – 7 x5 is a polynomial of degree 5.  All the elements of an array has 2 values namely coefficient and exponent.  We also assume that the exponent of each successive term is less than that of the previous term.  A single dimensional array is used to store a single variable polynomial. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 33. Polynomial representation using 1-d array  3 x4 + 5 x3 + 6 x2 + 10 x – 14 is stored as below: index Value Meaning 0 -14 – 14 1 10 10 x 2 6 6 x2 3 5 5 x3 4 3 3 x4 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 34. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Set ‘i ‘ to point to first term in A Set ‘j ‘ to point to first term in B Set ‘k ‘ to point to first term in C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 35. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 36. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Increment ‘i ‘ to point to next term in A Increment ‘j ‘ to point to nextt term in B Set ‘k ‘ to point to next term in C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 37. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 38. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Increment ‘i ‘ to point to next term in A Increment ‘j ‘ to point to nextt term in B Set ‘k ‘ to point to next term in C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 39. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 40. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Increment ‘i ‘ to point to next term in A Increment ‘j ‘ to point to nextt term in B Set ‘k ‘ to point to next term in C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 41. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 5 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 42. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 5 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Increment ‘i ‘ to point to next term in A Increment ‘j ‘ to point to nextt term in B Set ‘k ‘ to point to next term in C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 43. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 5 3 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C Polynomial B is over M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 44. Polynomial addition 0 1 4 0 0 -7 -14 10 6 5 3 -14 11 10 5 3 -7 -7 x5 + 4 x2 + x 3 x4 + 5 x3 + 6 x2 + 10 x - 14 i j k Add the terms pointed by I & j into C Copy remaining A terms into C M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 45. 4.12 POINTERS & POINTER ARRAYS  A variable P is called a pointer if P “points” to an element, i.e if P contains the address of that element.  An array PTR is called a pointer array if each element of PTR is a pointer. 2002 Data element Address=2002P M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 46. Example  Consider an organization which divides its members into 4 groups, where each group contains an alphabetized list of members living in a certain area. There are 21 people totally, divided into 4 groups of 4, 9, 2 and 6 people. Group-1 Group-2 Group-3 Group-4 Evans Conrad Davis Baker Harris Felt Segal Cooper Lewis Glass Ford Shaw Hill Gray King Jones Penn Reed Silver Troy WagnerM.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 47. Example – continued …  Here, the data can be stored into a 2-dimensional array either in row-major or column-major order.  The drawback here is the wastage of memory locations. (show in green shades) Group-1 Group-2 Group-3 Group-4 Evans Conrad Davis Baker Harris Felt Segal Cooper Lewis Glass Ford Shaw Hill Gray King Jones Penn Reed Silver Troy WagnerM.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 48. Example – continued …  Another way of storing without wasting space is shown in the right side.  Even this model has certain drawbacks.  The list must be traversed from the beginning in order to recognize a particular group.  To correct these issues, we have the concept of DYNAMIC MEMORY.  Dynamic memory is a area in memory that is managed automatically.  We can request for and release storage as required. Evans Harris Lewis Shaw $$$ Conrad … Wagner $$$ Davis Segal $$$ Baker … Reed $$$ Group-1 Group-2 Group-3 Group-4 M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 49. POINTER ARRAYS  The space efficient data structure can be easily modified so that the individual groups can be indexed.  This is done by using a pointer array, which contains the locations of the different groups, or say the locations of the first element in that group. Evans Harris Lewis Shaw Conrad . . . Wagner Davis Segal Baker . . . Reed $$$ 1 5 14 16 22 Evans Harris Lewis Shaw Conrad . . . Wagner Davis Segal Baker . . . Reed $$$ GROUP M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 50. RECORDS; RECORD STRUCTURES  Collection of data are frequently organized into a hierarchy of field, records and files.  A record is a collection of related data items, each of which is called a field or attribute.  A file is a collection of similar records.  Each data item itself may be a group item composed of sub-items.  The items which are indecomposable are called elementary items or atoms or scalars.  The name given to various data items are called identifiers.  Record differs from a linear array in the follow ways: 1. A record may be a collection of nonhomogeneous data. 2. The data items in a record are indexed by attribute names. M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar
  • 51. Example  A hospital maintains a record on each new born baby that contains the following data items: Name, Sex, Birthday (Day, Month, Year), Father (Name, Age), Mother (Name, Age).  The above structure can be described as mentioned:  The number in the left of each identifier is called level number.  The subitems have greater level number than the above items. 1 New-born 2 Name 2 Sex 2 Birthday 3 Day 3 Month 3 Year 2 Father 3 Name 3 Age 2 Mother 3 Name 3 Age M.Priyavani,MCA,DCHN,M.Phil, V.V.Vanniaperumal College for Women, Virudhunagar