Open In App

Introduction to Queue – Data Structure and Algorithm Tutorials

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

What is Queue?

A queue is a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order.

We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.  The element which is first pushed into the order, the delete operation is first performed on that.

FIFO Principle of Queue:

  • A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve).
  • Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure.
FIFO property of queue

FIFO property of queue

Characteristics of Queue:

  • Queue can handle multiple data.
  • We can access both ends.
  • They are fast and flexible. 

Queue Representation:

1. Array Representation of Queue:

Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the array. Variables used in this case are

  • Queue: the name of the array storing queue elements.
  • Front: the index where the first element is stored in the array representing the queue.
  • Rear: the index where the last element is stored in an array representing the queue.

Array representation of queue:

C




// Creating an empty queue
 
// A structure to represent a queue
struct Queue {
    int front, rear, size;
    unsigned capacity;
    int* array;
};
 
// function to create a queue of given capacity
// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
    struct Queue* queue
        = (struct Queue*)malloc(sizeof(struct Queue));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
    queue->rear = capacity - 1;
    queue->array
        = (int*)malloc(queue->capacity * sizeof(int));
    return queue;
}
 
// This code is contributed by Susobhan Akhuli


C++




// Creating an empty queue
 
// A structure to represent a queue
class Queue {
public:
    int front, rear, size;
    unsigned cap;
    int* arr;
};
 
// Function to create a queue of given capacity
// It initializes size of queue as 0
Queue* createQueue(unsigned cap)
{
    Queue* queue = new Queue();
    queue->cap = cap;
    queue->front = queue->size = 0;
 
    queue->rear = cap - 1;
    queue->arr = new int[(queue->cap * sizeof(int))];
    return queue;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
class GFG {
 
    // A structure to represent a queue
    static class Queue {
        int front, rear, size;
        int cap;
        int arr[];
    }
 
    // Function to create a queue of given capacity
    // It initializes size of queue as 0
    Queue createQueue(int cap)
    {
        Queue queue = new Queue();
        queue.cap = cap;
        queue.front = 0;
        queue.size = 0;
 
        queue.rear = cap - 1;
        queue.arr = new int[queue.cap];
        return queue;
    }
}
 
// This code is contributed by aadityapburujwale


Python3




# Creating an empty queue
 
# A structure to represent a queue
 
 
class Queue:
        # constructor
    def __init__(self, cap):
        self.cap = cap
        self.front = 0
        self.size = 0
        self.rear = cap - 1
        self.arr = [0] * cap
 
    # Function to create a queue of given capacity
    # It initializes size of queue as 0
    def createQueue(self):
        return Queue(self.cap)
 
# This code is contributed by Tapesh (tapeshdua420)


C#




// Creating an empty queue
 
class GFG {
    // A structure to represent a queue
    static class Queue {
        public int front, rear, size;
        public int cap;
        public int[] arr;
    }
 
    // Function to create a queue of given capacity
    // It initializes size of queue as 0
    public static Queue createQueue(int cap)
    {
        Queue queue = new Queue();
        queue.cap = cap;
        queue.front = 0;
        queue.size = 0;
 
        queue.rear = cap - 1;
        queue.arr = new int[queue.cap];
        return queue;
    }
}
 
// This code is contributed by Tapesh (tapeshdua420)


Javascript




<script>
// Queue class
class Queue
{
    // Array is used to implement a Queue
    constructor()
    {
        this.items = [];
    }
}
 
// This code is contributed by Susobhan Akhuli
<script>


2. Linked List Representation of Queue:

A queue can also be represented using following entities:

  • Linked-lists, 
  • Pointers, and 
  • Structures.

C




// A C program to demonstrate linked list based
// implementation of queue
 
// A linked list (LL) node to store a queue entry
struct QNode {
    int key;
    struct QNode* next;
};
 
// The queue, front stores the front node of LL and rear
// stores the last node of LL
struct Queue {
    struct QNode *front, *rear;
};
 
// A utility function to create a new linked list node.
struct QNode* newNode(int k)
{
    struct QNode* temp
        = (struct QNode*)malloc(sizeof(struct QNode));
    temp->key = k;
    temp->next = NULL;
    return temp;
}
 
// A utility function to create an empty queue
struct Queue* createQueue()
{
    struct Queue* q
        = (struct Queue*)malloc(sizeof(struct Queue));
    q->front = q->rear = NULL;
    return q;
}
 
// This code is contributed by Susobhan Akhuli


C++




struct QNode {
    int data;
    QNode* next;
    QNode(int d)
    {
        data = d;
        next = NULL;
    }
};
 
struct Queue {
    QNode *front, *rear;
    Queue() { front = rear = NULL; }
};


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
 
    static class QNode {
        int data;
        QNode next;
 
        QNode(int data)
        {
            this.data = data;
            next = null;
        }
    }
 
    static class Queue {
        QNode front, rear;
        Queue()
        {
            front = null;
            rear = null;
        }
    }
}
 
