c++ - Trouble Implementing Array Based Queue -


i working on homework class. have implement array based circular queue. having trouble figuring out algorithm determining if queue full , empty. below c++ source file working in:

#include <iostream> #include <assert.h> #include "circularqueue.h" using namespace std;  template <class type> circularqueue<type>::circularqueue(const int & capacity) {  assert (capacity >= 0); this-> capacity = capacity; front = 0; rear = capacity-1; array = new type[capacity]; }  template <class type> circularqueue<type>::~circularqueue() {  delete [] array; }  template <class type> void circularqueue<type>::add(type value){      if (!isfull())     {         rear = (rear + 1) % capacity;         array[rear] = value;     }      else     {         cout << "the queue full, cannot add value." << endl;     } }  template <class type> void circularqueue<type>::remove(){      if(!isempty())     {         front = (front + 1) % capacity;     }      else     {         cout << "cannot delete , empty queue." << endl;     } }  template <class type> type circularqueue<type>::getfront() {      return array[front]; }  template <class type> type circularqueue<type>::getrear() {      return array[rear]; }  template <class type> int circularqueue<type>::size() {  int size;      if (front > rear)     {         size = (capacity + rear) - front;     }      else     {         size = rear - front;     }      return size; }  template <class type> bool circularqueue<type>::isempty() {  bool empty = false;      if (front == rear) //if front , rear not pointing same index, not full     {         empty = true;     }      return empty; }  template <class type> bool circularqueue<type>::isfull(){  bool full = true;     if (size() != capacity) // if size = capacity, queue full;    {         full = false;    }      return full; }  /* test procedure */ int main() {  circularqueue<int> queue(3); //create queue can hold 3 items(int) queue.add(1); //add value 1 queue [1] queue.add(2); //add value 2 queue [2, 1] queue.add(3); //add value 3 queue [3, 2, 1] queue.add(4); // shouldn't work capacity 3 trying store 4 values queue.remove(); //remove element @ front[3, 2] front should point 2  cout << "the front is: " << queue.getfront() << endl; cout << "the rear is: " << queue.getrear() << endl; cout << "the number of elements in queue is: " << queue.size() <<     endl; } 

when run program following output produced:

cannot delete , empty queue.
the front is: 4
the rear is: 4
the number of elements in queue is: 0

this leads me believe have errors in logic isfull() isempty() , possible add() , remove() functions.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -