Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Implementation of queue using an array
2.1.
1. Insertion of the element in the queue
2.2.
2. Deletion of the element from the queue
2.3.
3. Printing the whole queue
2.4.
4. Displaying the front element of the queue
3.
Implementation of queue using a linked list
3.1.
1. Insertion of the element in the queue
3.2.
2. Deletion of the element from the queue
3.3.
3. Printing the whole queue
4.
Frequently Asked Questions
4.1.
What is LIFO?
4.2.
What is the difference between stack and queue?
4.3.
What are the types of queues?
5.
Conclusion
Last Updated: Mar 27, 2024

Implement std::queue in C++

Introduction

This blog will discuss the implementation of std::queue in C++. We will discuss two different approaches to the implementation of queues in C++. But before discussing the different approaches to implementing the queue, we should look at what a queue is.

A queue is a Data Structure that follows the principle of FIFO (First In First Out), which means that the element that enters first will exit first.

 

Now that we have seen what exactly a queue is. It is time to implement the queue in C++.

We will implement the queue using two methods:

  1. Implementation of queue using an array
  2. Implementation of queue using a linked list

Implementation of queue using an array

// C++ program to implement a queue using an array
#include <bits/stdc++.h>

using namespace std;

struct Queue {
    int front, rear, size;
    int * queue;
    Queue(int c) {
            front = rear = 0;
            size = c;
            queue = new int;
        }

        ~Queue() {
            delete[] queue;
        }

    // to insert an element in the queue
    void enqueue(int element) {
        if (size == rear) {
            cout << "Queue is full" << endl;
            return;
        } else {
            cout << "Element inserted" << endl;
            queue[rear] = element;
            rear++;
        }
        return;
    }

    // function to delete an element from the queue
    void dequeue() {
        // to check if the queue is empty
        if (front == rear) {
            cout << "Queue is empty" << endl;
            return;
        }

        // shift all the elements from index 1 till the last element to the left by 1
        else {
            for (int i = 0; i < rear - 1; i++) {
                queue[i] = queue[i + 1];
            }
            cout << "Element deleted" << endl;
            // decrement the rear
            rear--;
        }
        return;
    }

    // function to print the elements of the queue
    void printQueue() {
        // to check if queue is empty
        if (front == rear) {
            cout << "Queue is Empty" << endl;
            return;
        }

        // traverse front to rear and print elements
        cout << "The queue is: ";
        for (int i = front; i < rear - 1; i++) {
            cout << queue[i] << " <-- ";
        }
        cout << queue[rear - 1] << endl;
        return;
    }

    // print the front of the queue
    void getFront() {
        // to check if the queue is empty
        if (front == rear) {
            cout << "Queue is Empty" << endl;
            return;
        }

        cout << "Front element is: " << queue[front] << endl;
        return;
    }
};

