Implementation Of Queue Operations Using Array In C

Implementation Of Queue Operations Using Array In C

In this program implement queue operations using array

Enqueue operation is used to insert new element in queue from front.Dequeue operation remove and return element from the rear of the queue.

  • Queue follow the first-in-first-out approach(FIFO).

#include
#define N 10
int q[N];
int f=-1;
int r=-1;
//this function is used to insert data into queue
void enqueue(int val)
{
    if(r>=N-1)
        printf("Queue overflow\n");
    else if(r==-1 && f== -1) 
    {
        f=r=0;
        q[r]=val;
        printf("value inserted  : %d \n",val);
    }
    else
    {
        r++;
        q[r]=val;
        printf("value inserted  : %d \n",val);
    }
}
//this function is used to remove and return element from queue
int dequeue()
{
    int tmp=0;
    if(f==-1)
        printf("queue underflow\n");
    tmp=q[f];
    if(f==r)
        f=r=-1;
    else
        f++;
    return tmp;
}
//in this function is used for display queue element from front to rear
void display()
{
    int i;
    if(f==-1)
        printf("Queue underflow\n");
    else
    {
        for(i=f;i<=r;i++)
        {
            printf("%d t",q[i]);
        }
    }
}
int main()
{
    int ch=1,val;
    while(ch)
    {
            printf("\n---------menu---------");
            printf("\n1.Insert \n2.delete\n3.display\n4.Exit\n");
            printf("Enter choice ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1 :
                    printf("\nEnter value for insert  : " );
                    scanf("%d",&val);
                    enqueue(val);
                    break;
                case 2:
                    val=dequeue();
                    printf("\nDeleted value = %d",val);
                    break;
                case 3:
                    display();
                    break;
                case 4:
                    exit(0);
                    break ;
                default :
                    printf("Invalid choice try again ");
                    break;
            }
        }
}
Queue Operations Using Array
In this image is show the output of above program
Implementation Of Queue Operations Using Array In C

Zala Digvijaysinh

MCA student at Dharamsinh Desai University


4 thoughts on “Implementation Of Queue Operations Using Array In C”

  1. Pingback: Create Static Binary Tree And Level Order Traversal In C | MCAPROGRAMMING

  2. Pingback: Implementation Of Circular Queue Using Array Solution In C | MCAPROGRAMMING

  3. Pingback: Implementation Of Circular Queue Using Linked List Solution In C | MCAPROGRAMMING

  4. Pingback: Reverse Queue Elements Using Stack In Solution C | MCAPROGRAMMING

Leave a Comment

Your email address will not be published. Required fields are marked *