Inserting a node after the specified node in a singly linked list

To insert a new node after the specified node, first we get the number of the node in an existing list after which the new node is to be inserted. This is based on the assumption that the nodes of the list are numbered serially from 1 to n. The list is then traversed to get a pointer to the node, whose number is given. If this pointer is x, then the link field of the new node is made to point to the node pointed to by x, and the link field of the node pointed to by x is made to point to the new node. Following Figures show the list before and after the insertion of the node, respectively.

# include <stdio.h>
# include <stdlib.h>
int length(struct node *);
struct node 
{
	int data;
	struct node *link;
};

/* a function which appends a new node to an existing list used for
 building a list */
struct node *insert(struct node *p, int n) 
{
	struct node *temp;
	if (p == NULL) 
	{
		p = (struct node *) malloc(sizeof(struct node));
		if (p == NULL) 
		{
			printf("Error\n");
			exit(0);
		}
		p->data = n;
		p->link = NULL;
	} 
	else 
	{
		temp = p;
		while (temp->link != NULL)
			temp = temp->link;
		temp->link = (struct node *) malloc(sizeof(struct node));
		if (temp->link == NULL) 
		{
			printf("Error\n");
			exit(0);
		}
		temp = temp->link;
		temp->data = n;
		temp->link = NULL;
	}
	return (p);
}
/* a function which inserts a newly created node after the specified
 node */
struct node * newinsert(struct node *p, int node_no, int value) 
{
	struct node *temp, *temp1;
	int i;
	if (node_no <= 0 || node_no > length(p)) 
	{
		printf("Error! the specified node does not exist\n");
		exit(0);
	}
	if (node_no == 0) 
	{
		temp = (struct node *) malloc(sizeof(struct node));
		if (temp == NULL) 
		{
			printf(" Cannot allocate \n");
			exit(0);
		}
		temp->data = value;
		temp->link = p;
		p = temp;
	} 
	else 
	{
		temp = p;
		i = 1;
		while (i < node_no) 
		{
			i = i + 1;
			temp = temp->link;
		}
		temp1 = (struct node *) malloc(sizeof(struct node));
		if (temp == NULL) 
		{
			printf("Cannot allocate \n");
			exit(0);
		}
		temp1->data = value;
		temp1->link = temp->link;
		temp->link = temp1;
	}
	return (p);
}

void printlist(struct node *p) 
{
	printf("The data values in the list are\n");
	while (p != NULL) 
	{
		printf("%d\t", p->data);
		p = p->link;
	}
}
int main() 
{
	int n;
	int x;
	struct node *start = NULL;
	printf("Enter the nodes to be created \n");
	scanf("%d", &n);
	while (n-- > 0) 
	{
		printf("Enter the data values to be placed in a node\n");
		scanf("%d", &x);
		start = insert(start, x);
	}
	printf(" The list before deletion is\n");
	printlist(start);
	printf(" \n Enter the node no after which the insertion is to be done\n");
	scanf(" %d", &n);
	printf("Enter the value of the node\n");
	scanf("%d", &x);
	start = newinsert(start, n, x);
	printf("The list after insertion is \n");
	printlist(start);
	getchar();
	return 0;
}
int length(struct node *p) 
{
	int count = 0;
	while (p != NULL) 
	{
		count++;
		p = p->link;
	}
	return (count);
}

Explanation