DS - Unit 1
DS - Unit 1
Unit-I: Topics
Introduction to data structures - Definition, Types of Data Structure
Primitive and non-primitive data structures and operations on them
Applications and advantages of Data structure
Introduction to arrays
Terminology: Size, Type, Base, Index, Range of Indices
One dimensional arrays – declaration, initialization and accessing one dimensional array
elements
Memory allocation and address calculation of one dimensional array
Two dimensional arrays – declaration, initialization and accessing two dimensional array
elements
Representation of arrays in memory : row-major and column-major Order, Address
calculation of elements of two-dimensional arrays
Applications of Array
References:
1) D. Samanta - Classis Data Structures, 2nd Edition – PHI Publication
2) G. S. Baluja - Data Structures through C, 4th Edition – Dhanpat Rai & Co.
INTRODUCTION
There are a number of ways to write programs: they must run correctly and efficiently; be easy to
read and understand; be easy to debug and; be easy to modify.
Data is the basic fact or entity that is utilized in calculation or manipulation. There are two different
types of data such as numerical data and alphanumeric data. These two data type specify the nature of
data item that go through certain operations. Integers and floating point numbers are of numerical data type.
Data may be a single value or it may be a set of values. Whether it is a single value or a group of values to
be processed must be organized in a particular fashion. This organization leads to structuring of data.
The intimate relationship between data and programs can be traced to the beginning of computing. In
any area of application, the input data, (internally stored data) and output data may each have a unique
structure.
Definitions
Data structure is representation of the logical relationship existing between individual elements of
data.
OR
In other words, a data structure is a way of organizing all data items that considers not only the
elements stored but also their relationship to each other.
OR
We can also define it as a mathematical or logical model of particular organization of dataitems.
Data structure mainly specifies the following four things:
1. Organization of data.
2. Accessing methods.
3. Degree of associativity.
4. Processing alternatives for information.
Data structures are the building blocks of a program. And hence the selection of a particular data structure
Page 1 of 16
stresses on the following two things.
1. The data structures must be rich enough in structure to reflect the relationship existing between
the data.
2. And the structure should be simple so that we can process data effectively whenever required.
The identification of the inherent data structure is vital in nature. And the structure of input and output data
can be use to derive the structure of a program. Data structure affects the design of both structural and
functional aspects of a program.
Algorithm + Data structure = Program
An algorithm is a step-by-step procedure to solve a particular function. That is, it is a set of instructions
written to carry out certain tasks and the data structure is the way of organizing the data with their logical
relationship retained.
To develop a program of an algorithm, we should select an appropriate data structure for that algorithm.
Therefore, algorithm and its associated data structures form (creates) a program.
Page 2 of 16
There are two ways to represent a linear data structure in memory,
o Static memory allocation
o Dynamic memory allocation
The possible operations on the linear data structure are: Traversal, Insertion, Deletion,
Searching, Sorting and Merging.
Examples of Linear Data Structure are Stack and Queue.
Stack: Stack is a data structure in which insertion and deletion operations are performed at one end
only.
o The insertion operation is referred to as ‘PUSH’ and deletion operation is referred to as ‘POP’
operation.
o Stack is also called as Last in First out (LIFO) data structure.
Queue: The data structure which permits the insertion at one end and Deletion at another
end, known as Queue.
o End at which deletion is occurs is known as FRONT end and another end at whichinsertion
occurs is known as REAR end.
o Queue is also called as First in First out (FIFO) data structure.
Page 3 of 16
Difference between Primitive and Non-Primitive Data Structure
Primitive data are only single values, they have not The non-primitive data types are used to
special capabilities. store the group of values.
The examples of Primitive data types are givenbyte, Examples of non-primitive data type are
short, int, long, float, double, char etc. Array, structure, union, link list, stacks,
queue etc.
1. CREATE: This operation results in reserving memory for the program elements (variables). This can
be done by declaration statement. The creation of data structure may take place either during compile-
time or during run-time.
2. DESTROY: This operation destroys the memory space allocated for the specified data structure.
malloc( ) and free( ) function of C language are used for these two operations respectively (as far as
dynamic memory allocation and de-allocation is concerned).
3. SELECTION: This operation deals with accessing a particular data within a data structure.
4. UPDATE: This operation updates or modifies the data in the data structure. Probably new data
may be entered or previously data may be deleted.
5. SEARCHING: This operation finds the presence of the desired data item in the list of dataitem. It
may also find the locations of all elements that satisfy certain conditions.
6. SORTING: This is the process of arranging all data items in a data structure in a particular order
say for example, either in ascending order or in descending order.
7. MERGING: This is a process of combining the data items of two different sorted lists into asingle
sorted list.
Page 4 of 16
Applications of Tree: Representation of data lists, Quickly accessible data storage,
Representation of hierarchal data, Routing of algorithms
Applications of Files: Implementation of computer programs, Data comparison, Storage of data
having varying data types
INTRODUCTION TO ARRAYS
If we want to store a group of data together in one place, then array is suitable data structure. This
data structure enables us to arrange more than one element that’s why it is known as composite data
structure.
In this data structure, all the elements are stored in contiguous location of memory. Figure shows an
array of data stored in a memory block starting at location 101.
101
102
103
104
105
106
107
Definition:
An array is a finite, ordered and collection of homogeneous data elements.
Array is ordered because all the elements are stored one by one in contiguous location of
computer memory in a linear ordered fashion.
Array is collection of homogeneous (identical) elements because all the elements of anarray are of
the same data type only.
Page 5 of 16
An array is known as linear data structure because all elements of the array are stored in a linear
order.
Terminology:
1. Size: Number of elements in an array is called the size of the array. It is also known as
dimension or length.
2. Type: Type of an array represents the kind of data type for which it is meant
(designed). E.g. array of integer, array of character.
3. Base: Base of an array is the address of memory location where the first element in the array is
located.
E.g. 101 is the base address of the array mentioned in Figure.
4. Index: All the elements in an array can be referred by a subscript like Ai or A[i], this
subscript is known as index. An index is always an integer value.
5. Range of indices: Indices of an array element may change from lower bound (L) to an upper
bound (U). These bounds are called the boundaries of an array.
If the rage of index varies from L...U then size of the array can be calculated as:
Size (A) = U – L + 1
A one dimensional array is one in which only one subscript specification is needed to specify a
particular element of the array.
e.g. int n[5]; This array will store seven integer values, which is specified by n. It can be visualized as
below.
Index Data
n[0] 5
n[1] 48
N[2] 0
n[3] 80
n[4] 21
Page 6 of 16
Initializing one dimensional array
These initializing expressions must be constant values. Expression with identifiers or function calls may
not be used in the identifiers. The initializers are specified within braces and separated by commas.
String initializers may be written as string constants instead of character constants withinbraces.
Individual elements of the array can be accessed using the following syntax:
array_name [ index or subscript ];
Example:
The subscript for third element is 2 because the lower bound of array in C is 0.
This statement reads a value from the keyboard and assigns it to third location of the array.
Suppose an array A[100] is to be stored in a memory as in Figure. Let the memory location where the
first element can be stored is M.
1 M
2
3
4
5
.
.
i
.
100
An array can be written as A[L..U] where L denotes lower bound and U denotes the upper bound for
index.
Page 7 of 16
TO FIND ADDRESS OF AN ELEMENT IN ONE DIMENSIONAL ARRAY WITH LOWER
BOUND = 1
If each element requires one word (byte) then the location for any element say A[ i ] in the array can be
obtained as:
If array is stored starting from memory location M and for each element it requires w numberof words
then the address for A[ i ] will be
Address ( A[ i ]) = M + ( i – L ) * w
Here Lower bound of array can be any arbitrary value denoted by L.
Above formula is known as indexing formula, which is used to map the logical
presentation of an array to physical presentation.
By knowing the starting address of array M, the location of the ith element can be
calculated instead of moving towards i from M.
Page 8 of 16
Example
Suppose that each element requires 2 word (byte), the base address of the array a[10] is 100and the lower
bound of the array is 1. Find out the address of following elements:
1. Find address of a[4].
2. Find address of a[7].
Answer
We are given that: Base address of the array is 100. So M=100
Lower bound of the array is 1. So L=1.
Each element requires 2 word (byte). So w=2.
Definition:
A Two-Dimensional array (commonly called a matrix) consists of elements of the same type arranged in
rows and columns. The rows and columns of a matrix are numbered starting from 0. Thus, for an array of
size rows x cols, the row subscripts are in the range 0 to rows - 1 and the column subscripts are in the range
0 to cols - 1.
mxn
The subscripts of any arbitrary element say Aij represent ith row and jth column. Declaration
Page 9 of 16
number of array elements. For a two-dimensional array, the memory required is given as rows* cols*
sizeof(arr_type).
A matrix can be initialized using a syntax similar to that used for the initialization of vectors. Thus, the
declaration
int mat [3] [4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
C language allows the initialization list for each row to be enclosed within a pair of braces. Thus, the
declaration of array mat given above can alternatively be written as follows:
int mat[3] [4] = {{l, 2, 3, 4), {5, 6, 7, 8), {9, 10, 11, 12}};
An element of a matrix can be accessed by writing the array name followed by row and column subscript
expressions within separate array subscript operators as shown below.
var_ name [ row_expr ] [ col_expr ]
where row_ expr and col_ expr are integral expressions that specify the row and column positions,
respectively, of an element in the array. As mentioned earlier, for an element access to be valid, the values
of row_ expr should be in the range 0 to rows - 1 and the values of col_ expr should be in the range 0
to cols – 1.
Example:
Like one dimensional array, matrices are also stored in continuous memory location. There are two
conventions of storing any matrix in memory:
In row major order, elements of matrix are stored on a row by row basis. i.e. all the elements of
the first row, then all the elements of second row and so on.
In column major order, elements of matrix are stored column by column. i.e. all the elements of the
first column are stored in their order of rows, then in second column and so on.
Page 10 of 16
Reference of elements in a matrix
Logically a matrix appears as two dimensional but physically it is stored in a linear fashion. So in
order to map from logical view to physical structure, we need indexing formula. Obviously, the
indexing formula for different order will be different.
Storing all the elements in first ( i -1 )th rows + Number of elements in the ith row upto the jth column” i.e.
Address ( Aij ) = ( i – 1 ) * n + j
So for the matrix A3X4, the location of A32 will be calculated as 10.
Note: This formula assumes that lower bound for i (row) and j (column) will be 1.
Assume that the base address is 1 (the first location of the memory). So the address of Aij will beobtained as-
Storing all the elements in first ( j - 1)th columns + Number of elements in the jth columnupto the
ith row i.e. Address ( Aij ) = ( j – 1 ) * m + i
Note: This formula assumes that lower bound for i (row) and j (column) will be 1.
Example:
Assume that the base address of the two dimensional array A[3][3] is 100, each element requires 2 byte
(word). Find the address of the element A[3][2] using
1. Row major order
2. Column major order
Solution
We are given that: Base address of the array is 100. So M=100
Each element requires 2 word (byte). So w=2.
Number of rows are 3. So m=3.
Page 12 of 16
Number of columns are 3. So n=3.
Lower bound of i (row) L1 = 1
Lower bound of j (column) L2 = 1
1. Row major
(A[3][2] ) = M + ( ( i – L1 ) * n + j – L2 ) * w
= 100 + ( ( 3 – 1 ) * 3 + 2 – 1 ) * 2
= 114
2. Column major
APPLICATION OF ARRAYS
1. Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. Many databases, small and large, consist of (or include) one- dimensional arrays
whose elements are records.
2. Arrays are used to implement other data structures, such as heaps, hash tables, deques, queues,
stacks, strings, and VLists.
3. There are wide applications of arrays in computation. That is why almost every programming
language includes this data type as a built in data type.
4. Suppose you want to store records of all students in a class. The record structure is givenby
STUDENT
Roll No. Mark1 Mark2 Mark3 Total Grade
Alpha Numeric Numeric Numeric Numeric Numeric Character
If sequential storage of record is not objection, then we can store the records by maintaining 6 array
whose size is specified by the total number of students in the class as in Figure.
5. Arrays can be used to represent polynomials so that mathematical operations can be
performed in an efficient manner.
Page 13 of 16
QUESTION BANK
SHORT QUESTIONS
1) Define Data Structure.
2) What is Primitive Data structure and list out commonly used Primitive data structure.
3) What is Linear Data structure and list out linear data structure.
4) What is Non-Linear Data structure and list out Non-linear data structure.
5) Draw the diagram of classification of Data structure.
6) Differentiate Primitive data structure and Non-Primitive Data Structure.
7) Differentiate Linear data structure and Non Linear Data Structure.
8) List out application of Data Structure.
9) List out advantages of Data Structure.
10) Define Array with suitable example.
11) Define Array and explain its terminologies: Size, Type, Base, Index, Range of Indices (Any Two).
12) What is the upper bound and lower bound of an array?
13) Write the formula for address calculation of 1-D array element and explain it.
14) Write the formula for address calculation of 2-D array element and explain it.
15) List out applications of an array.
16) Suppose that each element requires 2 word (byte), the base address of the array a[10] is 250 and
lower bound of the array is 0. (Each of 2 Marks)
(a) Find the address of a[4].
(b) Find the address of a[7].
(c) Find the address of a[9].
LONG QUESTIONS
Page 15 of 16
(a) Row-major order
(b) Column-major order
11) Assume that the base address of the two dimensional array a[10][10] is 250, each
element requires 2 byte (word). Find the address of the element a[2][3] using
(a) Row-major order
(b) Column-major order
Page 16 of 16