Circular Linked List
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct link
{
int data;
struct link *next;
};
typedef struct link node;
node *head=NULL,*tail=NULL;
void creation(void);
void insertion(void);
void deletion(void);
void display(void);
void main()
{
int cond;
clrscr();
while(1)
{
printf("enter 1.creation\n2.insertion\n3.deletion\n4.display \n5.exit");
printf("enter condition:");
scanf("%d",&cond);
switch(cond)
{
case 1:
creation();
break;
case 2:
insertion();
break;
case 3:
deletion();
break;
case 4:
display();
break;
case 5:
return;
}
}
}
void creation()
{
node *temp;
temp=(node*)malloc(sizeof(node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
tail->next=head;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
}
}
void insertion()
{
int tgt;
node *temp,*ptr;
printf("enter target:");
scanf("%d",&tgt);
ptr=head;
if(ptr->data==tgt)
{
temp=(node*)malloc(sizeof(node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=head;
head=temp;
tail->next=head;
}
else
{
while(ptr->next->data!=tgt&&ptr->next!=head)
ptr=ptr->next;
if(ptr->next!=head)
{
node *temp;
temp=(node*)malloc(sizeof(node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=ptr->next;
ptr->next=temp;
}
else
printf("target not found");
}
}
void deletion()
{
int tgt;
node *ptr,*temp;
printf("enter target:");
scanf("%d",&tgt);
ptr=head;
if(head->data==tgt)
{
temp=head;
if(head->next==NULL)
{
head=NULL;
tail=NULL;
}
else
head=temp->next;
tail->next=head;
free(temp);
}
else
{
while(ptr->next->data!=tgt&&ptr->next!=head)
ptr=ptr->next;
if(ptr->next!=head)
{
if(ptr->next==tail)
{
temp=tail;
tail=ptr;
ptr->next=NULL;
free(temp);
}
else
{
temp=ptr->next;
ptr->next=temp->next;
free(temp);
}
}
}
}
void display()
{
node *ptr;
ptr=head;
do
{
printf("->%d",ptr->data);
ptr=ptr->next;
}
while(ptr!=head);
}
No comments:
Post a Comment