Danieboy
Danieboy

Reputation: 4511

C++ Circular queue class with a length of 100

I've got some questions on how to create and use a circular queue class in C++. The questions are in the code as comments because I am looking for general pointers on how to see if I'm on the right track.


Edit:

Adding the specific questions here for clarity:

  1. Do I need a pointer for both the head and the tail of the queue?

  2. Do I initialize a queue like this or is there a better way?

  3. How do I dequeue and return the correct value?


The code:

#include <iostream>
#include <stdexcept>
using namespace std;

class Queue{
private:
    int maxSize;
    int *data; // Do I maybe need one for head and one for tail?
    int counter; // Used to count the amount of elements in the queue.
public:
    Queue();
    Queue(int max=100); 
    ~Queue();
    void clear(); 
    bool empty() const; 
    bool full() const; 
    int dequeue() throw (length_error); 
    void enqueue(int value) throw (length_error); 
    int length(); 
};

Queue :: Queue() // Is this the correct way to initialize the queue?
{
      data[100];
      counter = 0;
      maxSize = 100;
}

void Queue::clear() 
{
    data = NULL;
}

bool Queue::empty()const
{
    if(counter == 0)
       return true;
    return false;
}

bool Queue::full()const
{
    if(counter == 100)
       return true;
    return false;
}

int Queue::length()
{
    return counter;    
}

int Queue::dequeue() throw (length_error) // How do I get the correct element and then return it.
{
    if(counter >= 0)
    {
       counter--;
       return counter; 
    }
    return counter; 
}

void Queue::enqueue(int value) throw(length_error)
{
     if(counter < maxSize)
     {
        data[counter] = value;
        counter++;
        cout << "Adds the number at the end if there is room for it." << endl;
     }
     else
         ;// throw(length_error);
}

int main()
{
    Queue minQueue;
    minQueue.enqueue(10);
    minQueue.enqueue(12);
    minQueue.enqueue(14);
    minQueue.enqueue(16);
    minQueue.enqueue(18);
    cout << "Empty: " << minQueue.empty() << endl;
    cout << "Full: " << minQueue.full() << endl;
    cout << "Length: " << minQueue.length() << endl;
    minQueue.dequeue();
    minQueue.clear();
    system("pause");
}

Upvotes: 0

Views: 1167

Answers (1)

Doc Brown
Doc Brown

Reputation: 20044

Your code has some issues.

class Queue{
    // ...
  public:
    // Queue(); // deleted this line, since it collides with the next ctor declation. 
    // A ctor with one optional argument includes a ctor with no arguments
    Queue(int max=100);
    ~Queue();
}
//...

Queue::Queue(int max) // argument required!
{
      data = new int[max]; // this is probably what you want
      counter = 0;
      maxSize = max;
}

void Queue::clear() 
{
    delete[] data;  // see above, must match the allocation
}

int Queue::dequeue() throw (length_error) 
{
    if(counter > 0) // !!!! dont't decrease when 0
    {
       counter--;
       return data[counter];
    }
    else 
       throw(length_error());  // an exception class length_error must exist!
}

These are a few starting points, fill the remaining gaps by yourself.

Upvotes: 3

Related Questions