Dsa Unit 1
Dsa Unit 1
CS-303
DATA STRUCTURE
DATA STRUCTURE DEFINITION
• Whenever we want to work with a large amount of
data, then organizing that data is very important.
• If that data is not organized effectively, it is very
difficult to perform any task on that data.
• If it is organized effectively then any operation can
be performed easily on that data.
• Data structure is a method of organizing a large
amount of data more efficiently so that any
operation on that data becomes easy
TYPES OF DATA STRUCTURE
Primitive Data Structures
#include <stdio.h>
void main ()
{
int marks_1 = 56, marks_2 = 78, marks_3 = 88,
marks_4 = 76, marks_5 = 56, marks_6 = 89;
float avg = (marks_1 + marks_2 + marks_3 + marks_4
+ marks_5 +marks_6) / 6 ;
printf(avg);
}
Array
Solution:
The given values are: B = 1020, LB = 1300, W =
2, I = 1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
Properties of the Array
Function Purpose
. malloc Allocates the memory of requested size and
returns the pointer to the first byte of
allocated space
calloc Allocates the space for elements of an
array. Initializes the elements to zero and
returns a pointer to the memory.
realloc It is used to modify the size of previously
allocated memory space.
Free Frees or empties the previously allocated
memory space
The malloc Function
• The malloc() function stands for memory allocation.
• It is a function which is used to allocate a block of
memory dynamically.
• It reserves memory space of specified size and
returns the null pointer pointing to the memory
location.
• The pointer returned is usually of type void. It means
that we can assign malloc function to any pointer.
• Syntax
• ptr = (cast_type *) malloc (byte_size);
The malloc Function
• Here,
• ptr is a pointer of cast_type.
• The malloc function returns a pointer to the
allocated memory of byte_size.
• Example: ptr = (int *) malloc (50)
• When this statement is successfully executed, a
memory space of 50 bytes is reserved.
• The address of the first byte of reserved space is
assigned to the pointer ptr of type int.
The calloc Function
Example:-
Operations on Single Linked List