Queue Data Structure

A queue is a linear data structure where insertion of element is done from one end and deletion from the other end. The queue is like a line of person at polling booth where the first person in the line is first voted. This is called First-In-First-Out (FIFO). Insertion of the element is done from the one end called FRONT end and deletion is done from the other end called REAR.


Data Structure Queue

Queue Operations

These are the operations performed by a queue.

  • Enqueue - The element to add in a queue.
  • Dequeue - The element to delete in a queue.
  • Peek - Retrieve the element from the front end of the queue without removing it from the queue.

Insertion in a Queue

The element insertion in a queue can be performed in two ways, either using Arrays or using Pointer.

Algorithm to Insert Element in a Queue

Step 1: IF REAR = MAX-1
	print('Queue Overflow')
	return
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT

In the above algorithm, we have checked the existence of space in the queue. If there is no space then print 'Queue Overflow' and the code is exited and if not then, we check the element at the FRONT and REAR, if both are empty then set both to 0 else increase REAR with one and insert element from the REAR end and return.


Algorithm to Delete Element in a Queue

Step 1: IF FRONT = -1 OR FRONT > REAR
	print('Queue Underflow')
	return
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT

In the above algorithm, we have checked the existence of elements in the queue. If there is no any element, then print 'Queue Underflow' and the code is exited and if not, then FRONT is increased by one and point to the next element in the queue.





Queue Program in C

#include <stdio.h>
 
#define MAX 30
 
void insert_queue();
void delete_queue();
void display_queue();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
    int che;
    while (1)
    {
        printf("\nEnter 1 to insert element to queue \n");
        printf("\nEnter 2 to delete element from queue \n");
        printf("\nEnter 3 to display all elements of queue \n");
        printf("\nEnter 4 to exit \n");
        printf("Enter your choice : ");
        scanf("%d", &che);
        switch (che)
        {
            case 1:
            insert_queue();
            break;
            case 2:
            delete_queue();
            break;
            case 3:
            display_queue();
            break;
            case 4:
            exit(1);
            default:
            printf("Invalid input \n");
        } 
    } 
} 
 
void insert_queue()
{
    int add_item;
    if (rear == MAX - 1)
    printf("QUEUE OVERFLOW \n");
    else
    {
        if (front == - 1)
        front = 0;
        printf("Enter value to inset in queue : ");
        scanf("%d", &add_item);
        rear = rear + 1;
        queue_array[rear] = add_item;
    }
} 
 
void delete_queue()
{
    if (front == - 1 || front > rear)
    {
        printf("QUEUE UNDERFLOW \n");
        return ;
    }
    else
    {
        printf("Element deleted from queue : %d\n", queue_array[front]);
        front = front + 1;
    }
} 
 
void display_queue()
{
    int i;
    if (front == - 1)
        printf("EMPTY QUEUE \n");
    else
    {
        printf("Displaying Queue : \n");
        for (i = front; i <= rear; i++)
            printf("%d ", queue_array[i]);
        printf("\n");
    }
} 

Output of the above program:


Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 1
Enter value to inset in queue : 45

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 1
Enter value to inset in queue : 89

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 1
Enter value to inset in queue : 24

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 3
Displaying Queue : 
45 89 24 

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 2
Element deleted from queue : 45

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 3
Displaying Queue : 
89 24 

Enter 1 to insert element to queue 

Enter 2 to delete element from queue 

Enter 3 to display all elements of queue 

Enter 4 to exit 
Enter your choice : 






Read more articles


General Knowledge



Learn Popular Language