"circular queue" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "circular queue" answered properly. Developers are finding an appropriate answer about circular queue related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like circular queue. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on circular queue. 

circular queue

By PYTHON_NOT_SNAKE.pyPYTHON_NOT_SNAKE.py on Apr 14, 2021
// Circular Queue implementation in C++

#include <iostream>
#define SIZE 5 /* Size of Circular Queue */

using namespace std;

class Queue {
   private:
  int items[SIZE], front, rear;

   public:
  Queue() {
    front = -1;
    rear = -1;
  }
  // Check if the queue is full
  bool isFull() {
    if (front == 0 && rear == SIZE - 1) {
      return true;
    }
    if (front == rear + 1) {
      return true;
    }
    return false;
  }
  // Check if the queue is empty
  bool isEmpty() {
    if (front == -1)
      return true;
    else
      return false;
  }
  // Adding an element
  void enQueue(int element) {
    if (isFull()) {
      cout << "Queue is full";
    } else {
      if (front == -1) front = 0;
      rear = (rear + 1) % SIZE;
      items[rear] = element;
      cout << endl
         << "Inserted " << element << endl;
    }
  }
  // Removing an element
  int deQueue() {
    int element;
    if (isEmpty()) {
      cout << "Queue is empty" << endl;
      return (-1);
    } else {
      element = items[front];
      if (front == rear) {
        front = -1;
        rear = -1;
      }
      // Q has only one element,
      // so we reset the queue after deleting it.
      else {
        front = (front + 1) % SIZE;
      }
      return (element);
    }
  }

  void display() {
    // Function to display status of Circular Queue
    int i;
    if (isEmpty()) {
      cout << endl
         << "Empty Queue" << endl;
    } else {
      cout << "Front -> " << front;
      cout << endl
         << "Items -> ";
      for (i = front; i != rear; i = (i + 1) % SIZE)
        cout << items[i];
      cout << items[i];
      cout << endl
         << "Rear -> " << rear;
    }
  }
};

int main() {
  Queue q;

  // Fails because front = -1
  q.deQueue();

  q.enQueue(1);
  q.enQueue(2);
  q.enQueue(3);
  q.enQueue(4);
  q.enQueue(5);

  // Fails to enqueue because front == 0 && rear == SIZE - 1
  q.enQueue(6);

  q.display();

  int elem = q.deQueue();

  if (elem != -1)
    cout << endl
       << "Deleted Element is " << elem;

  q.display();

  q.enQueue(7);

  q.display();

  // Fails to enqueue because front == rear + 1
  q.enQueue(8);

  return 0;
}

Source: www.programiz.com

Add Comment

1

All those coders who are working on the C++ based application and are stuck on circular queue can get a collection of related answers to their query. Programmers need to enter their query on circular queue related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about circular queue for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of circular queue while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "circular queue". Visit this developer's friendly online web community, CodeProZone, and get your queries like circular queue resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "circular queue"

View All C++ queries

C++ queries related to "circular queue"

Browse Other Code Languages

CodeProZone