2. What is Data Structure?
• The Logical or Mathematical model of a
particular organization of data.
• Data Structures may be divided into two
types:-
1. Linear Data Structures
2. Non Linear Data Structures
3. Linear Data Structures
• If the elements of a data structure are
stored sequentially. e.g.
1. Arrays
2. Linked Lists
3. Stacks
4. Queues
4. Non Linear Data Structures
• If the elements of a data structure are not
stored in a sequential order. e.g.
1. Trees
– Represent Hierarchical relationship between
nodes
2. Graphs
– Represent relationship between pairs of
elements which is not necessarily
hierarchical in nature
5. Data Structure Operations
1. Traversing
– Accessing each record exactly once so that
certain items in the record may be
processed
2. Searching
– Finding the location of the record with the
given key value
3. Inserting:- Adding a new record
4. Deleting:- Removing a record
6. Abstract Data Types
• A useful tool for specifying the logical
properties of a data type is the ADT
• It is the way we look at a data structure,
focusing on what it does and ignoring how
it does its job.
• Abstract means considered apart from
the detailed specification or
implementation
• Data Type is a collection of values and a
set of operations on those values
7. Abstract Data Types (contd..)
• ADT can be a structure considered without
regard to its implementation
OR
• It can be thought of as a description of the
data in the structure with a list of
operations that can be performed on the
data within the structure
• e.g. stack.h file has been provided for
performing the operations over stack
8. Specifying an ADT
• An ADT consists of two parts:
– a value definition and
– An operator definition
• The value definition defines the collection
of values for the ADT and consists of two
parts: 1. a definition clause 2. a condition
clause
• Each operator is defined as abstract
function with three parts: 1. a header 2.
optional preconditions 3. postconditions
9. Specifying an ADT (Example)
• To illustrate the concept of an ADT and our
specification method, consider the ADT
RATIONAL, corresponds to Rational Number.
/* value definition */
abstract typedef <integer, integer> RATIONAL;
Condition RATIONAL[1] <> 0;
/* operator definition */
abstract RATIONAL mult (a, b) /* written a*b */
RATIONAL a,b;
Postcondition mult[0] == a[0]*b[0];
mult[1] == a[1]*b[1];
Similarily, other operators definition come here
10. Static Implementation of Data
Structures
• Implementation of Data Structures at
compile time is known as Static
Implementation
• The memory to be allocated to the variable
is fixed and is determined by the Compiler
at the compile time.
• e.g. Array
int a[5];
• Five memory locations will be allotted
(each of 2 bytes for integer)
11. Problems of Static Implementation
1. There is no bound checking in C for
array boundaries.
2. If you store less number of elements
than the number of elements for which
you have declared memory, Then the
rest of the memory will be wasted
12. Dynamic Implementation of Data
Structures
• Implementation of Data Structures at
Run time is known as Dynamic
Implementation
• Problems of Arrays can be removed here
• We need not to know in advance, the
size of memory required
• C provides following functions:
1. malloc( )
2. calloc( )
3. free( )
4. realloc( )
13. Dynamic Implementation of Data
Structures (contd..)
1. malloc( ): It allocates a block of memory
in bytes. It acts as a request to RAM for
allocation of memory. If request is
granted then it returns a pointer to the
first block of that memory. The syntax is:
malloc(number of elements * size of each element);
e.g.
int *ptr;
ptr = malloc(10 * sizeof (int) );
14. Dynamic Implementation of Data
Structures (contd..)
2. calloc( ): This function works exactly similar to
malloc( ) function except for the fact that it
needs two arguments as against one argument
by malloc( )
e.g.
int *ptr;
ptr = (int *) calloc (10,2);
• (int *) is the casting operator that will change
the void to int type pointer.
• Memory allocated by malloc( ) contains
Garbage value, Whereas, Memory allocated
by calloc( ) contains all zeros.
15. Dynamic Implementation of Data
Structures (contd..)
3. Free( ): It is used to de-allocate the
previously allocated memory using
malloc( ) or calloc( ) functions. The
syntax is:
free ( ptr_var);
16. Dynamic Implementation of Data
Structures (contd..)
4. realloc( ): It is used to resize the size of
memory block which is already allocated.
It found use of in two situations:
1. If the allocated memory block is insufficient
for current application
2. If the allocated memory is much more than
what is required by the current application
The Syntax of this function:
ptr_var = realloc(ptr_var, new_size);
17. Real Life Applications
1. 2-D Array for record keeping
2. Stack of plates in a function/party
3. Queue for bus at bus stand
4. Queue for buying tickets at ticket counter
5. Tree for representing the family relations
6. Graph for traveling in a number of cities
18. Built-in & User defined data
structures
Data Structures
Primitive/Built-in
Data Structures
Non-Primitive/ User
Defined Data Structures
Integer Float PointerCharacter Arrays FilesLists
Linear Lists
Stacks Queues
Non Linear Lists
Graphs Trees
19. Ordered List
• An ordered list is one which is maintained in some
predefined order such as alphabetical or numerical
order. It is also known as Linear List
• For ordered list…
the sequence is as follows:
X1<=X2<=X3………….<=Xn
• Can be implemented in 1-D Array e.g.
(1,2,3,4,5,6,7,8,9,10)
OR
(MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY, SUNDAY)
20. Operations on Ordered List
• Find the length of the list
• Traversing: Read the list from left to right
(OR right to left)
• Searching: Retrieve the i-th element
• Updation: Store a new value in the i-th
position
• Insertion: Insert a new element at position i
• Deletion: Delete an element from position i