// This code is contributed by aadityapburujwale


Python3




class QNode:
    def __init__(self, data):
        self.data = data
        self.next = None
 
 
class Queue:
    def __init__(self):
        self.front = None
        self.rear = None
 
 # This code is contributed by Tapesh (tapeshdua420)


C#




// Include namespace system
using System;
 
public class GFG {
    static class QNode {
        public int data;
        public QNode next;
        public QNode(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
    static class Queue {
        public QNode front;
        public QNode rear;
        public Queue()
        {
            this.front = null;
            this.rear = null;
        }
    }
}
 
// This code is contributed by aadityaburujwale.


Javascript




<script>
// JavaScript program for linked-list implementation of queue
class QNode
{
    constructor(key)
    {
        this.key = key;
        this.next = null;
    }
}
  
let front = null, rear = null;
 
//This code is contributed by Susobhan Akhuli
<script>


Types of Queue:

There are different types of queues:

  1. Input Restricted Queue: This is a simple queue. In this type of queue, the input can be taken from only one end but deletion can be done from any of the ends.
  2. Output Restricted Queue: This is also a simple queue. In this type of queue, the input can be taken from both ends but deletion can be done from only one end.
  3. Circular Queue: This is a special type of queue where the last position is connected back to the first position. Here also the operations are performed in FIFO order. To know more refer this.
  4. Double-Ended Queue (Dequeue): In a double-ended queue the insertion and deletion operations, both can be performed from both ends. To know more refer this.
  5. Priority Queue: A priority queue is a special queue where the elements are accessed based on the priority assigned to them. To know more refer this.

To learn more about different types of queues, read the article on “Types of Queues“.

Basic Operations for Queue in Data Structure:

Some of the basic operations for Queue in Data Structure are:

  1. Enqueue() – Adds (or stores) an element to the end of the queue..
  2. Dequeue() – Removal of elements from the queue.
  3. Peek() or front()- Acquires the data element available at the front node of the queue without deleting it.
  4. rear() – This operation returns the element at the rear end without removing it.
  5. isFull() – Validates if the queue is full.
  6. isNull() – Checks if the queue is empty.

There are a few supporting operations (auxiliary operations):

1. Enqueue(): 

Enqueue() operation in Queue adds (or stores) an element to the end of the queue.
The following steps should be taken to enqueue (insert) data into a queue:

