CIRCULAR LINKED LIST


CIRCULAR LINKED LIST

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*first=NULL,*last=NULL;
unsigned int count;
int main(void)
{
int choice;
void create(void);
void insert(void);
void rem(void);
void display(void);
printf("\n\tCircular Linked List");
do
{
clrscr();
printf("\n\toperations");
printf("\n\t1.create\n\t2.insert\n\t3.remove\n\t4.display");
printf("\n\t5.exit");
printf("\n\tEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
rem();
break;
case 4:
display();
break;
case 5:
printf("Exited");
getch();
return 0;
default:
printf("Enter(1-5)only");
getch();
}
}
while(1);
}
void create(void)
{
struct node *temp=NULL;
int n;
if(first!=NULL)
{
printf("\nlist is not empty");
getch();
return;
}
printf("enter number of items");
scanf("%d",&n);
if(n<1)
{
printf("\tlist is not created");
getch();
return;
}
count=0;
printf("\n");
while(n--)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n\tHeap exhausted");
getch();
return;
}
printf("Enter the item");
scanf("%d",&temp->info);
temp->next=NULL;
count++;
if(first==NULL)
{
first=last=temp;
}
else
{
last->next=temp;
last=temp;
last->next=first;
}
}
return;
}
void insert(void)
{
struct node *temp1=NULL,*temp2=NULL;
int position;
if(first==NULL)
{
printf("\n\tList is empty");
getch();
}
else
{
printf("enter the position");
scanf("%d",&position);
if(position<1||position>count+1)
{
printf("out of range");
getch();
}
else
{
temp1=(struct node*)malloc(sizeof(struct node));
if(temp1==NULL)
{
printf("Heap Exhausted");
return;
}
printf("Enter the item");
scanf("%d",&temp1->info);
temp1->next=NULL;
if(position==1)
{
temp1->next=first;
first=temp1;
last->next=first;
}
else if(position==count+1)
{
last->next=temp1;
last=last->next;
last->next=first;
}
else
{
temp2=first;
position--;
while(--position)
{
temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next=temp1;
}
count++;
}
}
return;
}
void rem(void)
{
struct node *temp=NULL,*t=NULL;
int position;
if(first==NULL)
{
printf("list is empty");
getch();
}
else
{
printf("enter the position");
scanf("%d",&position);
if(position<1||position>count)
{
printf("out of range");
getch();
}
else
{
if(position==1)
{
temp=first;
first=first->next;
last->next=first;
free(temp);
}
else if(position==count)
{
t=first;
while(t->next!=last)
{
t=t->next;
}
temp=t->next;
free(temp);
t->next=first;
last=t;
}
else
{
t=first;
position--;
while(--position)
{
t=t->next;
}
temp=t->next;
t->next=temp->next;
free(temp);
}
}
count--;
}
return;
}
void display(void)
{
struct node *temp=NULL,*start=NULL;
int position;
if(first==NULL)
{
printf("\nList is empty");
}
else
{
printf("Enter the position from where to display");
scanf("%d",&position);
printf("\nList contains:\n");
printf("\nFrom the beginning");
for(temp=first;temp!=last;temp=temp->next)
printf("%d",temp->info);
printf("From the %d position:",position);
temp=first;
while(--position)
{
temp=temp->next;
}
for(start=temp;temp->next!=start;temp=temp=temp->next)
{
printf("%d->",temp->info);
}
printf("%d",temp->info);
}
getch();
return;
}

Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

BOOKS DETAILS USING C STRUCTURE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE