Data
structure:
·
Data structure is
representation of the logical relationship existing between individual elements
of data.
·
Data structure is a
way of organizing all data items.
·
It is a mathematical or model of particular organization of data items.
Data
structure mainly specifies:
1.
Organization of data
2.
Accessing methods
3.
Processing alternatives of information
Classification:
Data structures are classified into TWO types that are
1. PRIMITIVE DATA STRUCTURE:
- Integer
- Float
- Character
- Pointer
2. NON-PRIMITIVE DATA
STRUCTURE.
- Arrays
- lists
1.
Linear
list (Linked lists, stacks, Queues)
2.
Non-linear list
(Trees, Graphs)
- Files
Most commonly used operations on data structures:
a. CREATE
b. DESTROY or DELETE
c. SELECTION
d. UPDATION
a. CREATE operation results in
reserving memory
b. DESTROY operation destroys
the memory space allocated
c. SELECTION operation deals with
accessing a particular data within the Data Structure.
d. UPDATE operation updates or
modifies the data.
Other operations performed on data structures includes:
a. Searching
b. Sorting
c. Merging
a.
Searching operation finds the presence of desired data item in list.
b. Sorting is process of
arranging all data items in particular order, i.e., either ascending or
descending order.
c. Merging is a process of
combining the data items of two different sorted lists in a single sorted list.
Memory
allocation in ‘c’:
Two types of memory allocation in ‘c’
- Compile – time /
static allocation
- Run – time /
dynamic allocation(using pointers)
For
dynamic allocation and de-allocation:
‘C’
provides 1.malloc ()
2.calloc()
3. free()
4.realloc() functions
1.
malloc() :
Function
allocates a block of memory in bytes
mallow(no.of elements *
size of each element);
|
For
eg: int *ptr;
Ptr = malloc(10*sizeof(int));
Size
represents size of memory required in bytes.
But
function malloc() returns a void pointer, so a cart operator is required to
change the returned pointer type according to our need.
Ptr_var =(type_cart *)malloc(size);
|
Here
ptr_var is holds starting address of allocated memory
(type_cart *) is data type to which returned pointer is to be converted
malloc(size) is specifies size of allocated memory block
For
ex: int *ptr;
ptr =(int *)malloc(10*sizeof(int));
char *ptr;
ptr = (char *)malloc(10*sizeof(char));
For
structures:
struct
student
{
int roll_no;
char name[30];
float percentage;
};
Struct
student *st_ptr;
St_ptr
= (struct student *)malloc(sizeof(struct student));
Program: To
demonstrate the use of dynamic memory allocation using malloc() function.(sum
and average of elements) ?
#include<stdio.h>
#include<conio.h>
#define
NULL 0
void
main()
{
int *ptr;
int n, sum;
float avg;
sum=0;
printf(“enter no. of elements to insert”);
scanf(“%d”,&n);
ptr=(int *)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf(“required memory not available”);
exit(0);
}
else
{
printf(“enter the elements”);
for(int i=0;i<n;i++)
scanf(“%d”,ptr+i);
for(i=0;i<n; i++)
{
Sum=sum+(*(ptr+i));
}
printf(“sum of %d elements of array is =%d”, n , sum);
avg=sum/n;
printf(“average of %d elements of array is =%f”, n , avg);
}
}
Calloc():
Function
works exactly similar to malloc() function except for the fact that it needs
two arguments as against one argument required by malloc().
For eg: int
*ptr;
Ptr=(int *)calloc(10,2);
Here
10 is number of elements, 2 is size of
data type
free():
Function is used to de–allocate the previously allocated memory using malloc()
or calloc() function.
free(ptr_var);
realloc():
Function is used to resize the of memory block, which is already allocated.
Ptr_var= realloc(ptr_var,new_size);
|
Program
illustrates the use of function free() and realloc():
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void
main()
{
Char
*msg;
msg=(char
*) malloc(30*sizeof(char));
strcpy(msg,
“c programming”);
printf(“
message now is : %s”, msg);
msg
= (char *) realloc(msg,50);
strcpy(msg,
“Java programming”);
printf(“message
now is : %s”,msg);
free(msg);
}
No comments:
Post a Comment