You are on page 1of 21

DMA(Dynamic Memory Allocati on)

The Process of allocating memory to the variables during the execution of program
Or at runtime is known as Dynamic Memory Allocation.

There are many Function used in DMA.

1. malloc() 3. free()

2. calloc() 4. realloc()

Note:- These function are declared in <stdlib.h>, So we include that header in


any program.
1. ) malloc() :- It allocates single blocks of memory. Its reserves a block of
memory of specified size and returns a pointer of type void. It means we
assign it to any type of pointer.

Syntax:

Ptr=(data_type*) malloc(sizeof(data_type));

Example:-

Ptr=(int*)malloc(10*sizeof(int));
This statement is used DMA equivalent to 10 times the area of int bytes.
2.) calloc() :- This function allocates multiple blocks of memory. It stands for
Contiguous memory allocation.

Syntax:
Ptr= (data_type*) calloc(n,sizeof(data_type*));
This statement allocate contiguous space for n block of each Element
Size bytes.

Example:-
ptr=(int*)calloc(10,sizeof(int));
Calloc() returns a pointer to the first bytes to the allocated region.

Note: Only difference between malloc() and calloc() is that when we use calloc()
all bytes initialized zero.
3.) Free():- when we release the previously allocated memory space
then use this function.

Syntax:
free(ptr);
where ptr is pointer that has been created by malloc() or calloc().

4.) Realloc():- This function is change the memory size already allocated
by calloc() and malloc(). This is called reallocation of memory.
Syntax:
ptr=realloc(ptr,newsize);
It returns a pointer to first bytes of the memory block.
Link list

Link list a Data Structure used for storing Collections of node.

Link list has the following properties

-- Successive elements are connected by Pointers


-- Last Element points to NULL
-- It can grow or shrink during the execution of a program
-- It can be made just as long as required
-- It does not waste memory Space
We will implement the link list using the following Code:

struct node
{
int data;
struct node*next;
};
NODE

DATA will store the information part Next will store the address of next node
Diff erence between link list
and Array
An array is Linear collection of data element and Link list is Linear collection of nodes.

Link list can be accessed only in a sequential manner, but an array insertion and
Deletion can be done at any point in the list in a constant time.

In link list we can add number of elements in the list. But not possible in array.

Link list provide an efficient way of storing related data and perform basic operation
Such as insertion , deletion and updation of information.
There are diff erent types of link list

1. Singly link list 2. Circular link list

3. Doubly link list


Singly link list

A singly link list is simplest type of linked list in which every node contain
some data and pointer to the next node of the same data type.

START

1 2 3 4 X

Singly link list


There are many Basic operation perform in Singly Link List

1. Creating
2. Traversing
3. Searching
4. Inserting new node in the list
 Insert at Beginning
 Insert at End
 Insert after given node
4. Delete node from the List
 Delete the first node
 Delete last node
 Delete after a given node
Traversing a singly link list

Traversing link list means accessing the nodes of the list in order to perform some
Processing on them.

A link list always contain a pointer variable START which stored the address of
The first node of the list. End of the list is marked by stored NULL or -1 in next
Field of the last node.
Algorithm of traversing a link list

Step 1 : [initialize] set PTR=START


Step 2: Repeat Steps 3 and 4 while PTR!= NULL
Step 3 : apply process to PTR->DATA
Step 4 : set PTR= PTR->NEXT
[END OF LOOP]
Step 5 : Exit
Searching Link list

Searching a linked list means to find a particular element in the link list…

Algorithm to search Link list

Step 1 : [INITIALIZE] set PTR=START


Step 2 : Repeat steps 3 while PTR!=NULL
Step 3 : if VAL=PTR->DATA
SET POS=PTR
go to Step 5
else
set PTR=PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4 : set POS=NULL
Step 5 : Exit
Insert after a given Node
Suppose we want to add a new node with data and add it as the last node of the list.

Algorithm to add after a given node


Step 1 : IF AVAIL=NULL then
write Overflow
Go to Step 10 Step 10 : Set PREPTR->NEXT=New_Node
[END OF IF]
Step 2 : set New_Node=AVAIL Step 11 : Set New_Node->NEXT=PTR
Step 3 : set AVAIL = AVAIL-> NEXT Step 12 : Exit
Step 4 : Set New_Node->data=VAL
Step 5 : set PTR=START
Step 6 : Set PREPTR= PTR
Step 7 : Repeat step 8 and 9 while PREPTR->DATA!=NUM
Step 8 : set PREPTR=PTR
Step 9 : Set PTR=PTR->NEXT
[END OF LOOP]
Delete the first Node
Suppose we want to delete a node from the beginning of the list

Algorithm to Delete the first Node from the linked list

Step 1 : IF START= NULL , then


write Underflow
go to step 5
[END OF IF]
Step 2 : SET PTR = START
Step 3 : SET START=START->NEXT
Step 4 : FREE PTR
Step 5 : EXIT
Delete the last Node
Suppose we want to delete a node from the Last Node of the Linked list

Algorithm to Delete the Last Node from the linked list

Step 1 : IF START= NULL , then


write Underflow
go to step 8
[END OF IF]
Step 2 : SET PTR = START
Step 3 : Repeat step 4 and 5 while PTR->NEXT!= NULL
Step 4 : set PREPTR=PTR
Step 5 : PTR=PTR->NEXT
[END OF LOOP]
Step 6 : Set PREPTR->NEXT=NULL
Step 7: FREE PTR
Step 8 : EXIT
Delete the Node after a given Node
Suppose we want to delete a node from the given Node of the Linked list

Algorithm to Delete the Node from the given node of linked list
Step 1 : IF START= NULL , then
write Underflow
go to step 10
[END OF IF]
Step 2 : SET PTR = START
Step 3 : set PREPTR=PTR
Step 4 : Repeat Step 5 and 6 while PREPTR->DATA!=NUM
Step 5 : Set PREPTR=PTR
Step 6 : PTR=PTR->NEXT
[END OF LOOP]
Step 7 : Set TEMP= PTR->NEXT
Step 8 : Set PREPTR->NEXT= TEMP->NEXT
Step 9: FREE PTR
Step 10 : EXIT
Circular link list

In Circular link list, the last node contain a pointer to the first node of the list.

While Traversing the circular link list , we can begin at any node and traverse
the list in any direction, forward or backward, until we reach the same node
Where we started.

The circular link list has no beginning and no Ending.

START

1 2 3 4
There are many Basic operation perform in Circular Link List

1. Creating
2. Traversing
3. Searching
4. Inserting new node in the list
 Insert at Beginning
 Insert at End
 Insert after given node
4. Delete node from the List
 Delete the first node
 Delete last node
 Delete after a given node
Doubly Link List

A Doubly Link List or Two-way Linked list is more complex type of link list
Which contain a pointer to the next as well as previous node in the sequence.
Therefore, It consist of three parts.
The structure of a doubly link list is
struct node
{
struct node *prev;
int data;
struct nod * next;
};

Address of previous node Address of next node


data
There are many Basic operation perform in Doubly Link List

1. Traversing
2. Searching
3. Inserting new node in the list
 Insert at Beginning
 Insert at End
 Insert after given node
4. Delete node from the List
 Delete the first node
 Delete last node
 Delete after a given node

You might also like