Facebook Page Twitter Page LinkedIn Page
× Data Structures Data Structures Resources


A queue is a linear dynamic data structure that follows First-In/First-Out (FIFO) principle. In a queue, addition of a new element and deletion of an element occurs at different end which implies that the element which is added first in the queue will be the first to be removed from the queue.

Features of queue

  • It is a dynamic data structure.
  • It has dynamic size.
  • It uses dynamic memory allocation.

Basic Operations of a Queue

  • isEmpty(): Checks whether the queue is empty or not.
  • size(): Returns the size of the queue.
  • frontElement(): Returns the front element of the queue. It is the element which will be dequeued next.
  • rearElement(): Returns the rear element of the queue. It is the element behind which next element will be enqueued.
  • EnQueue(x): Adds a new element ‘x’ from the rear side of the queue. Consequently, size of the queue increases by 1.
  • Queue EnQueue
  • DeQueue(): Deletes the front element of the queue. Consequently, size of the queue decreases by 1.
  • Queue DeQueue

Implementation of Queue

#include <iostream>
using namespace std;

#define MAX 100 
class CreateQueue {
  int front;
  int rear;

  public:
  //assigning MAX size of the queue
  int queue[MAX];

  CreateQueue() {
    rear = -1;
    front = -1;
  }

  void isEmpty();
  int size();
  void EnQueue(int x);
  void DeQueue();
  int frontElement();
  int rearElement();
};

  // create a function to check whether 
  // the queue is empty or not  
  void CreateQueue::isEmpty() {
    if(rear == front) {
      cout<<"Queue is empty."<<"\n";
    } else {
      cout<<"Queue is not empty."<<"\n";
    }
  }

  //create a function to return size of the queue 
  int CreateQueue::size() {
     return (rear - front);
  } 

  //create a function to add new element       
  void CreateQueue::EnQueue(int x){
    if(rear == (MAX - 1)){
      cout<<"Queue size limit reached."<<"\n";
    } else {
      queue[++rear] = x;
      cout<<x<<" is added into the queue."<<"\n";
    }
  }

  //create a function to delete front element       
  void CreateQueue::DeQueue(){
    if(rear == front){
      cout<<"Queue is empty."<<"\n";
    } else {
      int x = queue[++front];
      cout<<x<<" is deleted from the queue."<<"\n";
    }
  }  

  //create a function to get front element       
  int CreateQueue::frontElement() {
    if(rear == front) {
      cout<<"Queue is empty."<<"\n";
      return 0;
    } else {
      return queue[front+1];
    }
  }

  //create a function to get rear element       
  int CreateQueue::rearElement() {
    if(rear == front) {
      cout<<"Queue is empty."<<"\n";
      return 0;
    } else {
      return queue[rear];
    }
  }

//test the code
int main() {
  class CreateQueue MyQueue;
  MyQueue.EnQueue(10);
  MyQueue.EnQueue(20);
  MyQueue.EnQueue(30);
  MyQueue.EnQueue(40);

  MyQueue.DeQueue();
  MyQueue.isEmpty();
  return 0; 
}

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.
#include <stdio.h>

#define MAX 100 

//define structure of a queue
struct queue {
  int array[MAX];
  int front;
  int rear;
};

typedef struct queue qu;

void CreateQueue(qu *q) {
  q->front = -1;
  q->rear = -1;
}

// create a function to check whether 
// the queue is empty or not  
void isEmpty(qu *q) {
  if(q->rear == q->front) {
    printf("Queue is empty.\n");
  } else {
    printf("Queue is not empty.\n");
  }
}

//create a function to return size of the queue 
int size(qu *q) {
   return (q->rear - q->front);
} 

//create a function to add new element       
void EnQueue(qu *q, int x){
  if(q->rear == (MAX - 1)){
    printf("Queue size limit reached.\n");
  } else {
    q->array[++q->rear] = x;
    printf("%i is added into the queue.\n", x);
  }
}

//create a function to delete front element       
void DeQueue(qu *q){
  if(q->rear == q->front){
    printf("Queue is empty.\n");
  } else {
    int x = q->array[++q->front];
    printf("%i is deleted from the queue.\n", x);
  }
}  

//create a function to get rear element       
int rearElement(qu *q) {
  if(q->rear == q->front) {
    printf("Queue is empty.\n");
    return 0;
  } else {
    return q->array[q->rear];
  }
}

//create a function to get front element       
int frontElement(qu *q) {
  if(q->rear == q->front) {
    printf("Queue is empty.\n");
    return 0;
  } else {
    return q->array[q->front + 1];
  }
}


//test the code
int main() {
  qu s;
  qu *MyQueue;
  MyQueue = &s;
  CreateQueue(MyQueue);
  EnQueue(MyQueue, 10);
  EnQueue(MyQueue, 20);
  EnQueue(MyQueue, 30);
  EnQueue(MyQueue, 40);

  DeQueue(MyQueue);
  isEmpty(MyQueue);
  return 0; 
}

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.
# function to create queue
def CreateQueue():
  queue = []
  return queue

# create function to check whether 
# the queue is empty or not
def isEmpty(queue):
  if(len(queue) == 0):
    print("Queue is empty.")
  else:
    print("Queue is not empty.") 
    
#create function to return size of the queue       
def size(queue):
  return len(queue)

#create function to add new element       
def EnQueue(queue, newElement):
  queue.append(newElement)
  print(newElement, "is added into the queue.")

#create function to delete front element       
def DeQueue(queue):
  print(queue.pop(0), "is deleted from the queue.")

#create function to get front element       
def frontElement(queue):
  return queue[0]

#create function to get rear element       
def rearElement(queue):
  return queue[len(queue) - 1]

#test the code                
MyQueue = CreateQueue()

EnQueue(MyQueue, 10)
EnQueue(MyQueue, 20)
EnQueue(MyQueue, 30)
EnQueue(MyQueue, 40)

DeQueue(MyQueue)
isEmpty(MyQueue)

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.
class CreateQueue {
  static final int MAX = 100;
  int front;
  int rear;
  //assigning MAX size of the queue
  int queue[] = new int[MAX];

  CreateQueue() {
    front = -1;
    rear = -1;
  }

  // create a method to check whether 
  // the queue is empty or not  
  void isEmpty() {
    if(rear == front) {
      System.out.println("Queue is empty.");
    } else {
      System.out.println("Queue is not empty.");
    }
  }

  //create a method to return size of the queue 
  int size() {
     return (rear - front);
  } 

  //create a method to add new element       
  void EnQueue(int x){
    if(rear == (MAX - 1)){
      System.out.println("Queue size limit reached.");
    } else {
      queue[++rear] = x;
      System.out.println(x + " is added into the queue.");
    }
  }

  //create a method to delete front element       
  void DeQueue(){
    if(rear == front){
      System.out.println("Queue is empty.");
    } else {
      int x = queue[++front];
      System.out.println(x + " is deleted from the queue.");
    }
  }  

  //create a method to get front element       
  int frontElement() {
    if(rear == front) {
      System.out.println("Queue is empty.");
      return 0;
    } else {
      return queue[front+1];
    }
  }

  //create a method to get rear element       
  int rearElement() {
    if(rear == front) {
      System.out.println("Queue is empty.");
      return 0;
    } else {
      return queue[rear];
    }
  }
}

//test the code
public class MyClass {
  public static void main(String[] args) {
    CreateQueue MyQueue = new CreateQueue();
    MyQueue.EnQueue(10);
    MyQueue.EnQueue(20);
    MyQueue.EnQueue(30);
    MyQueue.EnQueue(40);

    MyQueue.DeQueue();
    MyQueue.isEmpty(); 
  }
}

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.
using System;

class CreateQueue {
  private int MAX = 100;
  private int front;
  private int rear;
  private int[] queue;

  public CreateQueue() {
    //assigning MAX size of the queue
    queue = new int[MAX];   
    front = -1;
    rear = -1;
  }

  // create a method to check whether 
  // the queue is empty or not  
  public void isEmpty() {
    if(rear == front) {
      Console.WriteLine("Queue is empty.");
    } else {
      Console.WriteLine("Queue is not empty.");
    }
  }

  //create a method to return size of the queue 
  public int size() {
     return rear - front;
  } 

  //create a method to add new element       
  public void EnQueue(int x){
    if(rear == (MAX - 1)){
      Console.WriteLine("Queue size limit reached.");
    } else {
      queue[++rear] = x;
      Console.WriteLine(x + " is added into the queue.");
    }
  }

  //create a method to delete front element       
  public void DeQueue(){
    if(rear == front){
      Console.WriteLine("Queue is empty.");
    } else {
      int x = queue[++front];
      Console.WriteLine(x + " is deleted from the queue.");
    }
  }  

  //create a method to get rear element       
  public int rearElement() {
    if(rear == front) {
      Console.WriteLine("Queue is empty.");
      return 0;
    } else {
      return queue[rear];
    }
  }

  //create a method to get front element       
  public int frontElement() {
    if(rear == front) {
      Console.WriteLine("Queue is empty.");
      return 0;
    } else {
      return queue[front + 1];
    }
  }
}

//test the code
class MyProgram {
 static void Main(string[] args) {
  CreateQueue MyQueue = new CreateQueue();
  MyQueue.EnQueue(10);
  MyQueue.EnQueue(20);
  MyQueue.EnQueue(30);
  MyQueue.EnQueue(40);

  MyQueue.DeQueue();
  MyQueue.isEmpty();  
 }
}

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.
<?php

class CreateQueue {
  public $front;
  public $rear;

  public $queue = array();

  function __construct() {
    $this->rear = -1;
    $this->front = -1;
  }

  // create a function to check whether 
  // the queue is empty or not 
  public function isEmpty()
  {
    if($this->rear == $this->front) {
      echo "Queue is empty. \n";
    } else {
      echo "Queue is not empty. \n";
    }
  }

  //create a function to return size of the queue 
  public function size()
  {
     return ($this->rear - $this->front);
  }

  //create a function to add new element  
  public function EnQueue($x)
  {
    $this->queue[++$this->rear] = $x;
    echo $x." is added into the queue. \n";
  }

  //create a function to delete front element  
  public function DeQueue()
  {
    if($this->rear == $this->front){
      echo "Queue is empty. \n";
    } else {
      $x = $this->queue[++$this->front];
      echo $x." is deleted from the queue. \n";
    }
  }

  //create a function to get front element  
  public function frontElement()
  {
    if($this->rear == $this->front) {
      echo "Queue is empty. \n";
    } else {
      return $this->queue[$this->front+1];
    }
  }

  //create a function to get rear element   
  public function rearElement()
  {
    if($this->rear == $this->front) {
      echo "Queue is empty. \n";
    } else {
      return $this->queue[$this->rear];
    }
  }
}

//test the code
$MyQueue = new CreateQueue();
$MyQueue->EnQueue(10);
$MyQueue->EnQueue(20);
$MyQueue->EnQueue(30);
$MyQueue->EnQueue(40);

$MyQueue->DeQueue();
$MyQueue->isEmpty();
?>

The above code will give the following output:

10 is added into the queue.
20 is added into the queue.
30 is added into the queue.
40 is added into the queue.
10 is deleted from the queue.
Queue is not empty.