title: Queue implementation using Array in C++
body: <p>I have implemented an integer type Queue using an array in C++ as:<br>
  This is not a circular queue so we don't have to bother for the empty space after filling it once and then deleting some values.</p>
  
  <pre><code>class IntQueue
  {
      private:
          int *queueArray;
          int queueSize;
          int rear, front;
  
      public:
          IntQueue(int);          // Constructor
          ~IntQueue(void);
          void Enqueue(int);
          int Dequeue(void);
          bool isFull(void);
          bool isEmpty(void);
          void Display(void);
  };  
  
  // Constructor
  IntQueue::IntQueue(int size)
  {
      queueArray = new int[size];
      queueSize = size;
      rear = -1;
      front = -1;
  }  
  
  // Destructor
  IntQueue::~IntQueue(void) 
  {    
      delete [] queueArray;
  }  
  // Enqueue function
  void IntQueue::Enqueue(int num)
  {
      if (isFull())
      {
          cout &lt;&lt; "The Queue is full.\n";
      }
      else
      {
          rear++;
          queueArray[rear] = num;
      }   
  }
  
  // Dequeue function
  int IntQueue::Dequeue()
  {
      int num;
      if (isEmpty())
      {
          cout &lt;&lt; "The Queue is empty.\n";
      }
      else
      {
          front++;
          num = queueArray[front];
      }
      return num;
  }
  
  //isFull function
  bool IntQueue::isFull(void)
  {
      bool status;
  
      if (rear == queueSize - 1)
          status = true;
      else
          status = false;
      return status;
  }
  
  //isEmpty function
  bool IntQueue::isEmpty(void)
  {
      bool status;
  
      if (front == rear)
          status = true;
      else
          status = false;
      return status;
  }
  
  // Display function
  void IntQueue::Display()
  {
      if(isEmpty())
      {
          cout &lt;&lt; "The Queue is empty.\n";
      }
  
      for(int i = front++; i &lt;= rear; i++)
      {
          cout &lt;&lt; "queueArray[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; queueArray[i] &lt;&lt; endl;
      }
  }  
  // Main function
  int main()
  {
      int option, queueCapacity, value;
  
  
      cout &lt;&lt; endl &lt;&lt; "Enter size of queue: ";
      cin  &gt;&gt; queueCapacity;
  
      IntQueue *queue = new IntQueue(queueCapacity);
  
      Menu:
      cout &lt;&lt; endl &lt;&lt; "Queue implementation using Array" &lt;&lt; endl;
      cout &lt;&lt; "--------------------------------" &lt;&lt; endl;
      cout &lt;&lt; "1. Enqueue"    &lt;&lt; endl;
      cout &lt;&lt; "2. Dequeue"     &lt;&lt; endl;
      cout &lt;&lt; "3. Display Queue" &lt;&lt; endl;
      cout &lt;&lt; "4. Display Menu" &lt;&lt; endl;
      cout &lt;&lt; "5. Exit"    &lt;&lt; endl;
      cout &lt;&lt; "--------------------------------" &lt;&lt; endl;
  
      YourOption:
      cout &lt;&lt; endl &lt;&lt; "Your option: ";
      cin &gt;&gt; option;
  
      switch(option)
      {
          case 1:
              // Enqueue
              cout &lt;&lt; "Enter value to enqueue: ";
              cin  &gt;&gt; value;
              queue-&gt;Enqueue(value);
              goto YourOption;
  
          case 2:
              // Dequeue
              int data;
              data = queue-&gt;Dequeue();
              cout &lt;&lt; data &lt;&lt; " is dequeued!";
              goto YourOption;    
          case 3:
              // Display
              queue-&gt;Display();
              goto YourOption;    
          case 4:
              goto Menu;  
          case 5:
              exit(0);
          default:
              cout &lt;&lt; "Invalid option!" &lt;&lt; endl;
              break;
      }
      delete queue;
      return 0;
  }
  </code></pre>
  
  <p>The variable <code>rear</code> is initially <code>-1</code>. I have checked <code>isFull()</code> function as if <code>rear</code> is equal to the size of <code>Queue</code>, then the queue is full, not otherwise. However, to check queue emptiness, I compared if <code>rear == front</code>, then empty, not otherwise. But after executing it, by opting <code>2</code> from the menu, it displays some random value sometimes and the message i.e., "The Queue is empty".<br>
  What is the problem in this example?</p>