# Implementation of Queue using arrays | Queue insertion and deletion operations

Queue data structure implementation can be done using arrays and linked lists. Now, let us see the implementation of queue using arrays.

## Enqueue and dequeue operations using arrays

### enqueue()

1. Check if the queue is full.

2. If the queue is full, then print "Queue overflow" and exit.

3. Else increment REAR by 1.

4. Assign QUEUE [ REAR ] = ELEMENT.

### Algorithm_Enqueue

`if ((FRONT == 0) and (REAR == N - 1)) // Condition for overflowPrint “Queue Overflow”end Algorithm_Enqueueend ifif (FRONT == -1)   //Inserting an element in an empty queueFRONT = REAR = 0end ifelse REAR = REAR +1    //  Increment rearend elseQUEUE [REAR] =  element    //    Assign the inserted element to the queueend Enqueue`

### dequeue()

1. Check if the queue is empty.

2. If the queue is empty, the print "Queue underflow".

3. Copy the element at the front of the queue to some temporary variable, TEMP = QUEUE[ FRONT ].

4. Increment FRONT by 1.

5. Print temp and delete it.

### Algorithm_Dequeue

`if(FRONT == -1)  // Condition for unsderflowPrint “Underflow”end Dequeueend ifif(FRONT == REAR)  // Deleting the only element in the queueFRONT = REAR = -1end iftemp = QUEUE[FRONT] FRONT = FRONT + 1    // Increment frontdelete(element)   // Delete the front elementend Dequeue`

Now, let us see a program for array implementation of queue data structure

## C program for implementing queue using array in C

`#include <stdio.h>#include <stdlib.h>/* Structure for queue with array size and pointers */struct queue{ int s[3]; int front,rear;}st;/* Function to check if the queue is full */int full(){ if(st.rear >= 2) return 1; else return 0;}/* Function to check if the queue is empty */int empty(){ if(st.front == -1) return 1; else return 0;}/* Function to insert elements in a queue */void enqueue(int num){ if(st.front == -1)    // The queue is initially empty  st.front++; st.rear++; st.s[st.rear]=num;}/* Function to delete elements in a queue */int dequeue(){ int x; x = st.s[st.front]; if(st.front==st.rear)    // The queue contains only one element  st.front=st.rear=-1;   else st.front++;   // Increment frontprintf("\nDequeued Element : %d",x);}/* Function to display the elements in the queue */void display(){ int i; if(empty()) printf("\nEMPTY QUEUE\n"); else { printf("\nQUEUE ELEMENTS : "); for(i=st.front;i<=st.rear;i++) printf("%d ",st.s[i]);}}/* Main function */int main(){ int num,choice; st.front = st.rear = -1; while(1) { printf("\n\nQUEUE OPERATIONS\n\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n\n"); scanf("%d",&choice); switch (choice) { case 1: if(full()) { printf("\nQUEUE IS FULL\n"); } else { printf("\nEnter item : "); scanf("%d",&num); enqueue(num); } break; case 2: if (empty()) { printf("\nEMPTY QUEUE\n"); } else { dequeue(); }break; case 3: display(); break; default: exit(0); }} return 0;}`

OUTPUT

Enqueue operation in a queue

Dequeue operation in a queue

Display elements in a queue

### Relevant exercises

POST A NEW COMMENT

• Input (stdin)

Output (stdout)

Input (stdin)

Expected Output

Compiler Message

Input (stdin)

`2    3`

`5`
`5`
`5`