Deleting the nodes in a singly linked list

To delete a node, first we determine the node number to be deleted (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, as well as a pointer to a node that appears before the node to be deleted. Then the link field of the node that appears before the node to be deleted is made to point to the node that appears after the node to be deleted, and the node to be deleted is freed. Following Figures show the list before and after deletion, respectively.

# include <stdio.h>
# include <stdlib.h>
struct node *delet(struct node *, int);
int length(struct node *);
struct node
{
	int data;
	struct node *link;
};
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);
}

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 id\n");
	printlist(start);
	printf("% \n Enter the node no \n");
	scanf(" %d", &n);
	start = delet(start, n);
	printf(" The list after deletion is\n");
	printlist(start);
	getchar();
	return 0;
}

/* a function to delete the specified node*/
struct node *delet(struct node *p, int node_no)
{

	struct node *prev, *curr;
	int i;

	if (p == NULL)
	{
		printf("There is no node to be deleted \n");
	}
	else
	{
		if (node_no > length(p))
		{
			printf("Error\n");
		}
		else
		{
			prev = NULL;
			curr = p;
			i = 1;
			while (i < node_no)
			{
				prev = curr;
				curr = curr->link;
				i = i + 1;
			}
			if (prev == NULL)
			{
				p = curr->link;
				free(curr);
			}
			else
			{
				prev->link = curr->link;
				free(curr);
			}
		}
	}
	return (p);
}
/* a function to compute the length of a linked list */
int length(struct node *p)
{
	int count = 0;
	while (p != NULL)
	{
		count++;
		p = p->link;
	}
	return (count);
}

Explanation