Introduction to
Data Structure
Unit 1
What is Data?
• Data represent single value or set of value assigned to entities.
• Eg: 12 , arun
• Data have very l i ttle meaning until they are processed.
• The result of data processing is called information
• Eg: Arun is a student, whose age is 12
• The data should be arranged in systematic way so i t gets s t r u c t u r e b e c o m e a meaningful.
Data Data Data
Processing
Information
What is Data Structure
• A Data Structure is a data organization and storage format that enables efficient
access and modification. Data Structure determines the arrangement of data in a
computer' s memory. Basically, data structure is a method to store data in a
structured way so that it can be created, accessed and managed efficiently.
• It is a particular way of storing and organizing data on computer so that it can be used efficiently
• They are building blocks of a program. A program built using improper data structures
may not work as expected. So as a programmer it is mandatory to choose most
appropriate data structures for a program
• General data structure types include array, l ist , queue, tree etc.
Characteristics
The following are the f ive characteristics of data structures.
• Has a set of operations that can be performed on data i tems, such as insertion,
deletion, searching, etc.
• Describes the rules of how the data i tems are related to each other.
• Implements an interface to provides the l ist of supported operations.
• Organizes i ts elements such that the memory usage and the execution t ime of
operations of data structure is as small as possible.
Data Structure Vs. Data Type
DATA TYPES DATA STRUCTURES
• A data type specifies the kind of data a variable can • Data Structure is the collection of
hold, such as integers, floating-point numbers, different kinds of data.
characters, or strings.
Implem en t at i o n through Data Structures is
• I m p le menta t io n i s a f o r m o f a b s t ra ct called concrete implementation
im p le m e n t a t io n.
• Can hold different kind and types of data
• C a n h o ld va lu es a n d n o t d a t a , s o i t i s d a t a
within one single object
le s s
Data Structure Vs. Data Type
DATA TYPES DATA STRUCTURES
• Values can directly be assigned • The data is assigned to the data
to the data type variables structure object using some set
of algorithms and operations
l ike push, pop and so on.
• No problem of t ime complexity
• Time complexity comes into play
when working with data
• Examples: int, float, double,
structures
char
• Examples: stacks, queues, tree
Operations on data structure
Operation means processing the data in the data structure.
The following some important operations are
Traversing : To visit or process each data exactly once in the data structure
Searching : To search for a particular value in the data structure for a given key
Inserting : To add a new value to the data structure.
Deleting :To remove a value from the data structure in a particular order
Merging : To join two same type data structure.
Abstract data type (ADT)
An abstract data type an abstraction of data structure that provides only the interface to
which the data structure must adhere
An ADT composed of
Collection of data
A set of operation on that data
Implementation include choosing a perticular data structure to store the data item and
algorithms for basic operations.
Classification
Data
Structures
Non -
Primitive
Primitive
Non –
int char float double pointer Linear
linear
list array stack queue trees graphs
Primitive & Non-Primitive Data Structures
PRIMITIVE DATA NON- PRIMITIVE DATA
STRUCTURES STRUCTURES
• a r e t h e f u n da menta l d a t a t y pes w h i c h a r e su • Created using primitive data structure.
p po r ted b y p r o g ra m ming
la n g ua ges. • A r ray s , St r u c t ur es, U n io n s, L in ked l i s t s , St a c k s , Q u
e ues, Tr e e s , G ra p h s
• E x a m ples : I n t e ger d a t a t y pe,
C h a ra c t er d a t a t y p e, F lo a t in g p o in t d a t a
type
Linear and Non-linear Structures
LINEAR STRUCTURES NON-LINEAR STRUCTURES
This data structure involve representing
This data structure involve
the elements in hierarchical order
arranging the element in linear
fashion.
Eg Tree
Eg Array
Graph
Stack
Queue
List
ARRAYS
• An array is a list of finite number of “n” homogenous data elements ( ie, data elements of same type)
• Elements of an array referenced by index
• Elements of array stored at continuous memory location
• This makes it easier to calculate the position of each element by simply adding offset to base vale.
• The location in memory of the first byte in the array called base address of the array(base)
• Location of next index depends on the data type we use.
• An array is a collection of similar data elements which are stored in consecutive memory
locations, where each memory location stores one f ixed -length data i tem.
• The number “n” of elements is called the length or size of the array.
• Length =UB-LB+1 (UB-upper bound ,,,,, LB-lower bound)
Array of 10 Integers Array of 10 Characters
a[ 10] b[ 10 ]
0 1 2 3 4 5 6 7 8 9 1
0 1 2 3 4 5 6 7 8 9
0
5 6 4 3 7 8 9 2 1 2
s a f a c o l l e g e
Types of Array
1) One dimensional array or single dimensional array
2) Two dimensional array or matrix
3) Multi-dimensional array
4) Sparse matrix
1-diamentional array
10 20 30 40
1 2 3 4 index
2-dimensional array
Two dimensional array is also called matrix
Representation of array in memory.
Two possible arrangements
1. Row major order
2. Column major order
Row major order
Element of an array arranged sequentially row by row by row. This elements of
first rows occupies first set of memory location reserved for array elements of
second row occupies the next set of memory and so on.
1 2 3
4 5 6
1 2 3 4 5 6
(0,0) (0,1), (0,2), (1,0), (1,1) ,(1,2)
Column major order
Reverse of row major order
1 4 2 5 3 6
Multi-dimensional array
It represent M1,M2,M3…….Mn of array B
Two or more dimensional array is called multi-dimensional array.
Sparse matrix
If most of the element of the matrix have zero value, then it is called sparse matrix.
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Representing sparse matrix by two dimensional array leads to wastage of lot of memory as zeros in the matrix are no use in
most [Link] instead of storing zero with non-zero elements. This means storing non zero elements with triples
(Row,Column,value)
Two Common representations
1. Array representation
2. Linked list representation
Using Array representation
Row= index of row
Column = index of column
Value=value of non zero element
Using linked list
In linked list each node has 4 fields
Row =index of row
Column=index of column
Value=value of non zero element
Next node= Address of next node
Limitations
• Arrays are of fixed size.
• Data elements are stored in contiguous
memory locations which may not be always
available.
• Insertion and deletion of elements can be problematic
because of shif t ing of elements from their positions.