C Program to Implement Queue using Array
Last Updated :
27 May, 2024
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C.
Implement Queue using Array in C
To implement Queue using Array in C, we can follow the below approach:
Approach:
- Define a structure consisting of an array and two pointers front and rear.
- Initialize the array with MAX_SIZE.
- Initialize both the front and rear pointers to -1.
- The insertion of elements will take place through the rear pointer and the deletion of elements will take place through the front pointer.
- Implement isFull, isEmpty, Enqueue, and Dequeue functions to manipulate the elements of the queue easily.
Representation of Queue in C
The queue will be represented as a structure of fixed size array which consists of two pointers front and rear. The fixed size array will store the elements of the queue and the front and rear pointers will help the users to manipulate the queue elements.
struct Queue {
int queue[MAX_SIZE];
int front;
int rear;
};
The user can define the maximum size of the array as per their requirements and a utility function can be used to initialize the front and the rear pointers to -1.
Basic Operations of Queue
Following are the basic operations of the Queue data structure which are required to manipulate the elements present inside the Queue.
Operation
| Description
| Time Complexity
| Space Complexity
|
---|
Enqueue
| Inserts an element at the end of the queue using the rear pointer.
| O(1)
| O(1)
|
---|
Dequeue
| Deletes an element from the front of the queue using the front pointer.
| O(1)
| O(1)
|
---|
IsEmpty
| Checks if the queue is empty or not. If front ==-1 returns true.
| O(1)
| O(1)
|
---|
IsFull
| Checks if the queue is full or not . If rear==MAX_SIZE-1 returns true.
| O(1)
| O(1)
|
---|
Now let's see how we can implement the basic functions of queue in C:
1. Enqueue Function
The enqueue function will insert an element at the end of the queue using the rear pointer. Following is the algorithm for enqueue function:
Algorithm for Enqueue Function
1. Check if the queue is full
2. If the queue is empty, set front pointer to 0.
3. Increment rear pointer and add the element at the rear position in the queue array.
2. Dequeue Function
The dequeue function will delete an element from the front of the queue using the front pointer. Following is the algorithm for enqueue function:
Algorithm for Dequeue Function
- Check if the queue is empty.
- If not, store the data at the front position of the queue.
- If front pointer is equal to rear, reset both pointers to -1, indicating an empty queue.
- Otherwise, increment the front pointer to point to the next element.
3. IsEmpty Function
The isEmpty function returns true if the queue is empty otherwise it returns false. Following is the algorithm for the isEmpty function:
Algorithm for IsEmpty Function
- Check if front pointer is equal to -1
- Return true if front==-1 .
- Return false if front!=-1.
4. IsFull Function
The isFull function returns true if the queue is full otherwise it returns false.The queue is full when the rear pointer points to the last index of the array. Following is the algorithm for the isFull function:
Algorithm for IsFull Function
- Check if rear pointer is equal to MAX_SIZE - 1.
- Return true if rear==MAX_SIZE -1 .
- Return false if rear != MAX_SIZE-1.
C Program to Implement Queue using Array
The following program illustrates how we can implement the queue data structure using arrays in C:
C
// C Program to implement queue using arrays
#include <stdio.h>
// Define the maximum size for the queue
#define MAX_SIZE 100
// Define a structure for the queue
struct Queue {
int queue[MAX_SIZE];
int front;
int rear;
};
// Function to initialize the queue
void initializeQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
// Function to check if the queue is empty
int isEmpty(struct Queue *q) {
return (q->front == -1);
}
// Function to check if the queue is full
int isFull(struct Queue *q) {
return (q->rear == MAX_SIZE - 1);
}
// Function to insert an element into the queue
void enqueue(struct Queue *q, int data) {
if (isFull(q)) {
printf("Queue is full\n");
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->rear++;
q->queue[q->rear] = data;
printf("Enqueued %d in queue\n", data);
}
// Function to remove an element from the queue
int dequeue(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
int data = q->queue[q->front];
// If the queue is empty reset the pointers
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front++;
}
printf("Deleted element: %d\n", data);
return data;
}
// Function to display the elements of the queue
void display(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
}
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->queue[i]);
}
printf("\n");
}
int main() {
// Initialize a queue
struct Queue q;
initializeQueue(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
printf("Elements in the queue after enqueue operation: ");
display(&q);
dequeue(&q);
printf("Elements in the queue after dequeue operation: ");
display(&q);
return 0;
}
OutputEnqueued 1 in queue
Enqueued 2 in queue
Enqueued 3 in queue
Elements in the queue after enqueue operation: 1 2 3
Deleted element: 1
Elements in the queue after dequeue operation: 2 3
Applications of Queue
The queue data structure has various applications in different domains of computer science. Some of the applications are:
Conclusion
In the following article we have learned about the queue data structure and how we can implement it using arrays in C. We have learnt about the basic operations which are required in a queue data structure to manipulate the elements of the queue. We have also learned about the various applications of queue data structure in the computer science domain.
Related Articles
These are some articles that you may want to read to improve your understanding about queue:
Similar Reads
Priority Queue using array in C++
Priority Queue is an extension of the Queue data structure where each element has a particular priority associated with it. It is based on the priority value, the elements from the queue are deleted. Operations on Priority Queue: enqueue(): This function is used to insert new data into the queue.deq
4 min read
C Program to Implement Min Heap
In this article, we will learn the implementation of min heap in C programming language. A heap is a data structure like a tree with some special properties. The basic requirement of the heap is that the value of a node must be greater than equal to (or smaller than equal to) the value of its childr
10 min read
C Program to Implement Max Heap
In this article, we will learn the implementation of the max heap in the C programming language. A heap is a data structure like a tree with some special properties. The basic requirement of the heap is that the value of a node must be greater than equal to (or smaller than equal to) the value of it
10 min read
Stack and Queue C/C++ Programs
The stack and queue are popular linear data structures with a wide variety of applications. The stack follows LIFO (Last In First Out) principle where the data is inserted and extracted from the same side. On the other hand, the queue follows FIFO (First In First Out) principle, i.e., data is insert
3 min read
C Program To Remove Duplicates From Sorted Array
In this article, we will learn how to remove duplicates from a sorted array using the C program. The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are a
4 min read
Array within Structure in C
In C, a structure is a user-defined data type that allows us to combine data of different data types. While an array is a collection of elements of the same type. In this article, we will discuss the concept of an array that is declared as a member of the structure. Array within a Structure An array
3 min read
C Program to Traverse an Array
Write a C program to traverse the given array that contains N number of elements. Examples Input: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3 Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1 Different Ways to Traverse an Array in CArrays are versatile data structures and C language
3 min read
Return an Array in C
In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C
6 min read
Array of Structures in C
An array of structures is simply an array where each element is a structure. It allows you to store several structures of the same type in a single array. Let's take a look at an example: [GFGTABS] C #include <stdio.h> #include <string.h> // Structure definition struct A { int var; }; in
6 min read
Array of Pointers to Strings in C
In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
2 min read