Circular Queue using linked list

   /* Program to implement circular queue using linked list */

    #include
    #include
    #define new1 (nd *)malloc(sizeof(nd))
    typedef struct queue
    {
        int info;
        struct queue *next;
    }nd;   
    void display(nd **ptr,nd **rear)
    {
        nd *t=*ptr;   
        if(*ptr==NULL)
            printf("\nQueue is empty...\n");
        else
        {   
            printf("\nQueue  :    ");
            while(t!=*rear)
            {
                printf(" %d --> ",t->info);
                t=t->next;
            }
            printf("\b\b\b\b   ");
        }
        printf("\n");
    }
    void insert(nd **ptr,nd **rear)
    {
        nd *t=*ptr;
        (*rear)->next=*ptr;
        if(*ptr==NULL)
        {
            *ptr=new1;
            printf("\nEnter 1st element : ");
            scanf("%d",&(*ptr)->info);
            (*ptr)->next=*rear;
        }
        else
        {
            nd *t1=new1;
            printf("\nEnter element  : ");
            scanf("%d",&t1->info);
            while(t->next!=*rear)
                t=t->next;
            t1->next=*rear;
            t->next=t1;
        }
    }
    void delete(nd **ptr,nd **rear)
    {
        nd *c=*ptr,*t=*ptr;
        if(*ptr==NULL)
            printf("\nQueue is empty...\n");
        else if(t->next==*rear)        // If head contains the address of "rear" pointer
        {
            *ptr=NULL;
            free(t);
        }
        else
        {
            *ptr=c->next;
            free(c);
        }
    }
    main()
    {
        nd *h=NULL;
        nd *rear=new1;
        printf("\nProgram to implement circular queue using linked list : \n");
        int ch;
        do
        {
            printf("\n[1] Insert  [2] Delete [3] Display [4] Exit : \n");
            printf("\nEnter your choice : ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1:
                    insert(&h,&rear);
                    display(&h,&rear);
                    break;
                case 2:
                    delete(&h,&rear);
                    display(&h,&rear);
                    break;
                case 3:
                    display(&h,&rear);
                    break;
                case 4:
                    break;
                default : printf("\nEnter correct choice : \n");
                      break;
            }
        }while(ch!=4);
    }

Top