# data structure: Queue

Posted By on December 15, 2014

A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court of the canteen. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item.

In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.

implementation of queue in c::

#include<stdio.h>
#include<conio.h>
#include<process.h>

void main()
{
int a[5],i,f=-1,r=-1,c;
clrscr();
do{
printf(“nEnter the operation you want to perform:”);
printf(“1:insert.”);
printf(“2:delete.”);
printf(“3:display.”);
printf(“4:exit.”);
scanf(“%d”,&c);

switch(c)
{
case 1:printf(“insert.”);
if(r==4)
{
printf(“queue is full.”);
}
else{
f=0; r++;
scanf(“%d”,&a[r]);
}
break;

case 2:
if(f==-1 || f==r+1)
{
printf(“The queue is empty.”);
}
else
{
printf(“the deleted element is:%d”,a[f]);
f++;
}
break;

case 3: printf(“The queue is:”);
if(f!=-1 || f!=r+1)
{
for(i=f; i<=r; i++)
{printf(“%d “,a[i]); }
}
else
{
printf(“empty.”);
break;

}

case 4: exit(0);
break;

default:printf(“invalid choice.!!!”);
}
}while(c!=4);
getch();
}