  • Step 1: Check if the queue is full.
  • Step 2: If the queue is full, return overflow error and exit.
  • Step 3: If the queue is not full, increment the rear pointer to point to the next empty space.
  • Step 4: Add the data element to the queue location, where the rear is pointing.
  • Step 5: return success.
Enqueue representation

Enqueue representation

Implementation of Enqueue:

C




// Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->array[queue->rear] = item;
    queue->size = queue->size + 1;
    printf("%d enqueued to queue\n", item);
}
 
// This code is contributed by Susobhan Akhuli


C++




void queueEnqueue(int data)
{
    // Check queue is full or not
    if (capacity == rear) {
        printf("\nQueue is full\n");
        return;
    }
 
    // Insert element at the rear
    else {
        queue[rear] = data;
        rear++;
    }
    return;
}


Java




void queueEnqueue(int data)
{
    // Check queue is full or not
    if (capacity == rear) {
        System.out.println("\nQueue is full\n");
        return;
    }
 
    // Insert element at the rear
    else {
        queue[rear] = data;
        rear++;
    }
    return;
}
 
// This code is contributed by aadityapburujwale


Python3




# Function to add an item to the queue.
# It changes rear and size
 
 
def EnQueue(self, item):
    if self.isFull():
        print("Full")
        return
    self.rear = (self.rear + 1) % (self.capacity)
    self.Q[self.rear] = item
    self.size = self.size + 1
    print("% s enqueued to queue" % str(item))
# This code is contributed by Susobhan Akhuli


C#




// Function to add an item to the queue.
// It changes rear and size
public void enqueue(int item)
{
    if (rear == max - 1) {
        Console.WriteLine("Queue Overflow");
        return;
    }
    else {
        ele[++rear] = item;
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




<script>
enqueue(element){   
    // adding element to the queue
    this.items.push(element);
}
 
// This code is contributed by Susobhan Akhuli
</script>


2. Dequeue(): 

Removes (or access) the first element from the queue.
The following steps are taken to perform the dequeue operation:

  • Step 1: Check if the queue is empty.
  • Step 2: If the queue is empty, return the underflow error and exit.
  • Step 3: If the queue is not empty, access the data where the front is pointing.
  • Step 4: Increment the front pointer to point to the next available data element.
  • Step 5: The Return success.
     
Dequeue operation

Dequeue operation

Implementation of dequeue:

C




// Function to remove an item from queue.
// It changes front and size
int dequeue(struct Queue* queue)
{
    if (isEmpty(queue)) {
        printf("\nQueue is empty\n");
        return;
    }
    int item = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}
 
// This code is contributed by Susobhan Akhuli


C++




void queueDequeue()
{
    // If queue is empty
    if (front == rear) {
        printf("\nQueue is empty\n");
        return;
    }
 
    // Shift all the elements from index 2
    // till rear to the left by one
    else {
        for (int i = 0; i < rear - 1; i++) {
            queue[i] = queue[i + 1];
        }
 
        // decrement rear
        rear--;
    }
    return;
}


Java




void queueDequeue()
{
    // If queue is empty
    if (front == rear) {
        System.out.println("\nQueue is empty\n");
        return;
    }
 
    // Shift all the elements from index 2
    // till rear to the left by one
    else {
        for (int i = 0; i < rear - 1; i++) {
            queue[i] = queue[i + 1];
        }
 
        // decrement rear
        rear--;
    }
    return;
}
 
// This code is contributed by aadityapburujwale


Python3




# Function to remove an item from queue.
# It changes front and size
 
 
def DeQueue(self):
    if self.isEmpty():
        print("Queue is empty")
        return
 
    print("% s dequeued from queue" % str(self.Q[self.front]))
    self.front = (self.front + 1) % (self.capacity)
    self.size = self.size - 1
# This code is contributed by Susobhan Akhuli


C#




// Function to remove an item from queue.
// It changes front and size
public int dequeue()
{
    if (front == rear + 1) {
        Console.WriteLine("Queue is Empty");
        return -1;
    }
    else {
        int p = ele[front++];
        return p;
    }
}
// This code is contributed by Susobhan Akhuli


Javascript




<script>
dequeue(){
    // removing element from the queue
    // returns underflow when called
    // on empty queue
    if(this.isEmpty()){
        document.write("<br>Queue is empty<br>");
           return -1;
    }
    return this.items.shift();
}
// This code is contributed by Susobhan Akhuli
</script>


3. front(): 

This operation returns the element at the front end without removing it.

C




// Function to get front of queue
int front(struct Queue* queue)
{
    if (isempty(queue))
        return INT_MIN;
    return queue->arr[queue->front];
}
 
// This code is contributed by Susobhan Akhuli


C++




// Function to get front of queue
int front(Queue* queue)
{
    if (isempty(queue))
        return INT_MIN;
    return queue->arr[queue->front];
}


Java




// Function to get front of queue
int front(Queue queue)
{
    if (isempty(queue))
        return Integer.MIN_VALUE;
    return queue.arr[queue.front];
}
 
// This code is contributed by aadityapburujwale


Python3




# Function to get front of queue
def que_front(self):
        if self.isempty():
            return "Queue is empty"
        return self.Q[self.front]
 
# This code is contributed By Susobhan Akhuli


C#




//  Function to get front of queue
public int front()
{
    if (isempty())
        return INT_MIN;
    return arr[front];
}
 
//  This code is contributed By Susobhan Akhuli


Javascript




<script>
// Function to get front of queue
front(){
    // returns the Front element of
    // the queue without removing it.
    if(this.isEmpty())
           return "No elements in Queue<br>";
    return this.items[0];
}
// This code is contributed By Susobhan Akhuli
<script>


4. rear(): 

This operation returns the element at the rear end without removing it.

C




int rear(struct Queue* front)
{
    if (front == NULL) {
        printf("Queue is empty.\n");
        return -1;
    }
 
    while (front->next != NULL) {
        front = front->next;
    }
 
    return front->data;
}


C++




int rear(queue<int>& myQueue)
{
    queue<int> tempQueue = myQueue;
 
    while (tempQueue.size() > 1) {
        tempQueue.pop();
    }
 
    return tempQueue.front();
}


Java




public static int rear(Queue<Integer> myQueue)
{
    if (myQueue.isEmpty()) {
        System.out.println("Queue is empty.");
        return -1;
    }
 
    int rearElement = -1;
    while (!myQueue.isEmpty()) {
        rearElement = myQueue.poll();
    }
 
    return rearElement;
}


Python3




def rear(queue):
    if queue.empty():
        print("Queue is empty.")
        return None
 
    rear_element = None
    while not queue.empty():
        rear_element = queue.get()
 
    return rear_element


C#




static int Rear(Queue<int> myQueue)
{
    if (myQueue.Count == 0) {
        Console.WriteLine("Queue is empty.");
        return -1;
    }
 
    int rearElement = -1;
    while (myQueue.Count > 0) {
        rearElement = myQueue.Dequeue();
    }
 
    return rearElement;
}


Javascript




function rear(queue) {
    if (queue.length === 0) {
        console.log("Queue is empty.");
        return -1;
    }
 
    let rearElement = -1;
    while (queue.length > 0) {
        rearElement = queue.shift();
    }
 
    return rearElement;
}


5. isEmpty(): 

This operation returns a boolean value that indicates whether the queue is empty or not.

C




// Queue is empty when size is 0
bool isEmpty(struct Queue* queue)
{
    return (queue->size == 0);
}
 
// This code is contributed by Susobhan Akhuli


C++




// This function will check whether
// the queue is empty or not:
bool isEmpty()
{
    if (front == -1)
        return true;
    else
        return false;
}


Java




// This function will check whether
// the queue is empty or not:
boolean isEmpty()
{
    if (front == -1)
        return true;
    else
        return false;
}
 
// This code is contributed by aadityapburujwale


Python3




# Queue is empty when size is 0
def isEmpty(self):
    return self.size == 0
# This code is contributed by Susobhan Akhuli


C#




// This function will check whether
// the queue is empty or not:
bool isEmpty()
{
    if (front == -1)
        return true;
    else
        return false;
}
 
// This code is contributed by lokeshmvs21.


Javascript




</script>
isEmpty(){
    // return true if the queue is empty.
    return this.items.length == 0;
}
// This code is contributed by Susobhan Akhuli
</script>


6. isFull(): 

This operation returns a boolean value that indicates whether the queue is full or not.

C




// Queue is full when size becomes
// equal to the capacity
bool isFull(struct Queue* queue)
{
    return (queue->size == queue->capacity);
}
 
// This code is contributed by Susobhan Akhuli


C++




// This function will check
// whether the queue is full or not.
bool isFull()
{
    if (front == 0 && rear == MAX_SIZE - 1) {
        return true;
    }
    return false;
}


Java




// This function will check
// whether the queue is full or not.
boolean isFull()
{
    if (front == 0 && rear == MAX_SIZE - 1) {
        return true;
    }
    return false;
}
 
// This code is contributed by aadityapburujwale


Python3




# Queue is full when size becomes
# equal to the capacity
 
 
def isFull(self):
    return self.size == self.capacity
 
# This code is contributed by Susobhan Akhuli


C#




// Function to add an item to the queue.
// It changes rear and size
public bool isFull(int item) { return (rear == max - 1); }
// This code is contributed by Susobhan Akhuli


Javascript




// This function will check
// whether the queue is full or not.
function isFull(){
    if(front==0 && rear==MAX_SIZE-1){
        return true;
    }
    return false;
}


Implementation of Queue:

Queue can be implemented using following data structures:

We have discussed the Structure implementation of Queue below:

C




// C program for array implementation of queue
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
 
// A structure to represent a queue
struct Queue {
    int front, rear, size;
    unsigned capacity;
    int* array;
};
 
// function to create a queue
// of given capacity.
// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
    struct Queue* queue
        = (struct Queue*)malloc(sizeof(struct Queue));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
 
    // This is important, see the enqueue
    queue->rear = capacity - 1;
    queue->array
        = (int*)malloc(queue->capacity * sizeof(int));
    return queue;
}
 
// Queue is full when size becomes
// equal to the capacity
int isFull(struct Queue* queue)
{
    return (queue->size == queue->capacity);
}
 
// Queue is empty when size is 0
int isEmpty(struct Queue* queue)
{
    return (queue->size == 0);
}
 
// Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->array[queue->rear] = item;
    queue->size = queue->size + 1;
    printf("%d enqueued to queue\n", item);
}
 
// Function to remove an item from queue.
// It changes front and size
int dequeue(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    int item = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}
 
// Function to get front of queue
int front(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    return queue->array[queue->front];
}
 
// Function to get rear of queue
int rear(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    return queue->array[queue->rear];
}
 
// Driver program to test above functions./
int main()
{
    struct Queue* queue = createQueue(1000);
 
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
 
    printf("%d dequeued from queue\n", dequeue(queue));
    printf("Front item is %d\n", front(queue));
    printf("Rear item is %d\n", rear(queue));
 
    return 0;
}
// This code is contributed by Susobhan Akhuli


C++




// Implementation of queue(enqueue, dequeue).
#include <bits/stdc++.h>
using namespace std;
 
class Queue {
public:
    int front, rear, size;
    unsigned cap;
    int* arr;
};
 
Queue* createQueue(unsigned cap)
{
    Queue* queue = new Queue();
    queue->cap = cap;
    queue->front = queue->size = 0;
 
    queue->rear = cap - 1;
    queue->arr = new int[(queue->cap * sizeof(int))];
    return queue;
}
 
int isFull(Queue* queue)
{
    return (queue->size == queue->cap);
}
 
int isempty(Queue* queue) { return (queue->size == 0); }
// Function to add an item to the queue.
// It changes rear and size.
void enqueue(Queue* queue, int item)
{
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->cap;
    queue->arr[queue->rear] = item;
    queue->size = queue->size + 1;
    cout << item << " enqueued to queue\n";
}
// Function to remove an item from queue.
// It changes front and size
int dequeue(Queue* queue)
{
    if (isempty(queue))
        return INT_MIN;
    int item = queue->arr[queue->front];
    queue->front = (queue->front + 1) % queue->cap;
    queue->size = queue->size - 1;
    return item;
}
int front(Queue* queue)
{
    if (isempty(queue))
        return INT_MIN;
    return queue->arr[queue->front];
}
int rear(Queue* queue)
{
    if (isempty(queue))
        return INT_MIN;
    return queue->arr[queue->rear];
}
 
// Driver code
int main()
{
    Queue* queue = createQueue(1000);
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
    cout << dequeue(queue);
    cout << " dequeued from queue\n";
    cout << "Front item is " << front(queue) << endl;
    cout << "Rear item is " << rear(queue);
    return 0;
}


Java




// Java program for array
// implementation of queue
 
// A class to represent a queue
class Queue {
    int front, rear, size;
    int capacity;
    int array[];
 
