hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

Circular Queue


  • Approach:

    1. Initiate a Queue.

    2. Check if the queue is already full while inserting an element.

    3. If the queue is full then the enqueue function will return false. Otherwise, it will return true and enqueue the element x.

    4. Check if the queue is already empty while deleting an element.

    5. If the queue is empty then the dequeue function will return false. Otherwise, it will return true and remove the front element.


  • Time and Space Complexity:

    • Time Complexity: O(Q), where Q = number of queries

    • Space Complexity: O(N)


Code [C++]

#include <bits/stdc++.h>

class CircularQueue{

public:

int N;

queue<int>myQueue;

CircularQueue(int n){

N = n;

}


// Enqueues 'X' into the queue. Returns true if it gets pushed into the stack, and false otherwise.

bool enqueue(int value){

if(myQueue.size() == N) return false;

myQueue.push(value);

return true;

}


// Dequeues top element from queue. Returns -1 if the stack is empty, otherwise returns the popped element.

int dequeue(){

if(myQueue.empty()) return -1;

int top = myQueue.front();

myQueue.pop();

return top;

}

};