Insertion At Beginning in Circular linked list

Procedure for insertion a node at the beginning of list

Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty,return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.




Algorithm for Insertion at the front of Circular linked list

node* AddFront(node* tail, int num)
{
	node *temp = (node*)malloc(sizeof(node));
	temp->data = num;
	temp->next = temp;	
	if (tail == NULL) 
		return temp;
	temp->next = tail->next;
	tail->next = temp;
	return tail;
}




C function for insertion at begining

void insert_first(struct link *node)
{
	node=start->next;
	ptr=start;
	new1=(struct link *)malloc(sizeof(struct link));
	printf("\n Insert data for first node:");
	scanf("%d",&new1->data);
	ptr->next=new1;
	new1->next=node;
	i++;
}



C programe for insertion at begining in Circular linked list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
	int data;
	struct link *next;
};
int i=0;
struct link *node,*start,*ptr,*new1;

void create_link(struct link *node)
{
	char ch;
	start->next=NULL;
	node=start;
	fflush(stdin);
	printf("\n Enter 'n' for break:");
	ch=getchar();
	while(ch!='n')
	{
		node->next=(struct link *)malloc(sizeof(struct link));
		node=node->next;

		printf("\n Enter data for node:");
		scanf("%d",&node->data);
		node->next=start;
		fflush(stdin);
		printf("\n Enter 'n' for break:");
		ch=getchar();
		i++;
	}
}
void insert_first(struct link *node)
{
	node=start->next;
	ptr=start;
	new1=(struct link *)malloc(sizeof(struct link));
	printf("\n Insert data for first node:");
	scanf("%d",&new1->data);
	ptr->next=new1;
	new1->next=node;
	i++;
}

void display(struct link *node)
{
	int count;
	node=start->next;
	count=i;

	while(count)
	{
		printf("\t%d",node->data);
		node=node->next;
		count--;
	}
}

void main()
{
	char ch;
	clrscr();
	create_link(node);
	insert_first(node);
	printf("List Item Are:\n");
	display(node);
	getch();
}

Output