Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Data Structures Using C Satish 8886503423

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.

The data structures may be classified into two types.


1 Primitive Data Structures.
2 Non-primitive Data Structures.

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.

2) Non-primitive Data Structures: - These data structures have no specific


instructions to manipulate the individual data items are called Non-primitive
Data Structures. The simple example of the non-primitive data structure is the
data structure consisting of the complex numbers.
The non-primitive data structures are divided into linear data structures
and non-linear 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.

Non-Linear Data Structures: - A non-linear data structure represents the


hierarchical relationship between the elements.
Ex: - Trees and Graphs and Tables.
1
Data Structures Using C Satish 8886503423

Real time applications of DS:


1. Compiler designing (Stack memory allocation can be organized in
compiler designing)
2. Operating system design like memory management (Linked list + Hash
map)
3. Data base management system (BTree)
4. File system representation of OS (Trees)
5. Statistical analysis package (Data mining algorithms)
6. Network data model (Graphs)

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.

Dynamic memory allocation:


→It is a procedure allocating or destroying the memory at run time, i.e
dynamically.
→By using DMA we can utilize the memory efficiently as per our requirement.
• DMA related all predefined functions are available in <alloc.h>,
<malloc.h>, <stdlib.h>.

DMA related pre defined functions are:


malloc() calloc() realloc() free()
farmalloc() farcalloc() farrealloc() farfree()

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);

free():- By using this pre defined function, we can deallocate dynamically


allocated memory.
When we are working with DMA related memory, it stores in heap area
of data segment and it is permanent memory, if we are not deallocating.
When we are working with DMA related programs, at the end of the
program recommended to deallocate memory by using free() function.
free() function requires one argument of type (void*) and retrurns void
type.

Syntax: void free(void *ptr);


Ex: free(p);

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

When we are working with calloc() function, it creates the memory in


block format and initial value is zero.

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));

realloc: - By using this predefined function we can create the memory


dynamically at middle stage of the program.
Generally this function is required to use when we are reallocating
memory.
realloc() requires two argument of type void*, size_type.
When we are working with realloc() function, it creates the memory in
bytes format and initial value is garbage.
Syntax:
pointer variable=(datatype*)realloc(pointervarible,size);
Ex: char *s;
s=(char *)malloc(10);
s=(char*)realloc(s,15);

Accept two numbers and display the sum.


#include<stdio.h>
main()
{
int *p,*q,*sum;
clrscr();
p=(int*)malloc(sizeof(int));
q=(int*)malloc(2);
sum=(int*)malloc(2);
printf("Enter two numbers");
5
Data Structures Using C Satish 8886503423

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.

Single dimensional arrays:


Write a program using accept n numbers and display that numbers.
(Using malloc function).
#include<stdio.h>
main()
{
int i,n,*p;
clrscr();
printf("Enter how many elements:");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",p+i);
printf("The elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",*(p+i));
getch();
}

6
Data Structures Using C Satish 8886503423

Write the above program using Using calloc function.


#include<stdio.h>
main()
{
int i,n,*p;
clrscr();
printf("Enter how many elements:");
scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",p+i);
printf("The elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",*(p+i));
getch();
}

Exercises:
1) Accept n elements into an array and display the search no is found
or not.

2) Accept n elements into an array and display in sorting order.

Double dimensional arrays:


Display mXn matrix(using malloc function)
#include<stdio.h>
main()
{
int i,j,m,n,*p;
clrscr();
printf("Enter how many rows and columns:");
scanf("%d %d",&m,&n);
7
Data Structures Using C Satish 8886503423

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();
}

Display mXn matrix(using calloc function)


#include<stdio.h>
main()
{
int i,j,m,n,*p;
clrscr();
printf("Enter how many rows and columns:");
scanf("%d %d",&m,&n);
p=(int*)calloc(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);
8
Data Structures Using C Satish 8886503423

}
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.

You might also like