    public Queue(int capacity)
    {
        this.capacity = capacity;
        front = this.size = 0;
        rear = capacity - 1;
        array = new int[this.capacity];
    }
 
    // Queue is full when size becomes
    // equal to the capacity
    boolean isFull(Queue queue)
    {
        return (queue.size == queue.capacity);
    }
 
    // Queue is empty when size is 0
    boolean isEmpty(Queue queue)
    {
        return (queue.size == 0);
    }
 
    // Method to add an item to the queue.
    // It changes rear and size
    void enqueue(int item)
    {
        if (isFull(this))
            return;
        this.rear = (this.rear + 1) % this.capacity;
        this.array[this.rear] = item;
        this.size = this.size + 1;
        System.out.println(item + " enqueued to queue");
    }
 
    // Method to remove an item from queue.
    // It changes front and size
    int dequeue()
    {
        if (isEmpty(this))
            return Integer.MIN_VALUE;
 
        int item = this.array[this.front];
        this.front = (this.front + 1) % this.capacity;
        this.size = this.size - 1;
        return item;
    }
 
    // Method to get front of queue
    int front()
    {
        if (isEmpty(this))
            return Integer.MIN_VALUE;
 
        return this.array[this.front];
    }
 
    // Method to get rear of queue
    int rear()
    {
        if (isEmpty(this))
            return Integer.MIN_VALUE;
 
        return this.array[this.rear];
    }
}
 
// Driver class
public class Test {
    public static void main(String[] args)
    {
        Queue queue = new Queue(1000);
 
        queue.enqueue(10);
        queue.enqueue(20);
        queue.enqueue(30);
        queue.enqueue(40);
 
        System.out.println(queue.dequeue()
                           + " dequeued from queue");
 
        System.out.println("Front item is "
                           + queue.front());
 
        System.out.println("Rear item is " + queue.rear());
    }
}
 
// This code is contributed by Susobhan Akhuli


Python3




# Python3 program for array implementation of queue
 
# Class Queue to represent a queue
 
 
class Queue:
 
