Wednesday, June 22, 2011

CIRCULAR LINKED LIST


#include<stdio.h>
#include<conio.h>


struct node
{
int info;
struct node *next;
};


struct node* creation();
void display(struct node*);
struct node* insfirst(struct node*);
struct node* inslast(struct node*);
struct node* insbyele(struct node*);
struct node* insbypos(struct node*);
struct node* delatlast(struct node*);
struct node* delbyele(struct node*);
struct node* delbypos(struct node*);
struct node* delfirst(struct node*);







void main()
{
int choice,opt;
struct node *head=NULL;
clrscr();
do
{
 printf("\n\n\t\t\tPROGRAM FOR CIRCULAR LINKED LIST\n");
 printf("\nenter your choice\n1.create linked list\n2.view linked list\n3.insert\n4.deletion of node\n5.quit program\n");
 scanf("%d",&opt);
 switch (opt)
{
 case 1:printf("creation of linked list\n");
head=creation();
printf("\nnode created successfully\ndisplaying the list\n\n");
display(head);
break;
 case 2:printf("\ndisplaying list");
display(head);
break;
 case 3:if(head!=NULL)
{
printf("\nenter your choice\n1.insertion at first\n2.insertion at last\n3.insertion by element\n4.insertion by positon\n");
scanf("%d",&choice);
switch (choice)
{
 case 1:printf("\ninsertion at first\n");
head=insfirst(head);
break;
 case 2:printf("\ninsertion at last\n");
head=inslast(head);
break;
 case 3:printf("\ninsertion by element\n");
head=insbyele(head);
break;
 case 4:printf("\ninsertion by position\n");
head=insbypos(head);


}
}
else
printf("\nwithout creation of a list, insertion is not possible\n");


break;
 case 4:if(head!=NULL)
{printf("\n1.deletion at first\n2.delation at last\n3.deletion by element\n4.deletion by position");
scanf("%d",&opt);
switch(opt)
 {
  case 1:printf("\ndeletion at first\ndisplayinglist\n");
 display(head);
 head=delfirst(head);
 break;
  case 2:printf("\ndeletion at last\ndislaying list\n");
 display(head);
 printf("\n\n");
 head=delatlast(head);
 break;
  case 3:printf("\ndeletion by element\n");
 delbyele(head);
 break;
  case 4:printf("\ndeletion of element by position\n");
 display(head);
 head=delbypos(head);
 break;
 }
}
else
printf("\nlist is empty\ndeletion not possible");
break;
 case 5:printf("\n\t\t\tTHANKYOU");
getch();
exit(0);
 }
 }while(opt!=5);
getch();


}


struct node* creation()
{


 struct node *first,*temp,*new;
 int i,n;
 printf("enter the number of nodes to be created\n");
 scanf("%d",&n);
 first=(struct node*)malloc(sizeof(struct node));
 printf("\nenter data for node 1\n");
 scanf("%d",&first->info);
 first->next=first;
 temp=first;
 if(n>1)
 {
  for(i=1;i<n;i++)
  {
   new=(struct node*)malloc(sizeof(struct node));
   printf("\nenter data for node %d\n",i+1);
   scanf("\n%d",&new->info);
   new->next=first;
   if(i==1)
     first->next=temp;
   temp->next=new;
   temp=new;
  }
 }
  return first;
}


void display(struct node* first)
{
struct node* temp;
temp=first;
printf("\n\n");
while(temp->next!=first)
 {
 printf("::%d->%u::",temp->info,temp->next);
 temp=temp->next;
 }
 printf("::%d->%u::",temp->info,temp->next);
}


struct node* insfirst(struct node* head)
{
struct node *new,*travel;
new=(struct node*)malloc(sizeof(struct node));
printf("\nenter the new data to be inserted\n");
scanf("%d",&(new->info));
new->next=head;
travel=head;
while(travel->next!=head)
 travel=travel->next;
travel->next=new;
head=new;
printf("\nnode created sucessfully\ndisplaying node\n");
display(head);
return(head);
}


