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.
In this article, we'll learn how to implement the queue data structure in the C programming language. We will also look at some of its basic operations along with their time and space complexity analysis.
Implementation of a Queue in C
We can implement a queue in C using either an array or a linked list. In this article, we will use the array data structure to store the elements. The insertion in the queue is done at the back of the queue and the deletion is done at the front. So we maintain two index pointers front and rear pointers to keep track of the front and back of the queue. The queue consists of two basic operations enqueue which adds elements to the queue (insertion) from the rear pointer and dequeue(deletion) which removes elements from the queue through the front pointer.

Representation of Stack in C
In C, the queue can be represented as the structure that contains one array of fixed size, index pointer to the front, and index pointer to the end.
struct Queue {
type arr[MAX_SIZE];
int back;
int front;
}The front pointer initial value will be -1 and the back pointer initial value will be 0. The front pointer will always point to one element before the element that is to be dequeue while the back pointer will point to the next empty element after the element that is just dequeued.
Basic Operations of Queue in C
Following are the basic operations of a Queue that are used frequently to manipulate the elements present inside the queue:
Operation
| Description
| Time Complexity
| Space Complexity
|
|---|
Enqueue
| Inserts an element in the queue through rear pointer.
| O(1)
| O(1)
|
|---|
Dequeue
| Removes an element from the queue through front pointer.
| O(1)
| O(1)
|
|---|
Peek
| Returns the front element of the queue.
| O(1)
| O(1)
|
|---|
IsFull
| Returns true is the queue is full otherwise returns false.
| O(1)
| O(1)
|
|---|
IsEmpty
| Returns true is the queue is empty otherwise returns false. | O(1)
| O(1) |
|---|
Just like stack, the queue also offers all its operation in constant time. Let’s see how to implement these basic operations for our queue in C.
1. isFull Function
The isFull function will check whether the queue is full or not. As rear will always be pointing to the leftmost empty element, if the rear gets greater than or equal to the MAX_SIZE, it means that it already contains the maximum possible number of elements.
Algorithm of isFull Function
Following is the algorithm for isFull Function:
- If rear == MAX_SIZE, return true.
- Else, return false.
2. isEmpty Function
The isEmpty function will check whether the queue is empty or not. When we initialize a queue, we set the front = -1 and rear = 0. So we know that when there are no elements in the queue, the front = rear - 1.
Algorithm of isEmpty Function
Following is the algorithm for isFull Function:
- If front == rear - 1, return true.
- Else, return false
3. Enqueue Function
The enqueue functions inserts an element to the queue through the rear pointer. We need to check for queue overflow (queue already full) before adding the new element.
Algorithm of Enqueue Function
Following is the algorithm for the enqueue function:
- Check whether the queue is full.
- If the queue is full, display the overflow message.
- If the queue is not full, add the new element to the position pointed to by the rear pointer.
- Increment the rear pointer.
4. Dequeue Function
The enqueue functions removes an element from the front of the queue through the front pointer. We need to check for the queue underflow (queue is already empty) condition before trying to dequeu the front element.
Algorithm of Dequeue
Following is the algorithm for the dequeue function:
- Check whether the queue is empty.
- If the queue is empty, display the underflow message.
- If the queue is not empty,
- Increment the front pointer of the queue.
- remove the element at the front position.
5. Peek Function
The peek functions returns the front most element of the queue. If the queue is empty it returns -1.
Algorithm of Peek Function
Following is the algorithm for the dequeue function:
- Check if the queue is empty.
- If the queue is empty, return -1.
- If not, return queueArray[front + 1].
C Program To Implement a Queue
The following program demonstrates how we can implement a Queue in C:
C
// 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;
}
OutputCurrent 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
Applications of Queue
Following are some common applications of the queue data structure:
- Queues are used in CPU scheduling .
- They are used in Print spooling.
- They are used in Breadth-first-search.
- They are used in web servers to schedule incoming requests.
- They are used in Buffering I/O systems.
Limitations of Queue
Following are the major limitation of the queue:
- Insertion and removal except from the end takes O(N) time.
- Seaching is an expensive operation again taking O(N) time.
Conclusion
In C, there is no build in data structure so knowing how to implement queue not only improves our efficiency in the language but also helps us to understand the queue data structure from the base. This article covered the basic implementation of the queue along with its basic operations and also discussed is limitations and referred the article with the solution to that limitation.
Related Articles:
These are some articles that you may want to read to improve your understanding about queue:
Similar Reads
Queue in C
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. In this article, we'll learn how to imple
7 min read
Queue in Scala
A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and a mutable queue. A mutable queue can be updated or extended in place. It means one can change, add, or remove elements of a queue as a side effect. Immutable queue, by contrast, never change. In Scala, Q
3 min read
Queue meaning in DSA
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in the First In First Out (FIFO) order. Characteristics of Queue:The first item added to the queue is the first one to be processed (or can be firstly deleted/removed), and subsequent items are p
3 min read
Circular Queue in Python
A Circular Queue is a kind of queue that can insert elements inside it dynamically. Suppose, in a given array there is not any space left at the rare but we have space at the front in the normal queue it is not possible to insert elements at the front but in the case of a circular queue we can do th
3 min read
FIFO Principle of Queue
FIFO stands for "First In, First Out". This principle dictates that the first element added to the queue is the first one to be removed. Understanding the FIFO principle is crucial for anyone working in computer science, especially when dealing with queues. It ensures that the first task or item you
2 min read
Queue using Linked List in C
Queue is a linear data structure that follows the First-In-First-Out (FIFO) order of operations. This means the first element added to the queue will be the first one to be removed. There are different ways using which we can implement a queue data structure in C. In this article, we will learn how
7 min read
Perl | Implementing a Queue
Prerequisite: StackQueue in Perl is a linear abstract data structure which follows the FIFO (First In First Out) order. It resembles the properties of the queue which we encounter in our daily life where the person who comes first will be served first. It is open at both ends. Unlike stack where the
10 min read
Input Restricted Queue
An input restricted queue is a special case of a double-ended queue where data can be inserted from one end(rear) but can be removed from both ends (front and rear). This kind of Queue does not follow FIFO(first in first out): Operations on Input Restricted Queue: Mainly the following three basic op
12 min read
Implement thread-safe queue in C++
What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr
3 min read
IPC using Message Queues
A Message Queue is a linked list of messages stored within the kernel and identified by a message queue identifier. A new queue is created or an existing queue is opened by msgget(). New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer type field, a non
5 min read
C++ STL - Reverse Priority Queue
A priority Queue is an abstract data type that resembles a queue, and each element has a corresponding priority value. Priority queues are built on the top of the max heap and use an array or vector as an internal structure. In C++ STL, by default, the first element of the queue is the greatest of a
2 min read
Task Queues - System Design
Task queues are an essential component in system design, allowing applications to handle tasks asynchronously. This means that instead of processing everything at once, tasks can be organized and executed in an orderly manner, improving performance and reliability. By using task queues, developers c
9 min read
Signals in C language
Prerequisite: Fork system call, Wait system call. A signal is a software-generated interrupt sent to a process by the OS when the user presses Ctrl-C or another process sends a signal to this process. There is a fixed set of signals that can be sent to a process. Signals are identified by integers.
5 min read
C++ Implementation Double-Ended Queue
A Double-Ended Queue (Deque) is an abstract data structure that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail). It is also often called a head-tail linked list. In this article, we will learn how to implement a Deque in C++ along with i
9 min read
Dead Letter Queue - System Design
A Dead Letter Queue (DLQ) is an essential component in message queuing systems. It serves as a holding area for messages that cannot be delivered or processed due to errors. By isolating these messages, a DLQ prevents them from disrupting the main queue's flow. This separation ensures that system pe
9 min read
Find Largest element in a Queue
Given a queue of integers, the task is to write a program that efficiently finds the largest element in that queue. Return -1 if the queue is empty. Examples: Input: Queue = {15, 27, 18}Output: 27Explanation: Among 15(front), 27 and 18(back), 27 is the largest. Input: Queue = {12, 25, 29, 16, 32}Out
5 min read
C Program to Implement Priority Queue
Priority queue is an abstract data type(ADT) in the computer science which is designed to the operate much like the regular queue except that each element has the certain priority. The priority can determines the order in which elements are dequeued - elements with the higher priority are removed fr
6 min read
C++ Program to Implement Circular Queue
In C++, Queues are a fundamental data structure in computer science which works on the principle of FIFO (First In, First Out). They can be implemented using both array and linked list. A circular queue is a type of queue in which the last element is connected to the first element, forming a circula
7 min read
C Program to Implement Circular Queue
A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last position back to the first, forming a circle. In this article, we will learn how to implement circular queue in C programming language. What is a Circular Queue in C?In a circular
5 min read