17 - Data Structure and Algorithms - Queue
17 - Data Structure and Algorithms - Queue
Queue
Representation of Queues
Similar to the stack ADT, a queue ADT can also be implemented using arrays,
linked lists, or pointers. As a small example in this tutorial, we implement
queues using a one-dimensional array.
Basic Operations
Queue uses two pointers − front and rear. The front pointer accesses the data
from the front end (helping in enqueueing) while the rear pointer accesses
data from the rear end (helping in dequeuing).
Algorithm
1 − START
4 − If the queue is not full, increment rear pointer to point the next empty
space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END
Example
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int itemCount = 0;
bool isFull(){
bool isEmpty(){
return itemCount == 0;
int removeData(){
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
intArray[++rear] = data;
itemCount++;
int main(){
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
Output
Queue: 3 5 9 1 12 15
Algorithm
1 – START
4 − If the queue is not empty, access the data where front is pointing.
6 − Return success.
7 – END
Example
CC++JavaPython
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int itemCount = 0;
bool isFull(){
bool isEmpty(){
return itemCount == 0;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
intArray[++rear] = data;
itemCount++;
int removeData(){
if(front == MAX) {
front = 0;
itemCount--;
return data;
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
Output
Queue: 3 5 9 1 12 15
Element removed: 3
Updated Queue: 5 9 1 12 15
Algorithm
1 – START
3 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int itemCount = 0;
int peek(){
return intArray[front];
bool isFull(){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
intArray[++rear] = data;
itemCount++;
}
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
Output
Queue: 3 5 9 1 12 15
Element at front: 3
Algorithm
1 – START
2 – If the count of queue elements equals the queue size, return true
4 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
bool isFull(){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
intArray[++rear] = data;
itemCount++;
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
printf("\n");
if(isFull()) {
printf("Queue is full!\n");
Output
Queue: 3 5 9 1 12 15
Queue is full!
The isEmpty() operation verifies whether the stack is empty. This operation is
used to check the status of the stack with the help of top pointer.
Algorithm
1 – START
4 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int itemCount = 0;
bool isEmpty(){
return itemCount == 0;
int main(){
int i;
printf("Queue: ");
printf("\n");
if(isEmpty()) {
printf("Queue is Empty!\n");
Output
Queue: 0 0 0 0 0 0
Queue is Empty!
Implementation of Queue
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int itemCount = 0;
int peek(){
return intArray[front];
bool isEmpty(){
return itemCount == 0;
bool isFull(){
int size(){
return itemCount;
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
intArray[++rear] = data;
itemCount++;
int removeData(){
if(front == MAX) {
front = 0;
itemCount--;
return data;
int main(){
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
// front : 0
// rear : 4
// ------------------
// index : 0 1 2 3 4
// ------------------
// queue : 3 5 9 1 12
insert(15);
// front : 0
// rear : 5
// ---------------------
// index : 0 1 2 3 4 5
// ---------------------
// queue : 3 5 9 1 12 15
if(isFull()) {
printf("Queue is full!\n");
// front : 1
// rear : 5
// -------------------
// index : 1 2 3 4 5
// -------------------
// queue : 5 9 1 12 15
insert(16);
// front : 1
// rear : -1
// ----------------------
// index : 0 1 2 3 4 5
// ----------------------
// queue : 16 5 9 1 12 15
insert(17);
insert(18);
// ----------------------
// index : 0 1 2 3 4 5
// ----------------------
// queue : 16 5 9 1 12 15
printf("----------------------\n");
printf("index : 5 4 3 2 1 0\n");
printf("----------------------\n");
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
Output
Queue is full!
Element removed: 3
Element at front: 5
----------------------
index : 5 4 3 2 1 0
----------------------
Queue: 5 9 1 12 15 16