C Program For Circular double Linked List

/*Circular double Linked List*/


#include<stdio.h>
#include<conio.h>
struct node
{
struct node *prev;
int n;
struct node *next;
};
struct node *curr,*temp,*pr;
struct node * create(struct node*);
void display(struct node*);
void insend(struct node *);
struct node *insbeg(struct node*);
void inspos(struct node *);
struct node *delbeg(struct node*);
struct node *delend(struct node *);
void delpos(struct node *);
void count(struct node*);
void search(struct node*);
void reverse(struct node *);
struct node *split(struct node*);
void merge(struct node*,struct node*);
void sort(struct node*);
void main(void)
{
struct node *s,*s1;
int cch,ch;
clrscr();
s=NULL;
s1=NULL;
do
{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insend\n");
printf("4.Insbeg\n");
printf("5.Inspos\n");
printf("6.Delbeg\n");
printf("7.Delend\n");
printf("8.Delpos\n");
printf("9.count\n");
printf("10.Search\n");
printf("11.Reverse\n");
printf("12.Split\n");
printf("13.Merge\n");
printf("14.Sort\n");
printf("15.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);
 switch(ch)
 {
 case 1:s=create(s);
  break;
 case 2: printf("1.First List\n2.Second List\n");
  printf("Enter u r choice\n");
  scanf("%d",&cch);
  if (cch==1)
  {
  display(s);
  }
  else if (cch==2)
  {
  display(s1);
  }
  break;
 case 3:insend(s);
  break;
 case 4:s=insbeg(s);
  break;
 case 5:inspos(s);
  break;
 case 6:s=delbeg(s);
  break;
 case 7:s=delend(s);
  break;
 case 8:delpos(s);
  break;
 case 9:count(s);
  break;
 case 10:search(s);
  break;
 case 11:reverse(s);
  break;
 case 12:s1=split(s);
  break;
 case 13:merge(s,s1);
  s1=NULL;
  break;
 case 14:sort(s);
  break;
 }

}while(ch!=15);

}

struct node * create(struct node *x)
{
 if (x==NULL)
 {
 x=(struct node*)malloc(sizeof(struct node));
 printf("Enter the number");
 scanf("%d",&x->n);
 x->next=x;
 x->prev=x;
 return x;
 }
 else
 {
 printf("The node already create\n");
 return x;
 }
}

void display(struct node *x)
{
curr=x;
 while(curr->next!=x)
 {
 printf("%d->",curr->n);
 curr=curr->next;
 }
printf("%d->",curr->n);
}

void insend(struct node *x)
{
curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 }
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->next=x;
temp->prev=curr;
curr->next=temp;
x->prev=temp;
}

struct node * insbeg(struct node *x)
{
curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 }

temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->next=x;
x->prev=temp;
curr->next=temp;
temp->prev=curr;
return temp;
}


void inspos(struct node *x)
{
int pos,c=1;
curr=x;
printf("Enter the pos to be inserted\n");
scanf("%d",&pos);
 while(curr->next!=x)
 {
 c++;
  if (c==pos)
  {
  temp=(struct node*)malloc(sizeof(struct node));
  printf("Enter the number");
  scanf("%d",&temp->n);
  temp->next=curr->next;
  curr->next->prev=temp;
  curr->next=temp;
  temp->prev=curr;
  }
 curr=curr->next;
 }
}


struct node *delbeg(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("No node to be deleted\n");
 return x;
 }
 else if (curr->next==x)
 {
 printf("u r deleting one and only node\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
 temp=x;
  while(temp->next!=x)
  {
  temp=temp->next;
  }
 x=x->next;
 free(curr);
 temp->next=x;
 x->prev=temp;
 return x;
 }
}


struct node * delend(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("No node to be deleted\n");
 return x;
 }
 else if (curr->next==x)
 {
 printf("U r deleting final node\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
  while(curr->next!=x)
  {
  pr=curr;
  curr=curr->next;
  }
 pr->next=x;
 x->prev=pr;
 free(curr);
 return x;
 }
}


void delpos(struct node *x)
{
int c=1,pos;
printf("Enter the pos to be deleted\n");
scanf("%d",&pos);
curr=x;
 while(curr->next!=x)
 {
 c++;
  if (c==pos)
  {
  temp=curr->next;
  curr->next->next->prev=curr;
  curr->next=curr->next->next;
  free(temp);
  break;
  }
 curr=curr->next;
 }
}


void count(struct node *x)
{
int c=1;
curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 c++;
 }
printf("The no of nodes are %d\n",c);
}


void search(struct node *x)
{
int se,found=0;
curr=x;
printf("Enter the searching element");
scanf("%d",&se);

 while(curr->next!=x)
 {
  if (curr->n==se)
  {
  printf("The element found\n");
  found=1;
  break;
  }
 curr=curr->next;
 }

if (found==0)
{
 if (curr->n==se)
 {
 printf("The element found\n");
 }
 else
 {
 printf("The element not found\n");
 }
}
}


void reverse(struct node *x)
{
curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 }
temp=curr;
 while(curr->prev!=temp)
 {
 printf("%d->",curr->n);
 curr=curr->prev;
 }
printf("%d->",curr->n);
}


struct node * split(struct node *x)
{
int pos,c=1;
curr=x;
printf("Enter the pos to split\n");
scanf("%d",&pos);

  while(curr->next!=x)
  {
  c++;
   if(c==pos)
   {
   temp=curr->next;
   curr->next=x;
   x->prev=curr;
   break;
   }
  curr=curr->next;
  }
curr=temp;
while(curr->next!=x)
{
curr=curr->next;
}
temp->prev=curr;
curr->next=temp;
return temp;

}

void merge(struct node *x,struct node *y)
{
temp=y;
 while(temp->next!=y)
 {
 temp=temp->next;
 }

curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 }
curr->next=y;
y->prev=curr;
temp->next=x;
x->prev=temp;
}


void sort(struct node *x)
{
int c=1,i,j,te;
curr=x;
 while(curr->next!=x)
 {
 curr=curr->next;
 c++;
 }

 for(i=0;i<c;i++)
 {
 curr=x;
  for(j=0;j<c-i-1;j++)
  {
   if (curr->n >curr->next->n)
   {
   te=curr->n;
   curr->n=curr->next->n;
   curr->next->n=te;
   }
  curr=curr->next;
  }
 }
}

Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment