You are on page 1of 6

#include<stdio.

h>
#include<stdlib.h>

void create();
void display();
void insert_begin();
void insert_spec();
void insert_end();
void delete_begin();
void delete_spec();
void delete_end();

typedef struct node_t


{
int data;
struct node_t *next;
}node;

node *head=NULL,*newnode,*temp;

int main()
{
int choice;
while(1)
{
printf("\n---------------------------------------------------");
printf("\nChoose operations to perform on circular linked list:\n");
printf("1.Create\n2.Display\n");
printf("3.Insert at the beginning\n");
printf("4.Insert at a specific position\n");
printf("5.Insert at the end\n");
printf("6.Delete from beginning\n");
printf("7.Delete from specific position\n");
printf("8.Delete from the end\n9.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();break;
case 2:display();break;
case 3:insert_begin();break;
case 4:insert_spec();break;
case 5:insert_end();break;
case 6:delete_begin();break;
case 7:delete_spec();break;
case 8:delete_end();break;
case 9:exit(0);
default:printf("\nWrong Choice!!!\n");
}
}
}

void create()
{
int i,n;
printf("Enter number of nodes: ");
scanf("%d",&n);
printf("Enter data: ");
for(i=0;i<n;i++)
{
newnode=(node *)malloc(sizeof(node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(i==0)
head=newnode;
else
{
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=newnode;
}
}
newnode->next=head;
printf("\n!!Circular linked list is created!!\n");
}

void display()
{
if(head==NULL)
printf("\n!! Empty list !!\n");
else
{
printf("\nList elements are:\n");
temp=head;
while(temp->next!=head)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("%d-->X\n",temp->data);
}
}

void insert_begin()
{
newnode=(node *)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
//connect last to first node
temp=head;
while(temp->next!=head)
temp=temp->next;
newnode->next=head;
head=temp->next=newnode;
}
printf("\n!! %d is inserted at the beginning!! \n",newnode->data);
}

void insert_spec()
{
int pos;
newnode=(node*)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<=0)
{printf("\n!! Invalid position !!\n");return;}
else if (pos==1)
{
newnode->next=head;
//connect last to first node
temp=head;
while(temp->next!=head)
temp=temp->next;
head=temp->next=newnode;
}
else
{
temp=head;
for(int i=1;i<pos-1;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
printf("\n!! %d is inserted at positon %d !!\n",newnode->data,pos);
}

void insert_end()
{
newnode=(node *)malloc(sizeof(node));
printf("Enter element:\n");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
temp=head;
while(temp->next!=head)
temp=temp->next;
temp->next=newnode;
newnode->next=head;
}
printf("\n!! %d is inserted at the end !!\n",newnode->data);
}

void delete_begin()
{
if(head==NULL)
{ printf("\n!! Empty list. Deletion not possible !!\n");return;}
temp=head;
if(head->next==head)
head=NULL;
else
{
//connect last to first node
while(temp->next!=head)
temp=temp->next;
temp->next=head->next;
temp=head;
head=head->next;
}
printf("\n!! %d is deleted from beginning !!\n",temp->data);
free(temp);
}

void delete_spec()
{
int pos;
node *temp2;
temp=head;
if(head==NULL)
{printf("\n!!! Empty List. Deletion not possible !!!\n");return;}
printf("Enter position:\n");
scanf("%d",&pos);
if (pos<=0)
{printf("\n!! Invalid position !!\n");return;}
else if(pos==1)
{
if(head->next==head)
{ head=NULL;}
else
{
while(temp->next!=head)
temp=temp->next;
temp->next=head->next;
temp=head;
head=head->next;
}
}
else
{
for(int i=1;i<pos;i++)
{
temp2=temp;
temp=temp->next;
}
temp2->next =temp->next;
}
printf("\n!! %d is deleted at position %d !!\n",temp->data,pos);
free(temp);
}

void delete_end()
{
node *temp2;
temp=head;
if(head==NULL)
{printf("\n!! Empty list. Deletion not possible !!\n");return;}
else if(head->next==head)
head=NULL;
else
{
while(temp->next!=head)
{
temp2=temp;
temp=temp->next;
}
temp2->next=head;
}
printf("\n!! %d is deleted from end !!\n",temp->data);
free(temp);
}

You might also like