C Program to implement Circular Linked List to insert and delete a node from front and display the contents of the Circular List using Header Node

Program

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
	int info;
	struct node *link;
};
typedef struct node* NODE;
NODE getnode()
{
	NODE x;
	x = (NODE) malloc(sizeof(struct node));
	if(x == NULL)
	{
		printf("Cannot create node\n");
		return NULL;
	}
	return x;
}
void freenode(NODE x)
{
	free(x);
}
NODE insert_front(int item, NODE head)
{
	NODE temp, cur;
	temp = getnode();	
	temp->info = item;
	if(head->link == head)
	{
		head->link = temp;
		temp->link = head;
		head->info = head->info + 1;
		return head;
	}
	cur = head->link;
	head->link = temp;
	temp->link = cur;
	head->info = head->info + 1;
	return head;
}
NODE delete_front(NODE head)
{
	NODE cur;
	if(head->link == head)
	{
		printf("List is empty. Deletion not possible\n");
		return head;
	}
	cur = head->link;
	head->link = cur->link;
	head->info = head->info - 1;
	printf("Item deleted is %d\n", cur->info);
	freenode(cur);
	return head;
}
void display(NODE head)
{
	NODE cur;
	if(head->link == head)
	{
		printf("List is empty. Cannot print\n");
		return;
	}
	cur = head->link;
	while(cur != head)
	{
		printf("%d\n", cur->info);
		cur = cur->link;
	}
	printf("Node count:\t%d\n", head->info);
}
void main()
{
	int choice, item;
	NODE head;
	head = getnode();
	head->info = 0;
	head->link = head;
	printf("Circular Linked List using header node\n");
	printf("Insert and Delete to the front of the list\n");
	for(;;)
	{
		printf("1. Insert Front\n2. Delete Front\n3. Display\n4. Exit\n");
		printf("Choice:\t");
		scanf("%d", &choice);
		switch(choice)
		{
			case 1:
				printf("Enter the item to be inserted:\t");
				scanf("%d", &item);
				head = insert_front(item, head);
				break;
			case 2:
				head = delete_front(head);
				break;
			case 3:
				display(head);
				break;
			default:
				exit(0);
		}
	}
}

Output

Circular Linked List using header node
Insert and Delete to the front of the list
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
List is empty. Deletion not possible
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	1
Enter the item to be inserted:	10
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	1
Enter the item to be inserted:	20
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	1
Enter the item to be inserted:	30
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	1
Enter the item to be inserted:	40 
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	3
40
30
20
10
Node count:	4
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
Item deleted is 40
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
Item deleted is 30
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	3
20
10
Node count:	2
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
Item deleted is 20
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
Item deleted is 10
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	2
List is empty. Deletion not possible
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	3
List is empty. Cannot print
1. Insert Front
2. Delete Front
3. Display
4. Exit
Choice:	4