A queue is a linear data structure that follows the First In First Out (FIFO) order of insertion and deletion. It means that the element that is inserted first will be the first one to be removed and the element that is inserted last will be removed at last.
- Elements are inserted at the rear (enqueue) and removed from the front (dequeue).
- Common operations are enqueue, dequeue, peek/front, isEmpty, and isFull.
- Queues can be implemented using arrays or linked lists in C.
- Used in real-life scenarios like printer queues, CPU task scheduling, and data buffers.

// C Program to demonstrate how to Implement a queue
#include <stdbool.h>
#include <stdio.h>
#define MAX_SIZE 100
// Defining the Queue structure
typedef struct
{
int items[MAX_SIZE];
int front;
int rear;
} Queue;
// Function to initialize the queue
void initializeQueue(Queue *q)
{
q->front = -1;
q->rear = 0;
}
// Function to check if the queue is empty
bool isEmpty(Queue *q)
{
return (q->front == q->rear - 1);
}
// Function to check if the queue is full
bool isFull(Queue *q)
{
return (q->rear == MAX_SIZE);
}
// Function to add an element to the queue (Enqueue
// operation)
void enqueue(Queue *q, int value)
{
if (isFull(q))
{
printf("Queue is full\n");
return;
}
q->items[q->rear] = value;
q->rear++;
}
// Function to remove an element from the queue (Dequeue
// operation)
void dequeue(Queue *q)
{
if (isEmpty(q))
{
printf("Queue is empty\n");
return;
}
q->front++;
}
// Function to get the element at the front of the queue
// (Peek operation)
int peek(Queue *q)
{
if (isEmpty(q))
{
printf("Queue is empty\n");
return -1; // return some default value or handle
// error differently
}
return q->items[q->front + 1];
}
// Function to print the current queue
void printQueue(Queue *q)
{
if (isEmpty(q))
{
printf("Queue is empty\n");
return;
}
printf("Current Queue: ");
for (int i = q->front + 1; i < q->rear; i++)
{
printf("%d ", q->items[i]);
}
printf("\n");
}
int main()
{
Queue q;
initializeQueue(&q);
// Enqueue elements
enqueue(&q, 10);
printQueue(&q);
enqueue(&q, 20);
printQueue(&q);
enqueue(&q, 30);
printQueue(&q);
// Peek front element
printf("Front element: %d\n", peek(&q));
// Dequeue an element
dequeue(&q);
printQueue(&q);
// Peek front element after dequeue
printf("Front element after dequeue: %d\n", peek(&q));
return 0;
}
Output
Current Queue: 10 Current Queue: 10 20 Current Queue: 10 20 30 Front element: 10 Current Queue: 20 30 Front element after dequeue: 20
Problem with Above Implementation
The queue above works fine only for single usage. For example, lets fill the queue completely and then dequeue all the elements. Then, the front = rear - 1, which is the condition for the full queue even though the queue is empty. To resolve this, we implement the circular increment (or modular increment) for the index pointers. This kind of queue is called Circular Queue.
To know more, refer to the article - Introduction to the Circular Queue
Types of Queues
Queue data structure can be classified into 3 types:

1. Simple Queue
A simple queue follows the FIFO (First In, First Out) principle.
- Insertion is allowed only at the rear (back).
- Deletion is allowed only from the front.
- Can be implemented using a linked list or a circular array.
When an array is used, we often prefer a circular queue, which is mainly an efficient array implementation of a simple queue. It efficiently utilizes memory by reusing the empty spaces left after deletion, avoiding wastage that occurs in a normal linear array implementation..
2. Double-Ended Queue (Deque)
In a deque, insertion and deletion can be performed from both ends.
3. Priority Queue
A queue where each element is assigned a priority, and deletion always happens based on priority (not just position).
Queue Operations
- Enqueue: Adds an element to the end (rear) of the queue. If the queue is full, an overflow error occurs.
- Dequeue: Removes the element from the front of the queue. If the queue is empty, an underflow error occurs.
- Peek/Front: Returns the element at the front without removing it.
- Size: Returns the number of elements in the queue.
- isEmpty: Returns true if the queue is empty, otherwise false.
- isFull: Returns true if the queue is full, otherwise false.
Application of Queue

Advantages of Queue

Related Articles:
These are some articles that you may want to read to improve your understanding about queue: