Professional Documents
Culture Documents
The Process of allocating memory to the variables during the execution of program
Or at runtime is known as Dynamic Memory Allocation.
1. malloc() 3. free()
2. calloc() 4. realloc()
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
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
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
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
Searching a linked list means to find a particular element in the link 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.
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;
};
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