Queue program #1

21. Implement Simple Queue using array that performs following operations: INSERT, DELETE, DISPLAY.





#include<stdio.h>
#define N 50
int q[N];
int f=-1;
int r=-1;

void enqueue(int y);
int dequeue();
void display();

void main()
{
     int ch,x,a;
    printf ("QUEUE OPERATION\n");
    while (1)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    INSERT               \n");
        printf ("      2    -->    DELETE               \n");
        printf ("      3    -->    DISPLAY               \n");
        printf ("      4    -->    EXIT           \n");
        printf ("------------------------------------------\n");
        printf ("Enter your choice\n");
        scanf    ("%d", &ch);
        switch (ch)
        {
        case 1:
            printf ("Enter the element to be pushed in queue:\n");
            scanf ("%d", &x);
            enqueue(x);
          
            break;
        case 2:
            a=dequeue();
            if(a!=0)
            printf("Delete=%d\n",a);
          
            break;
        case 3:
            display();
            break;
        case 4:
            return;
        default :
            printf("Unvalid entered choice, please enter choice between 1 to 4.\n");
            }
     
     }
}

void enqueue(int y)
{
     if(r>=N-1)
     {
          printf("Queue Overflow");
          return;
     }
     else
     {
          r=r+1;
          q[r]=y;
          if(f==-1)
          {
               f=0;
               return;
          }
     }
}

int dequeue()
{
     int y;
     if(f==-1)
     {
          printf("Queue Underflow\n");
          return(0);
     }
     else
     {
          y=q[f];
          if(f==r)
          {
               f=-1;
               r=-1;
          }  
          else
          {
               f=f+1;
          } 
          return(y);    
     }
}

void display()
{
     int j;
     if (f==-1)
     {
        printf ("Queue is empty\n");
     }
     else
     {
        printf ("\n The status of the queue is \n");
        for (j=f;j<=r;j++)
        {
            printf ("%d\t", q[j]);
        }
        printf("\n");
      }
}

Output

  1. QUEUE OPERATION
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed in queue:
    11
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed in queue:
    12
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed in queue:
    13
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed in queue:
    14
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed in queue:
    15
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    3

     The status of the queue is
    11    12    13    14    15   
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    2
    Delete=11
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    2
    Delete=12
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    3

     The status of the queue is
    13    14    15   
    ------------------------------------------
          1    -->    INSERT              
          2    -->    DELETE              
          3    -->    DISPLAY              
          4    -->    EXIT          
    ------------------------------------------
    Enter your choice
    4







For download this program please check the link ➡➡ Download

Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2