Queue using linked list

 /* Program to implement 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)
    {   
        if(ptr==NULL)
            printf("\nQueue is empty...\n");
        else
        {
            printf("\nQueue  :   ");
            while(ptr!=NULL)
            {
                printf(" %d --> ",ptr->info);
                ptr=ptr->next;
            }
            printf("\b\b\b\b   ");
            printf("\n");
        }
    }
    void insert(nd **ptr)
    {
       
        nd *c=*ptr;
        if(*ptr==NULL)
        {
            *ptr=new1;
            printf("\nEnter the element : ");
            scanf("%d",&(*ptr)->info);
            (*ptr)->next=NULL;
        }
        else
        {
            nd *t;
            t=new1;
            printf("\nEnter the element : ");
            scanf("%d",&t->info);
            while(c->next!=NULL)
                c=c->next;
            t->next=NULL;
            c->next=t;
        }
    }
    void delete(nd **ptr)
    {
        nd *c=*ptr,*t=*ptr;
        if(*ptr==NULL)
            printf("\nQueue is empty...");
        else if(t->next==NULL)        // If queue contains single node
        {
            *ptr=NULL;
            free(t);
        }
        else
        {
            *ptr=c->next;
            free(c);
        }
    }
    main()
    {
        nd *h=NULL;
        printf("\nProgram to implement 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);
                    display(h);
                    break;
                case 2:
                    delete(&h);
                    display(h);
                    break;
                case 3:
                    display(h);
                    break;
                case 4:
                    break;
                default : printf("\nEnter correct choice : \n");
                      break;
            }
        }while(ch!=4);
    }        

Top