Professional Documents
Culture Documents
1 Data Structures Introduction
1 Data Structures Introduction
DATA STRUCTURES
Data Strucure: -
Any kind of information is called data. Structure means representation.
Data structure is representation of information in primary or disk storage area.
Data structure is kind of relationships between loagical related data elements.
1) Primitive Data Structures: - The primitive data structures are those, which
are directly supported by the machine. A set of integers, real numbers and
some logical statements are the examples of the primitive data structures.
Linear Data Structures: - When we are working with this data structures then
all elements required to organized in sequential manner.
Sequential manner means it is not required to follow always continuous
memory locations.
When we are working with linear data structures then sequential relations are
formatted between elements.
Ex: - Stacks, Queues, Lists and Arrays.
2
Data Structures Using C Satish 8886503423
Memory management in C:
In C language we are having two types of memory management.
a. Static memory allocation
b. Dynamic memory allocation
Static memory allocation:
→When we are creating the memory at time of compilation then it is called
static memory or compile time memory management.
→When we are working with compile time memory management hard coding.
we require to manage which is not adjustable at run time.
→When we are working with static memeory allocation it is not possible to
handle memory to the requirement.
→When we are working with static memory allocation always memory
required to create in continuous memory locations only, i.e, if memory is not
adjustable then we cannot load the program.
→Insertion and deletion is not possible to perform when we are working with
arrays, i.e static memory management.
→If we required to utilized the memory efficiently then recommended to go for
dynamic memory allocation.
malloc(): By using this pre defined function, we can create the memory
dynamically at initial stage.
3
Data Structures Using C Satish 8886503423
malloc function require one argument of type size_type that is data type size.
malloc() creates memory in bytes format and initial value is garbage.
Syntax:
void* malloc(size_type);
when we are working with DMA related functions, we are required to perform
type casting because function returns void *.
Ex:
int *p;
p=(int*)malloc(sizeof(int));
float *p;
p=(float*)malloc(sizeof(float));
int *p;
p=(int*)malloc(sizeof(int)*10);
calloc(): - By using this pre defined function, we can create the memory
dynamically at initial stage.
calloc() requires two argument of type (count,type_size)
count will provide no of elements, size_type is data type size.
4
Data Structures Using C Satish 8886503423
Syntax:
pointervariable=(datatype*)calloc(size,sizeof(datatype));
Ex: int *p;
We want to store 5 elements for the above variable
p=(int*)calloc(5,sizeof(int));
scanf("%d%d",p,q);
*sum=*p+*q;
printf("Sum = %d",*sum);
free(p);
free(q);
free(sum);
getch();
}
Exercises:
1) Accept three numbers and display the biggest number using pointers.
2) Accept a number and display in reverse order.
6
Data Structures Using C Satish 8886503423
Exercises:
1) Accept n elements into an array and display the search no is found
or not.
p=(int*)malloc(m*n*sizeof(int));
printf("Enter %d elements:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",(p+i)+j);
}
printf("The Matrix is:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",*(p+i)+j);
}
getch();
}
}
printf("The Matrix is:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",*(p+i)+j);
}
getch();
}
Exercises:
1) Accept mXn matrix and display the transpose of the given matrix.