The queue is another data structure. A physical analogy for a queue is a line at a bank. When you go to the bank, customers go to the rear (end) of the line and customers who are serviced come out of the line from the front of the line.Â
The main property of a queue is that objects go on the rear and come off of the front of the queue.Â
• Make-QueueÂ
Create a new, empty queue object.Â
• EmptyÂ
Reports whether queue is empty or not.Â
• Enter(or Insert)Â
Places an object at the rear of the queueÂ
• Delete (or Remove)Â
Removes an object from the front of the queue and produces that object. Write a program to create a queue class and do queue operations with exception handling.Â
#include <iostream>
#include <cstdlib>
using namespace std;
#define SIZE 7
Â
class Queue
{
private:
  int *array1;
  int front;
  int rear;
  int capacity;
  int c; Â
Â
public:
  Queue(int size){
    array1 = new int[size];
    capacity = size;
    front = 0;
    rear = -1;
    c = 0;
  }
  int size() {
    return c;
  }
  bool isEmpty(){
    return (size() == 0);Â
  }
  bool isFull() {
    return (size() == capacity);
  }
  void dequeue()
  {
    if (isEmpty())
    {
      cout << "Underflow";
    }
    else{
      cout<< array1[front]<<" is removed" << endl;
      front = (front + 1) % capacity;
      c--;
    }
  }
  void enqueue(int n)
  {
    if (isFull())
    {
      cout << "Overflow";
    }
    else{
      cout << n<<" is inserted" << endl;
      rear = (rear + 1) % capacity;
      array1[rear] = n;
      c++;
    }
  }
Â
};
int main()
{
  Queue q(6);
Â
  q.enqueue(23);
  q.enqueue(42);
  q.enqueue(34);
  q.dequeue();
  q.dequeue();
  q.enqueue(49);
  q.isEmpty();
  q.dequeue();
Â
  return 0;
}
Comments
Leave a comment