Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# 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.

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

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.