circular doubly linked list

Circular Doubly linked list
In this linked list we can go from first node to last node and last node to first node directly.In this case the left link filled of first node contain address of last node and right link of last node contain address of first node.


// program of circular doubly linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();//function prototype
void display();
void sinsert();
void pinsert();
void einsert();
void sdelete();
void edelete();
void pdelete();
void searching();
void sorting();
struct node
{
    int info;
    struct node *llink,*rlink;
};

struct node *first=NULL,*last=NULL,*new1;
int main()
{
    int choice;
    do
    {
        system("cls");
        printf("press 1 to insert value in list\npress 2 to display list\npress 3 to insert at first position\npress 4 to insert at perticular position \npress 5 to insert at last position\npress 6 to delete firat value\npress 7 to delete last value\npress 8 to delete at perticular position\npress9 to searching value\npress 10 for sorting list->\t");
        printf("\n\nenter choice=");
        scanf("%d",&choice);
        switch (choice)
        {
    case 1:
        create(); break;
    case 2:
        display();break;
   case 3:
        sinsert();break;
    case 4:
        pinsert();break;
    case 5:
        einsert();break;
   case 6:
        sdelete();break;
    case 7:
        edelete();break;
    case 8:
        pdelete();break;
     case 9:
        searching();break;
     case 10:
        sorting(); break;
    default:
        printf("Invalid choice!");
        }

    }
    while(choice>0&&choice<11);

}
//create node
void create()
{
    new1=(struct node*)malloc(sizeof(struct node));
    printf("\nEnter the value of list-");
    scanf("%d",&new1->info);
    new1->llink=NULL;
    new1->rlink=NULL;
    if(first==NULL)
       first=new1;
       else
       {
           last->rlink=new1;
           new1->llink=last;
       }


       last=new1;
       last->rlink=first;
       first->llink=last;

}
void display()
{
    struct node *temp;
    printf("\nAll value of node\n");
    temp=first;
    do
    {
        printf("%d\n",temp->info);
        temp=temp->rlink;
    }
    while(temp!=first);
    system("pause");
}
void sinsert()
{
    new1=(struct node*)malloc(sizeof(struct node));
    if(first==NULL)
        printf("\nno node here!");
    else{
        printf("\nenter the value");
        scanf("%d",&new1->info);
        new1->rlink=first;
        new1->llink=last;
        first->llink=new1;
        first=new1;
        last->rlink=first;
    }
}

void pinsert()
{
    new1=(struct node*)malloc(sizeof(struct node));
    struct node *temp,*pre;
    int count1=0,position;
    printf("\nenter the position");
    scanf("%d",&position);
    temp=first;
    do
    {
        temp=temp->rlink;
        count1++;
    }
    while(temp!=first);
    if(position>count1||position<1)
        printf("\ninvalid position!\n");
    else
    {
        printf("\nEnter the value of list-");
        scanf("%d",&new1->info);
        temp=first;
        count1=0;
        do
        {
            pre=temp;
            temp=temp->rlink;
            count1++;
        }
        while(count1!=position-1);
        pre->rlink=new1;
        temp->llink=new1;
        new1->llink=pre;
        new1->rlink=temp;
    }

}
void einsert()
{
      new1=(struct node*)malloc(sizeof(struct node));
      if(first==NULL)
        printf("\nthere is no node\t");
      else
      {
          printf("\nenter the value of node-");
          scanf("%d",&new1->info);
          new1->llink=last;
          last->rlink=new1;
          last=new1;
          last->rlink=first;
          first->llink=last;

      }

}
void sdelete()
{
    printf("\ndeleted first node=%d",first->info);
    first=first->rlink;
    first->llink=last;
    last->rlink=first;
    system("pause");
}
void edelete()
{
    printf("\ndeleted last node=%d",last->info);
    last=last->llink;
    last->rlink=first;
    first->llink=last;
    system("pause");
}
void pdelete()
{
    new1=(struct node*)malloc(sizeof(struct node));
    struct node *temp,*pre;
    int count1=0,position;
    printf("\nenter the position you want to delete-");
    scanf("%d",&position);
    temp=first;
    do
    {
        temp=temp->rlink;
        count1++;
    }
    while(temp!=first);

    if(position>count1||position<1)
        printf("invalid position!\n");
    else
    {
     temp=first;
     count1=1;
     do
     {
         pre=temp;
         temp=temp->rlink;
         count1++;
     }
     while(count1!=position);
     pre->rlink=temp->rlink;
     temp=temp->rlink;
     temp->llink=pre;

    }

}

void searching()
{
    struct node *temp;
    int svalue,flag=0;
    printf("\nEnter the value for serching-");
    scanf("%d",&svalue);
    temp=first;
    do
    {
        if(temp->info==svalue)
        {
            flag=1;
            break;
        }
        temp=temp->rlink;
    }
    while(temp!=first);
    if(flag==1)
        printf("serching is found\n");
    else
        printf("serching is not found\n");
    system("pause");
}
void sorting()
{
    struct node *temp1,*temp2;
    int temp;
    temp1=first;
    do
    {
       for(temp2=temp1->rlink;temp2!=first;temp2=temp2->rlink)
       {
           if(temp1->info>temp2->info)
           {
               temp=temp1->info;
               temp1->info=temp2->info;
               temp2->info=temp;

           }
       }
       temp1=temp1->llink;


    }
    while(temp1!=first);


}
I hope this post is useful for you.
plzzz like and follow my facebook page.
              

























Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

javaScript Calculator.