// INSERT AT BEGINING OPERATION IN CIRCULAR LINKED LIST
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} clrlnk;
clrlnk *create();
void display(clrlnk *);
clrlnk*insertatfirst(clrlnk*,int);
void insertatend(clrlnk *,int);
void insertafter(clrlnk *,int,int);
clrlnk *insertbefore(clrlnk *,int,int);
clrlnk* deletenode(clrlnk *,int);
void main()
{
clrlnk *head=NULL;
int val,gval;
int ch;
while(1)
{
printf("\n1)create\n2)insert at first\n3)insert at end\n4)insert after a node\n5)insert before a node\n6)Delete a node\n7)Display");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
head=create();
break;
case 2:
printf("\nEnter the value:");
scanf("%d",&val);
head=insertatfirst(head,val);
break;
case 3:
printf("\nEnter the value:");
scanf("%d",&val);
insertatend(head,val);
break;
case 4:
printf("\nEnter the value you want to add:");
scanf("%d",&val);
printf("\nEnter the value after which you want to add:");
scanf("%d",&gval);
insertafter(head,val,gval);
break;
case 5:
printf("\nEnter the value you want to add:");
scanf("%d",&val);
printf("\nEnter the value before which you want to add:");
scanf("%d",&gval);
head=insertbefore(head,val,gval);
break;
case 6:
printf("\nEnter the value you want to delete: ");
scanf("%d",&val);
head=deletenode(head,val);
break;
case 7:
printf("\nLinked list is:");
display(head);
break;
}
}
}
clrlnk *create()
{
clrlnk *head=NULL;
clrlnk *ptr;
int val;
while (1)
{
printf("\nEnter the data(enter any negative value for exit ):");
scanf("%d", &val);
if (val < 0)
{
return head;
}
clrlnk *new = (clrlnk *)malloc(sizeof(clrlnk));
new->data = val;
new->next = NULL;
if (head == NULL)
{
head = new;
}
else
{
ptr->next = new;
}
ptr = new;
ptr->next = head;
}
}
clrlnk*insertatfirst(clrlnk*head,int val)
{
clrlnk *ptr;
clrlnk *q=head;
ptr=(clrlnk*)malloc(sizeof(clrlnk));
while(q->next!=head)
{
q=q->next;
}
ptr->data=val;
q->next=ptr;
ptr->next=head;
head=ptr;
return head;
}
void insertatend(clrlnk*head,int val)
{
clrlnk *ptr;
clrlnk *q=head->next;
ptr=(clrlnk*)malloc(sizeof(clrlnk));
while(q->next!=head)
{
q=q->next;
}
ptr->data=val;
ptr->next=q->next;
q->next=ptr;
}
void insertafter(clrlnk*head,int val,int gval)
{
clrlnk *ptr;
clrlnk *q=head;
ptr=(clrlnk*)malloc(sizeof(clrlnk));
while(q->data!=gval && q->next!=head)
{
q=q->next;
}
ptr->data=val;
ptr->next=q->next;
q->next=ptr;
}
clrlnk* insertbefore(clrlnk*head,int val,int gval)
{
clrlnk *ptr,*pre,*h;
clrlnk *q;
if(head->data==gval)
{
h=insertatfirst(head,val);
return h;
}
else
{
q=head->next;
ptr=(clrlnk*)malloc(sizeof(clrlnk));
while(q->data!=gval && q!=head)
{
pre=q;
q=q->next;
}
ptr->data=val;
ptr->next=pre->next;
pre->next=ptr;
return head;
}
}
clrlnk *deletenode(clrlnk *h,int v)
{
clrlnk *ptr,*pre;
if(h->data==v)
{
ptr=h;
while(ptr->next!=h)
ptr=ptr->next;
h=h->next;
ptr->next=h;
}
else
{
pre=h;
ptr=h->next;
while(ptr->data!=v && ptr!=h)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr!=h)
pre->next=ptr->next;
else
printf("Value not found");
}
return h;
}
void display(clrlnk *head)
{
clrlnk *p = head;
do {
printf("%d ", p->data);
p = p->next;
}
while (p!= head);
}