Circular Linklist | Data Structure | C Program

Circular link list :



Code:

INSERT AT BEGINNING:

struct node *temp1,*temp2;
temp1=(struct node*)malloc(sizeof(struct node));
temp2=start;
printf("Enter value insert:\t");
scanf("%d",&temp1->info);
if(start==NULL)
{
 start=temp1;
 temp1->link=start;
}
else
{
 while(temp2->link!=start)
  temp2=temp2->link;
 temp2->link=temp1;
 temp1->link=start;
 start=temp1;
}
}

INSERT AT GIVEN POSITION:

void cr_mid()
{
int i,pos;
struct node *temp1,*temp2;
temp2=start;
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter the postion:\n");
scanf("%d",&pos);
printf("Enter value to insert:\t");
scanf("%d",&temp1->info);
for(i=1;i<pos-1;i++)
 temp2=temp2->link;
temp1->link=temp2->link;
temp2->link=temp1;
}

INSERT AT END:

void cr_end()
{
struct node *temp1,*temp2;
temp1=(struct node*)malloc(sizeof(struct node));
temp2=start;
printf("Enter value to insert:\n");
scanf("%d",&temp1->info);
if(start==NULL)
{
 start=temp1;
 temp1->link=start;
}
else
{
 temp1->link=start;
 while(temp2->link!=start)
  temp2=temp2->link;
 temp2->link=temp1;
}
}

DELETE AT BEGIN:

void de_fr()
{
 struct node *temp1,*temp2;
 temp1=temp2=start;
 if(start==NULL)
  printf("List is empty!\n");
 else
 {
  while(temp1->link!=start)
   temp1=temp1->link;
  if(start->link==start)
  {
   free(start);
   start=NULL;
  }
  else
  {
   start=start->link;
   temp1->link=start;
   free(temp2);
  }
 }
}

DELETE AT GIVEN POSITION:

void de_mid()
{
int i,pos;
struct node *temp1,*temp2;
if(start==NULL)
        printf("List is empty!\n");
else
{
 printf("Enter the postion:\n");
 scanf("%d",&pos);
 if(start->link==start)
 {
  free(start);
  start=NULL;
 }
 else if(pos==1) 
 {
  temp2=temp1=start;
  while(temp2->link!=start)
   temp2=temp2->link;
  start=start->link;
  temp2->link=start;
  free(temp1);
 }
 else
 {
  temp1=start;
  for(i=1;i<=pos-1;i++)
  {
   temp2=temp1;
   temp1=temp1->link;
  }
  temp2->link=temp1->link;
  free(temp1);
 }
}
}

DELETE AT END:

void de_end()
{
struct node *temp1,*temp2;
temp1=start;
if(start==NULL)
 printf("List is empty!!\n");
else
{
 if(start->link==start)
 {
  free(start);
  start=NULL;
 }
 else
 {
  while(temp1->link!=start)
  { 
   temp2=temp1;
   temp1=temp1->link;
  }
  temp2->link=start;
  free(temp1);
 }
}
}

DISPLAY:

 void di()
{
 struct node *temp;
 if(start==NULL)
  printf("List is empty!\n");
 else
 {
  temp=start;
  printf("The elements:\n");
  do
  {
   printf("%d\t",temp->info);
   temp=temp->link;
  }
  while(temp!=start);
 }
}

---------------------------------------------------------

Data structure : Singly linklist

Comments

Popular posts from this blog

Print name in a pattern | name as abbreviation | C-Program

Print Fibonacci Series using Recursion | C program :