This document discusses arrays, records, and pointers. It begins by defining linear and non-linear data structures. Linear data structures have elements stored in sequential memory locations or linked by pointers. Arrays and linked lists are two ways to represent linear structures. Common operations on linear structures are traversal, search, insertion, deletion, sorting, and merging. Arrays are preferred over linked lists when the data needs to be traversed, searched, or sorted frequently. The document then discusses linear arrays in more detail, including representation in memory, traversing arrays, inserting and deleting elements, and multidimensional arrays.
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
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