struct node* inslast(struct node *head)
{
 struct node *temp1,*newnode;
 newnode=(struct node*)malloc(sizeof(struct node));
 newnode->next=head;
 temp1=head;
 printf("enter the data to be inserted at last");
 scanf("%d",&(newnode->info));


  do
   {


     temp1=temp1->next;
   }while(temp1->next!=head);
   temp1->next=newnode;
 printf("\nnode created sucessfully");
 display(head);
 return(head);
}




struct node* insbyele(struct node* head)
{
 struct node *travel,*newnode;
 int data,f;
 newnode=(struct node*)malloc(sizeof(struct node));
 travel=head;


  printf("\nenter the data after which the newnode is to be created\n");
  scanf("%d",&data);
  while(travel->next!=head)
  {
   if(travel->info==data)
    {
     printf("\nenter data for newnode\n");
     scanf("%d",&(newnode->info));
     newnode->next=travel->next;
     travel->next=newnode;
     f=1;
     break;
    }
    travel=travel->next;
  }
  if(f==1)
  {
   printf("\nnode created sucessfully\n");
   display(head);
   return(head);
  }
  else
  printf("\ndefined data not found in the list\ninsertion not possible");
  return(head);
 }


struct node* insbypos(struct node *head)
 {
  struct node *travel,*newnode;
  int count,pos;
  count=1;
  newnode=(struct node*)malloc(sizeof(struct node));
  travel=head;
   do
   {
    count++;
    travel=travel->next;
   } while(travel->next!=head);
   printf("\nenter the position after which insertion is to be made(b/w 1 and %d)\n",count);
   scanf("%d",&pos);
   if(pos<count)
   {
    travel=head;
    printf("\nenter the data to be inserted\n");
    scanf("%d",&(newnode->info));
    count=1;
    while(count<pos)
    { travel=travel->next;
      count++;
    }
    newnode->next=travel->next;
    travel->next=newnode;
    printf("\nnewnode sucessfully added at positon %d\n",pos);
   }
   else
   printf("invalid positon\n");
   display(head);
   return(head);
 }


struct node* delatlast(struct node *head)
 {
  struct node *last,*prev;
  while(last->next!=head)
  {prev=last;
   last=last->next;
  }
  prev->next=head;
  printf("\n\n");
  display(head);
  return(head);
 }




struct node* delbyele(struct node *head)
{
 int data;
  struct node *travel,*prev;
 printf("enter the data to be deleted from the linked list\n");
 scanf("%d",&data);
 travel=head;
 prev=head;
 while(travel->next!=head)
  {


   travel=travel->next;
   if(travel->info==data)
    {
     prev->next=travel->next;
     printf("\nsucessfully deleted data-%d from the list\n",data);
     display(head);
     return(head);
    }
    prev=travel;
    travel=travel->next;
  }
   if(travel==head)
   printf("element not found\n");
   return(head);
 }


struct node* delbypos(struct node *head)
 {
  struct node *travel,*prev;
  int count,pos;
  count=1;
  prev=head;
  do
  {
   count++;
   travel=travel->next;
  }while(travel->next!=head);
  travel=head;
  printf("\nenter the positon of the node to be deleted\n");
  scanf("%d",&pos);
  if(pos<count)
  {
    count=1;
    while(count<=(pos-1))
    { prev=travel;
      travel=travel->next;
      count++;
    }
    prev->next=travel->next;
    printf("\nelement deleted sucessfully\n");
    display(head);
    return(head);
  }
  else
  printf("\ninvalid entry");
  return(head);
 }


struct node* delfirst(struct node *head)
{
struct node *travel;
travel=head;
while(travel->next!=head)
 travel=travel->next;
travel->next=head->next;
head=head->next;
printf("\nnode deleted sucessfully\ndisplaying node\n");
display(head);
return(head);
}

No comments:

Post a Comment