// Driver code
int main() {
    // To create a queue of size 5
    Queue queue(5);

    // print the queue elements
    queue.printQueue();

    // inserting elements in the queue
    queue.enqueue(7);
    queue.enqueue(4);
    queue.enqueue(9);
    queue.enqueue(6);

    // print the queue elements
    queue.printQueue();

    // insert the element in the queue
    queue.enqueue(1);

    // print the queue elements
    queue.printQueue();

    // deletion of the front element from the queue
    queue.dequeue();

    // print the queue elements
    queue.printQueue();

    // deletion of the front element from the queue
    queue.dequeue();

    // print Queue elements
    queue.printQueue();

    // print the front element of the queue
    queue.getFront();

    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Queue is Empty
Element inserted
Element inserted
Element inserted
Element inserted
The queue is: 7 <-- 4 <-- 9 <-- 6
Element inserted
The queue is: 7 <-- 4 <-- 9 <-- 6 <-- 1
Element deleted
The queue is: 4 <-- 9 <-- 6 <-- 1
Element deleted
The queue is: 9 <-- 6 <-- 1
Front element is: 9

 

In the above approach we are using four different functions to implement the queue the functions are: insert(), delete(), print() and getFront().

1. Insertion of the element in the queue

void enqueue(int element) {
    // to check if queue is full or not
    if (size == rear) {
        cout << "Queue is full" << endl;
        return;
    }
    // to insert an element at the rear of the queue
    else {
        cout << "Element inserted" << endl;
        queue[rear] = element;
        rear++;
    }
    return;
}
You can also try this code with Online C++ Compiler
Run Code

 

If the queue is full, we will print the queue is full; else we increment the rear index and store the new element at the rear index.
 

2. Deletion of the element from the queue

void dequeue() {
    // to check if the queue is empty
    if (front == rear) {
        cout << "Queue is empty" << endl;
        return;
    }

    // shift all the elements from index 1 till the last element to the left by 1
    else {
        for (int i = 0; i < rear - 1; i++) {
            queue[i] = queue[i + 1];
        }
        cout << "Element deleted" << endl;
        // decrement the rear
        rear--;
    }
    return;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are first checking if the queue is empty or not. If the queue is empty then we are printing the queue is empty. Else, we shift all the elements from the first index to the last index to the left by 1. Here we ignore the element's value at index 0 because we have to remove the element at index 0.
 

3. Printing the whole queue

void printQueue() {
    // to check if queue is empty
    if (front == rear) {
        cout << "Queue is Empty" << endl;
        return;
    }

    // traverse from front to rear and print the elements
    cout << "The queue is: ";
    for (int i = front; i < rear - 1; i++) {
        cout << queue[i] << " <-- ";
    }
    cout << queue[rear - 1] << endl;
    return;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are first checking if the queue is empty or not. If the queue is empty then we are printing the queue is empty else we are simply printing all the elements of the queue.

4. Displaying the front element of the queue

void getFront() {
    // to check if the queue is empty
    if (front == rear) {
        cout << "Queue is Empty" << endl;
        return;
    }

    cout << "Front element is: " << queue[front] << endl;
    return;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are first checking if the queue is empty or not. If the queue is empty then we are printing the queue is empty else we are just printing the front element by using the front variable which is keeping the index of the front variable.

Implementation of queue using a linked list

// C++ program to implement a queue using a linked list
#include <iostream>

using namespace std;

// Node for linked list
struct Node {
    int data;
    Node * next;
};

struct Queue {
    Node * front, * rear;
    Queue() {
        front = rear = NULL;
    }

    // to insert an element in the queue
    void enqueue(int element) {
        Node * q;
        q = new Node;
        q -> data = element;
        q -> next = NULL;

        if (front == NULL) {
            front = rear = q;
        } else {
            cout << "Element inserted" << endl;
            rear -> next = q;
            rear = q;
        }
    }

    // function to delete element from the queue
    void dequeue() {
        Node * temp;
        if (front == NULL) {
            cout << "Queue is Empty" << endl;
        } else {
            cout << "Element deleted" << endl;
            temp = front;
            front = front -> next;
            delete temp;
        }
        return;
    }

    // function to print the elements of the queue
    void printQueue() {
        Node * temp;
        temp = front;
        cout << "The queue is: ";
        while (temp -> next != NULL) {
            cout << temp -> data << " <-- ";
            temp = temp -> next;
        }
        cout << temp -> data << endl;
    }
};

int main()
{
    Queue queue;

    // inserting the elements in the queue
    queue.enqueue(4);
    queue.enqueue(3);
    queue.enqueue(1);

    // print the queue elements
    queue.printQueue();

    // deletion of the front element from the queue
    queue.dequeue();

    // print the queue elements
    queue.printQueue();

    // insert the element in the queue
    queue.enqueue(9);

    // print the queue
    queue.printQueue();

    // delete the queue elements
    queue.dequeue();

    // print the queue
    queue.printQueue();
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Element inserted
Element inserted
The queue is: 4 <-- 3 <-- 1
Element deleted
The queue is: 3 <-- 1
Element inserted
The queue is: 3 <-- 1 <-- 9
Element deleted
The queue is: 1 <-- 9

 

In the above approach, we are using three different functions to implement the queue the functions are: insert(), delete(), and print().

 

1. Insertion of the element in the queue

void enqueue(int element) {
    Node * q;
    q = new Node;
    q -> data = element;
    q -> next = NULL;

    if (front == NULL) {
        front = rear = q;
    } else {
        cout << "Element inserted" << endl;
        rear -> next = q;
        rear = q;
    }
    return;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are first checking whether the element we are inserting in the queue is the first element or not. If it is the first element, we assign a value to the front node. Else, we are assigning value to the next node of the linked list. 

 

2. Deletion of the element from the queue

void dequeue() {
     Node * temp;
     if (front == NULL) {
         cout << "Queue is Empty" << endl;
     } else {
         cout << "Element deleted" << endl;
         temp = front;
         front = front -> next;
         delete temp;
     }
     return;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are first checking if the queue is empty or not. If the queue is empty then we are printing queue is empty else, we are storing the node in another node named temp, and we are shifting the current node that is front to front->next.
 

3. Printing the whole queue

void printQueue() {
     Node * temp;
     temp = front;
     cout << "The queue is: ";
     while (temp -> next != NULL) {
         cout << temp -> data << " <-- ";
         temp = temp -> next;
     }
     cout << temp -> data << endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this function, we are printing all the elements of the queue.

You can try by yourself with the help of online C++ Compiler.

Frequently Asked Questions

Also readDecimal to Binary c++

What is LIFO?

The term LIFO refers to how we will insert the data into the data structure. We will pop the data that has recently been added. It signifies that the last element will be the first to be popped out.

What is the difference between stack and queue?

Stack is only one end opened, so it follows the principle of Last in last out, and the queue has both ends opened, it follows the First in first out principle.

What are the types of queues?

We have four types of queues: 

  1. Normal queue 
  2. Circular queue 
  3. Priority queue 
  4. Double-ended queue

Conclusion

This article discussed the problem of implementing a queue in C++. We have discussed two different approaches to implementing the queue: implementation using an array and implementation using a linked list.

Recommended Readings:

You can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!


Live masterclass