    # __init__ function
    def __init__(self, capacity):
        self.front = self.size = 0
        self.rear = capacity - 1
        self.Q = [None]*capacity
        self.capacity = capacity
 
    # Queue is full when size becomes
    # equal to the capacity
    def isFull(self):
        return self.size == self.capacity
 
    # Queue is empty when size is 0
    def isEmpty(self):
        return self.size == 0
 
    # Function to add an item to the queue.
    # It changes rear and size
    def EnQueue(self, item):
        if self.isFull():
            print("Full")
            return
        self.rear = (self.rear + 1) % (self.capacity)
        self.Q[self.rear] = item
        self.size = self.size + 1
        print("% s enqueued to queue" % str(item))
 
    # Function to remove an item from queue.
    # It changes front and size
    def DeQueue(self):
        if self.isEmpty():
            print("Empty")
            return
 
        print("% s dequeued from queue" % str(self.Q[self.front]))
        self.front = (self.front + 1) % (self.capacity)
        self.size = self.size - 1
 
    # Function to get front of queue
    def que_front(self):
        if self.isEmpty():
            print("Queue is empty")
 
        print("Front item is", self.Q[self.front])
 
    # Function to get rear of queue
    def que_rear(self):
        if self.isEmpty():
            print("Queue is empty")
        print("Rear item is"self.Q[self.rear])
 
 
# Driver Code
if __name__ == '__main__':
 
