Queue
A queue is a useful data structure in programming. It is similar to the ticket queue outside
a cinema hall, where the rst person entering the queue is the rst person who gets the
ticket.
Queue follows the First In First Out(FIFO) rule - the item that goes in rst is the item that
comes out rst too.
In the above image, since 1 was kept in the queue before 2, it was the rst to be
removed from the queue as well. It follows the FIFO rule.
In programming terms, putting an item in the queue is called an "enqueue" and removing
an item from the queue is called "dequeue".
https://www.programiz.com/dsa/queue 1/8
2/5/2018 Queue
We can implement queue in any programming language like C, C++, Java, Python or C#,
but the speci cation is pretty much the same.
1. Two pointers called FRONT and REAR are used to keep track of the rst and last
elements in the queue.
2. When initializing the queue, we set the value of FRONT and REAR to -1.
3. On enqueing an element, we increase the value of REAR index and place the new
element in the position pointed to by REAR .
4. On dequeueing an element, we return the value pointed to by FRONT and increase
the FRONT index.
5. Before enqueing, we check if queue is already full.
6. Before dequeuing, we check if queue is already empty.
7. When enqueing the rst element, we set the value of FRONT to 0.
8. When dequeing the last element, we reset the values of FRONT and REAR to -1.
https://www.programiz.com/dsa/queue 2/8
2/5/2018 Queue
https://www.programiz.com/dsa/queue 3/8
2/5/2018 Queue
Queue Implementation in programming
language
The most common queue implementation is using arrays, but it can also be implemented
using lists.
void deQueue(){
if(front == -1)
printf("\nQueue is Empty!!");
else{
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",items[i]);
}
}
https://www.programiz.com/dsa/queue 4/8
2/5/2018 Queue
Queue is Empty!!
Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is Full!!
Queue elements are:
1 2 3 4 5
Deleted : 1
Queue elements are:
2 3 4 5
};
int main()
{
Queue q;
//enQueue 5 elements
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.display();
return 0;
https://www.programiz.com/dsa/queue 5/8
2/5/2018 Queue
}
When you run this program, the output will be
Queue is empty
Inserted 1
Inserted 2
Inserted 3
Inserted 4
Inserted 5
Queue is full
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Deleted -> 1
Front -> 1
Items -> 2 3 4 5
Rear -> 4
https://www.programiz.com/dsa/queue 6/8
2/5/2018 Queue
The indexes 0 and 1 can only be used after the queue is reset when all the elements have
been dequeued.
By tweaking the code for queue, we can use the space by implementing a modi ed queue
called circular queue.
Stack
Queue
Circular Queue
Linked List
https://www.programiz.com/dsa/queue 7/8
2/5/2018 Queue
DFS algorithm
Adjacency List
Adjacency Matrix
Kruskal's Algorithm
Prim's Algorithm
Dynamic Programming
Dijkstra's Algorithm
Subscribe
ABOUT
CONTACT
ADVERTISE
https://www.programiz.com/dsa/queue 8/8