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

Insertion Operation

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.

Imagine that we are inserting a node B (NewNode), between A (LeftNode)


and C (RightNode). Then point B.next to C

NewNode.next −> RightNode;

It should look like this −

Now the next of the node at left should point to the new node.

LeftNode.next −> NewNode;


This will put the new node in the middle of the two. The new list should look
like this −

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 −

LeftNode.next −> TargetNode.next;


This will remove the link that was pointing to target node. Now we shall
remove to what target node is pointing.

TargetNode.next −> NULL;

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.

void *malloc(size_t size)

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;

/* Initial memory allocation */


str = (char *) malloc(15);
strcpy(str, " cprogramming ");
printf("String = %s, Address = %u\n", str, 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:

String = cprogramming, Address = 355090448


String = cprogramming.com, Address = 355090448

C library function - free()

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.

void free(void *ptr)

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;

/* Initial memory allocation */


str = (char *) malloc(15);
strcpy(str, " cprogramming ");
printf("String = %s, Address = %u\n", str, str);

/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);

/* Deallocate allocated memory */


free(str);

return(0);
}

Let us compile and run the above program that will produce the following
result:

String = cprogramming, Address = 355090448


String = cprogramming.com, Address = 355090448
Self-Referential Data Structure in C
A self-referential data structure is essentially a structure definition which includes at least one
member that is a pointer to the structure of its own kind. A chain of such structures can thus be
expressed as follows.
struct name {
member 1;
member 2;
...
struct name *pointer;
};
The above illustrated structure prototype describes one node that comprises of two logical
segments. One of them stores data/information and the other one is a pointer indicating where the
next component can be found. .Several such inter-connected nodes create a chain of structures.
The following figure depicts the composition of such a node. The figure is a simplified
illustration
of nodes that collectively form a chain of structures or linked list.

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.

You might also like