Search This Blog

Wednesday 13 January 2016

Circular Linked List

/* Circular Linked List*/

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

    typedef struct cll{

    int data;
    struct cll *next;

    }node;

    node *getnode()
    {

    node *temp;
    int item;
    printf("\nEnter data:");
    scanf("%d",&item);
    temp=(node *)malloc(sizeof(node));
    temp->data=item;
    temp->next=NULL;
    return temp;

    }

    node *create()
    {

    int ch;
    node *start,*neww,*trv;
    start=NULL;
    trv=NULL;
    do
    {
         neww=getnode();
         if(start==NULL)
         {
              neww->next=neww;
              start=neww;
              trv=start;
          }
          else
          {
               trv->next=neww;
               neww->next=start;
               trv=trv->next;
          }

    printf("\nEnter any key to add & 0 for stop:");
    scanf("%d",&ch);

    }
    while(ch!=0);
    return start;

    }

    node *insert_first(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();

    if(start==NULL)
    {
         start=neww;
         neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
         start=neww;
         }
     return start;

    }
    void insert_last(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
          neww=start;
          neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
    }

    }
    node *del_first(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
         start->next=NULL;
         free(start);
         start=NULL;
    }
    else
    {
          while(trv->next!=start)
          {
              trv=trv->next;
          }
          trv->next=start->next;
          trv=start;
          start=start->next;
          free(trv);
    }
    return start;

    }
    node *del_last(node *start)
    {

    node *trv,*temp;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
          start->next=NULL;
          free(start);
          start=NULL;
    }
    else
    {
          while((trv->next)->next!=start)
          {
               trv=trv->next;
          }
          temp=trv->next;
          trv->next=start;
          free(temp);
    }
    return start;

    }
    void display(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
        printf("\nEmpty !\n");
    }
    printf("\n%d ",trv->data);
    trv=trv->next;

    while(trv!=start)
    {
          printf("%d ",trv->data);
          trv=trv->next;
    }

    }

    void main()
    {

    int ch;
    node *s=NULL;
    clrscr();
    while(1)
    {
         printf("\n1.Create\n2.insert_first\n3.insert_last\n4.delete           first\n5.delete_last\n6.Display\n10.exit\nYour choice:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:s=create();
              break;

              case 2:s=insert_first(s);
              break;

              case 3:insert_last(s);
              break;

              case 4:s=del_first(s);
              break;

              case 5:s=del_last(s);
              break;

              case 6:display(s);
              break;

              case 10:exit(0);
              break;
    
              default:printf("\nEnter proper choice\n");
         }
    }

    }

No comments:

Post a Comment