Implementation of Queue using arrays | Queue insertion and deletion operations

05 min read

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 overflow
Print “Queue Overflow”
end Algorithm_Enqueue
end if
if (FRONT == -1)   //Inserting an element in an empty queue
FRONT = REAR = 0
end if
else
 REAR = REAR +1    //  Increment rear
end else
QUEUE [REAR] =  element    //    Assign the inserted element to the queue
end 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 unsderflow
Print “Underflow”
end Dequeue
end if
if(FRONT == REAR) // Deleting the only element in the queue
FRONT = REAR = -1
end if
temp = QUEUE[FRONT]
FRONT = FRONT + 1    // Increment front
delete(element) // Delete the front element
end 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 front

printf("\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

queue using arrays overflow

Dequeue operation in a queue

dequeue elements

 

Display elements in a queue

display elements in queue

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error