    queue = Queue(30)
    queue.EnQueue(10)
    queue.EnQueue(20)
    queue.EnQueue(30)
    queue.EnQueue(40)
    queue.DeQueue()
    queue.que_front()
    queue.que_rear()
# This code is contributed by Susobhan Akhuli


C#




// C# program for array implementation of queue
using System;
 
namespace GeeksForGeeks {
// A class to represent a linearqueue
class Queue {
    private int[] ele;
    private int front;
    private int rear;
    private int max;
 
    public Queue(int size)
    {
        ele = new int[size];
        front = 0;
        rear = -1;
        max = size;
    }
 
    // Function to add an item to the queue.
    // It changes rear and size
    public void enqueue(int item)
    {
        if (rear == max - 1) {
            Console.WriteLine("Queue Overflow");
            return;
        }
        else {
            ele[++rear] = item;
        }
    }
 
    // Function to remove an item from queue.
    // It changes front and size
    public int dequeue()
    {
        if (front == rear + 1) {
            Console.WriteLine("Queue is Empty");
            return -1;
        }
        else {
            Console.WriteLine(ele[front]
                              + " dequeued from queue");
            int p = ele[front++];
            Console.WriteLine("Front item is {0}",
                              ele[front]);
            Console.WriteLine("Rear item is {0} ",
                              ele[rear]);
            return p;
        }
    }
 
