Erasing a Linked List

Erasing a linked list involves traversing the list starting from the first node, freeing the storage allocated to the nodes, and then setting the pointer to the list to NULL. If p is a pointer to the start of the list, the actions specified through the following code will erase the list:
 

while(p != NULL)
   {
           temp = p;
           p = p->link;
           free(t);
   }

But a better strategy of erasing a list is to mark all the nodes of the list to be erased as free nodes without actually freeing the storage of these nodes. That means to maintain this list, a list of free nodes, so that if a new node is required it can be obtained from this list of free nodes.

# include <stdio.h>
# include <stdlib.h>
struct node 
{
	int data;
	struct node *link;
};
struct node *insert(struct node *, int);
void erase(struct node **, struct node **);
void printlist(struct node *);

void erase(struct node **p, struct node **free) 
{
	struct node *temp;
	temp = *p;
	while (temp->link != NULL)
		temp = temp->link;
	temp->link = (*free);
	*free = *p;
	*p = NULL;
}

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;
	struct node *free = NULL;

	/* this code will create a free list for the test purpose*/
	printf("Enter the number of nodes in the initial free list \n");
	scanf("%d", &n);
	while (n-- > 0) 
	{
		printf("Enter the data values to be placed in a node\n");
		scanf("%d", &x);
		free = insert(free, x);
	}

	/* this code will create a list to be erased*/
	printf("Enter the number of nodes in the list to be created for 
erasing \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 free list islist is:\n");
			printlist(free);
			printf("The list to be erased is:\n");
			printlist(start);
			erase(&start, &free);
			printf("The free list after adding all the nodes from
 the list to be erased is:\n");
			printlist ( free );
			getchar();
			return 0;
}

Explanation

The method of erasing a list requires adding all the nodes of the list to be erased to the list of free nodes, as shown here.