Académique Documents
Professionnel Documents
Culture Documents
UNIT-I
INTRODUCTION
TO
DATA STRUCTURES
Operational Unit:
Register: temporary storage
present in CPU
aids in arithmetic calculations
stores operands and results
Storage Units:
permanent storage
store instructions and data
main memory, secondary memory
Main memory : semiconductor memory
has voltage representation
stored in flip-flops(2-state device)
Secondary : magnetic core memory
DATA STRUCTURE
the organization of data
and its storage allocations in a computer
CREATION
int a;
Creates memory space for integer variable a
DESTROY
Destructor function in C++ ; free( ) in C
Useful for efficient usage of memory
SELECT
To access data within data structure
UPDATE
int a=2;
To change data in the data structure
Linear DS: traverses the data elements sequentially, in which only one data
element can directly be reached.
Non-linear DS: Every data item is attached to several other data items in a way
that is specific for reflecting relationships. The data items are not arranged in a
sequential structure.
ARRAYS
DEFINITION
Is a finite and ordered collection of homogeneous data
elements
Finite: has limited no. of elements
Ordered: elements are stored one by one in contiguous
memory locations
Homogeneous: all elements are of the same data type
Example:
An array of integers to store age of students
An array of strings to store student names
TERMINOLOGY
SIZE no. of elements in an array
TYPE data type of elements
BASE address of memory location
where first element is stored
..
456
455
454
453
Base address
TERMINOLOGY.
INDEX all elements in an array are
referenced by a subscript
eg: a[i], where i=index
TERMINOLOGY.
Index of ith element is A[i] = L + i 1
To get index of 3rd element:
0+31=2
Size of array : U L + 1
To get the size of array A[5]
40+1=5
WORD size of an element
Word size varies from machine to machine
ONE-DIMENSIONAL ARRAY
If only one subscript or index is required
to reference all the elements in an array,
then the array is termed one-dimensional
array
MEMORY ALLOCATION
Physical representation of one-dimensional array
1000
A[0]
Address of A[1]=
=1000 + (1 0 ) * 4
=1004
.
.
1008
.
.
A[
Address ( A[i] ) = M + ( i L ) * w1]
1004
100
A[100]
MEMORY ALLOCATION .
Address mapping between logical and physical views of an array
Address ( A[i]) = M + (i L ) * w
where M=base address
L=lower bound
w=word length
OPERATIONS ON ARRAY
Traversing
Sorting
Searching
Insertion
Deletion
Merging
Algorithm..
Traversing
This operation is used to visit all the elements in the array
Algorithm TRAVERSE_ARRAY( )
Input: An array A with elements
Output: According to PROCESS ( )
Data structure: Array A [ L U ]
Steps:
1. i = L
2. While i <= U do
1. PROCESS A[i] //procedure to perform an action Eg: display element on
screen
2. i=i+1
3. EndWhile
4. Stop
Sorting
Steps:
1. i = U
2. While i >= L do
1. j= L
2. While j < i do
1. If ORDER(A[j], A[j+1])=FALSE // procedure to check if
elements are in order
1. SWAP (A[j],A[j+1])
2. EndIf
3. j=j+1
3. EndWhile
4. i=i-1
3. EndWhile
4. Stop
Searching
INSERTION.
element into the array
Insertion ..
Insertion
Deletion
Algorithm DELETE(KEY)
Input:KEY is the element to be deleted
Output: ArrayAwithout KEY element
Data Structure: Array A [ L . U ] // L=lower bound & U=upper bound of
A
Deletion
MERGING
Merging
Merging
APPLICATIONS OF
ARRAYS
To store students records:
maintains 6 arrays;
MULTI-DIMENSIONAL ARRAYS :
DIMENSIONAL ARRAYS
TWO-
Example:
Consider matrix A of order 3 x 4:
SPARSE MATRICES
A Sparse matrix is a two-dimensional array having
the value of majority elements as null
* denotes non-null values
Uses:
- Wastage of memory if entire matrix is stored
- So, store only the non-null values
- Helps to compress the matrix
Square Matrix:
A square matrix has the same
number of rows as columns .
Symmetric Matrix:
TRIANGULAR MATRICES
DIAGONAL MATRIX -
nonzero elements
only on the main diagonal, the first diagonal below this, and the
first diagonal above the main diagonal
EXAMPLE
:
POINTER ARRAYS
Pointer : holds address of memory variable or array
Pointer array: array containing pointers as elements
Example: Students marks record
Semester : 6 [S1, S2, S3, S4, S5, S6]
Has 5 subjects in each semester:
Sem 1: [S1-1, S1-2,S1-3,S1-4,S1-5]
Sem 2: [S2-1, S2-2,S2-3,S2-4,S2-5]
.
.
Sem 6: [S6-1, S6-2,S6-3,S6-4,S6-5]
No. of students:30
12MCA101
89
12MCA102
75
SEM
1
12MCA103
90
SEM 2
12MCA104
85
12MCA105
79
SEM3
SEM4
SEM5
SEM6
12MCA201
.
.
.
.
.
.
.
.
ARRAY AS ADT
[ABSTRACT DATA TYPE]
Consists of
Collection of data items
Basic operations performed on them
ARRAY AS ADT
[ABSTRACT DATA TYPE]
An abstract data type is a data type
defined in terms of a type and a set of
operations on that type. Each operation is
defined in terms of its input and output
without specifying how the data type is
implemented.
A stack is an example of an ADT, defined in
terms of push and pop operations.
A data structure is an implementation of
an ADT. The Stack ADT, for example, can be
implemented by means of an array.
Stack as ADT
Definition A stack is a restricted list in which entries are added and
removed
from the same end, called the top. This strategy is known as last-infirst-out
(LIFO) strategy.
Operations (methods) on stacks:
push (item)
pop ()
size ()
empty ()
full()
ontop()
the stack