How To Implement Queue in C?
Tags: queue, queuecode
Published: 2021-05-07 11:30:49

A Queue is a linear data structure that stores a collection of elements. The queue operates on first in first out (FIFO) algorithm. This article will help you explore Queue In C.

Analogy For Queue
You are visiting a doctor for a check-up. There are many people at the clinic. A lady is entering the names of all the people in a file. The person who comes first gets places first. When the doctor is free, he calls the first patient inside. This is a queue and follows a first in first out method as the first person to enter his name in the list gets treated first.

The people who are treated their names are removed from the list. This is how a queue works.

There are 2 pointers, the front is at the front of the queue and rear is at the back of the queue. We add elements from the back of the queue and remove them from the front of the queue.

Moving on with this article on Queue In C,

Operations On A Queue
Enqueue- adding an element in the queue if there is space in the queue.
Dequeue- Removing elements from a queue if there are any elements in the queue
Front- get the first item from the queue.
Rear- get the last item from the queue.
isEmpty/isFull- checks if the queue is empty or full.
Applications

A queue is used in scheduling processes in a CPU.
It is used in data transfer between processes.
It holds multiple jobs which are then called when needed.

Sample Code For Queue In C:(for 1,2,3 line of code include '#')

include<stdio.h>
include<stdlib.h>
define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
}
}
}
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow n");
else
{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}
Explanation

This code is a menu-driven implementation of a queue. First, define the size of MAX variable to be 50. Then, the array called queue_array is declared of size MAX. There are three functions that are to be declared. The functions are, insert, display and delete functions. A menu-driven main function is used. The user is asked to enter his choice and call the appropriate function to perform the task.

There are 2 pointers, the front is at the front of the queue and rear is at the back of the queue. We add elements from the back of the queue and remove them from the front of the queue.

Limitations Of This Implementation
Consider a queue, with size 5. We have entered 5 elements but have later deleted first 2 elements. Now there is a problem. We have free space but, space won’t be used as we can’t traverse again. This problem is solved using the circular queue.