Queues in C
Queue
A queue is a useful data structure in programming. It is similar to the ticket queue
outside a cinema hall, where the first person entering the queue is the first person
who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item
that comes out first.
•Front: the index where the first element is stored in the array representing the queue.
•Rear: the index where the last element is stored in an array representing the queue.
Basic Operations on Queue:
Some of the basic operations for Queue in Data Structure are:
qinsert() – Insertion of elements to the queue.
qdelete() – Removal of elements from the queue.
isFull() – Validates if the queue is full.
isEmpty() – Checks if the queue is empty.
Types of Queues
There are four different types of queues:
Simple Queue
Circular Queue
Priority Queue
Double Ended Queue
Applications of Queue
Simple Queue
INSERT Operation
check if the queue is full
for the first element, set the value of FRONT to 0
increase the REAR index by 1
add the new element in the position pointed to by REAR
DELETE Operation
check if the queue is empty
return the value pointed by FRONT
increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
#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");
}
}
Circular Queue
There was one limitation in the
array implementation of Queue.
If the rear reaches to the end
position of the Queue then there
might be possibility that some
vacant spaces are left in the
beginning which cannot be
utilized.
So, to overcome such limitations,
the concept of the circular queue
was introduced.
A Circular Queue is an extended version of
a normal queue where the last element of
the queue is connected to the first element
of the queue forming a circle.
•Front: It is used to get the front element from the Queue.
•Rear: It is used to get the rear element from the Queue.
•insert(value): This function is used to insert the new
value in the Queue. The new element is always inserted
from the rear end.
•delete(): This function deletes an element from the
Queue. The deletion in a Queue always takes place from
the front end.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 5
int front=0,rear=-1,count=0,it1;
char cqueue[MAX],element;
void insert();
void delete();
void display();
void main()
{
int choice;
while(1)
{
printf("\n program to illustrate operations on CIRCULAR QUEUE of characters\n");
printf("\n1 =>insert an element on to CIRCULAR QUEUE”);
printf(“\n 2 =>delete an element from CIRCULAR QUEUE”);
printf(“\n 3=>display the status of CIRCULAR QUEUE”):
printf(“\n 4 =>exit");
printf(“Enter Your Choice”);
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:return;
}
}
}
void insert()
{
if(count==MAX)
{
printf("CIRCULAR QUEUE is full, elements can not be inserted\n");
return;
}
rear=(rear+1)%MAX;
printf("\n enter the element to be inserted into the CIRCULAR QUEUE\n");
element=getche();
cqueue[rear]=element;
count++;
}
void delete()
{
if(count==0)
{
printf("CIRCULAR QHEUE is empty,no element to delete\n");
return;
}
it1=cqueue[front];
front=(front+1)%MAX;
printf("the element deleted is %c\n",it1);
count -=1;
}
void display()
{
int i;
if(count==0)
{
printf("CIRCULAR QUEUE is empty , no element to display\n");
return;
}
printf("CIRCULAR QUEUE contants are\n");
for(i=front;i<=count;i++)
printf("%c\n",cqueue[i]);
}