Implementing a Queue in C using Arrays: A Step by Step Guide (2024)

Implementing a Queue in C using Arrays: A Step by Step Guide (3)

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. Queues are commonly used in computer science for tasks such as scheduling and managing processes. In this blog post, we will learn how to implement a queue using an array in the C programming language.

Before we begin, it is important to understand the basic operations that can be performed on a queue. These include:

  • enqueue: adding an element to the rear of the queue
  • dequeue: removing an element from the front of the queue
  • peek: viewing the element at the front of the queue
  • isFull: check if the queue is full
  • isEmpty: check if the queue is empty

To implement a queue using an array, we first need to define the size of the array and initialize it. We will also need to keep track of the front and rear indices of the queue, as well as a variable to store the number of elements currently in the queue. Here is an example of how this can be done in C:

#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = 0;
int rear = -1;
int count = 0;

Next, we can implement the enqueue function. This function will add an element to the rear of the queue and update the rear index accordingly. It is important to check if the queue is full before adding an element to prevent overflow. Here is an example of how this can be done in C:

void enqueue(int element) {
if (isFull()) {
printf("Error: Queue is full\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = element;
count++;
}

The dequeue function will remove an element from the front of the queue and update the front index accordingly. It is important to check if the queue is empty before removing an element to prevent underflow. Here is an example of how this can be done in C:

int dequeue() {
if (isEmpty()) {
printf("Error: Queue is empty\n");
return -1;
}
int element = queue[front];
front = (front + 1) % MAX_SIZE;
count--;
return element;
}

The peek function simply returns the element at the front of the queue without removing it. Here is an example of how this can be done in C:

int peek() {
if (isEmpty()) {
printf("Error: Queue is empty\n");
return -1;
}
return queue[front];
}

The isFull and isEmpty functions check if the queue is full or empty respectively. Here is an example of how these can be implemented in C:

bool isFull() {
return count == MAX_SIZE;
}
bool isEmpty() {
return count == 0;
}

With these basic functions in place, we now have a working queue implemented using an array in the C programming language. This is just a basic example and can be further expanded upon to include additional functionality or error handling.

In conclusion, implementing a queue using an array in the C programming language is a relatively simple task. By understanding the basic operations of a queue and utilizing an array to store the elements, we can create a functional and efficient queue data structure. However, it’s important to keep in mind that this is just a basic example, and there are many more advanced implementations and optimization techniques that can be used to improve the performance of this data structure. Furthermore, it’s important to make sure that the program is well tested, and has proper error handling mechanism to avoid any unexpected crashes.

Full Code here

#include<stdio.h> 
#include<stdlib.h>
void insert();
void dequeue();
void display();
int front = -1, rear = -1 ,maxsize;
int queue[100];
int main ()
{
int choice;
printf("\n Enter the size of QUEUE : ");
scanf("%d",&maxsize);
printf("\n QUEUE OPERATIONS USING ARRAY");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");
while(choice != 4)
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
return 0;
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");

}
void dequeue()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;

}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}

}

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n Elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
}
}

Implementing a Queue in C using Arrays: A Step by Step Guide (2024)
Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5922

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.