    // Function to print queue.
    public void printQueue()
    {
        if (front == rear + 1) {
            Console.WriteLine("Queue is Empty");
            return;
        }
        else {
            for (int i = front; i <= rear; i++) {
                Console.WriteLine(ele[i]
                                  + " enqueued to queue");
            }
        }
    }
}
 
// Driver code
class Program {
    static void Main()
    {
        Queue Q = new Queue(5);
 
        Q.enqueue(10);
        Q.enqueue(20);
        Q.enqueue(30);
        Q.enqueue(40);
        Q.printQueue();
        Q.dequeue();
    }
}
}
// This code is contributed by Susobhan Akhuli


Javascript




<script>
// Queue class
class Queue
{
    // Array is used to implement a Queue
    constructor()
    {
        this.items = [];
    }
    isEmpty()
    {
        // return true if the queue is empty.
        return this.items.length == 0;
    }
    enqueue(element)
    {   
        // adding element to the queue
        this.items.push(element);
        document.write(element + " enqueued to queue<br>");
    }
    dequeue()
    {
        // removing element from the queue
        // returns underflow when called
        // on empty queue
        if(this.isEmpty())
            return "Underflow<br>";
        return this.items.shift();
    }
    front()
    {
        // returns the Front element of
        // the queue without removing it.
        if(this.isEmpty())
            return "No elements in Queue<br>";
        return this.items[0];
    }
    rear()
    {
        // returns the Rear element of
        // the queue without removing it.
        if(this.isEmpty())
            return "No elements in Queue<br>";
        return this.items[this.items.length-1];
    }
}
 
// creating object for queue class
var queue = new Queue();
 
// Adding elements to the queue
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
 
// queue contains [10, 20, 30, 40]
// removes 10
document.write(queue.dequeue() + " dequeued from queue<br>");
 
// queue contains [20, 30, 40]
// Front is now 20
document.write("Front item is " + queue.front() + "<br>");
 
// printing the rear element
// Rear is 40
document.write("Rear item is " + queue.rear() + "<br>");
 
// This code is contributed by Susobhan Akhuli
</script>


Output

10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40

Time complexity: All the operations have O(1) time complexity.
Auxiliary Space: O(N) 

Applications of Queue:

Application of queue is common. In a computer system, there may be queues of tasks waiting for the printer, for access to disk storage, or even in a time-sharing system, for use of the CPU. Within a single program, there may be multiple requests to be kept in a queue, or one task may create other tasks, which must be done in turn by keeping them in a queue.

  • It has a single resource and multiple consumers.
  • It synchronizes between slow and fast devices.
  • In a network, a queue is used in devices such as a router/switch and mail queue.
  • Variations: dequeue, priority queue and double-ended priority queue.

FAQs (Frequently asked questions) on Queue:

1. What data structure can be used to implement a priority queue?

Priority queues can be implemented using a variety of data structures, including linked lists, arrays, binary search trees, and heaps. Priority queues are best implemented using the heap data structure.

2. Queues are used for what purpose?

In addition to making your data persistent, queues reduce errors that occur when different parts of your system are down.

3. In data structures, what is a double-ended queue?

In a double-ended queue, elements can be inserted and removed at both ends.

4. What is better, a stack or a queue?

If you want things to come out in the order you put them in, use a queue. Stacks are useful when you want to reorder things after putting them in. 

Related articles:



Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads