Professional Documents
Culture Documents
Insertion Operation: Newnode - Next Rightnode
Insertion Operation: Newnode - Next Rightnode
Adding a new node in linked list is a more than one step activity. We Shall
learn this with diagrams here. First, create a node using the same structure
and find the location where it has to be inserted.
Now the next of the node at left should point to the new node.
Similar steps should be taken if the node being inserted at the beginning of
the list. While putting it at the end, then the second last node of list should
point to new node and the new node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using
searching algorithms.
The left (previous) node of the target node now should point to the next node
of the target node −
We need to use the deleted node we can keep that in memory otherwise we
can simply deallocate memory and wipe off the target node completely.
C library function - malloc()
Description
The C library function void *malloc(size_t size) allocates the requested
memory and returns a pointer to it.
Declaration
Following is the declaration for malloc() function.
Parameters
size -- This is the size of the memory block, in bytes.
Return Value
This function returns a pointer to the allocated memory, or NULL if the
request fails.
Example
The following example shows the usage of malloc() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str;
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
free(str);
return(0);
}
Let us compile and run the above program that will produce the following
result:
Description
The C library function void free(void *ptr) deallocates the memory
previously allocated by a call to calloc, malloc, or realloc.
Declaration
Following is the declaration for free() function.
Parameters
ptr -- This is the pointer to a memory block previously allocated with malloc,
calloc or realloc to be deallocated. If a null pointer is passed as argument, no
action occurs.
Return Value
This function does not return any value.
Example
The following example shows the usage of free() function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str;
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
return(0);
}
Let us compile and run the above program that will produce the following
result:
Such self-referential structures are very useful in applications that involve linked data structures,
such as lists and trees. Unlike a static data structure such as array where the number of elements
that can be inserted in the array is limited by the size of the array, a self-referential structure can
dynamically be expanded or contracted. Operations like insertion or deletion of nodes in a self-
referential structure involve simple and straight forward alteration of pointers.