Hello,
I can't seem to get my program to build correctly. It seems I've finally gotten close because now it will build with no errors but with 2 warnings. I'm not sure what else to change in my code to fix it. Could anyone please help? Here's my code:
// Queue.h: This file contains the definition and implementation
// for the queue class.
class Queue
{
public:
Queue(int size);
~Queue();
void clear();
bool isEmpty();
void enqueue(int element);
int dequeue();
int getFirstElement();
int getLength();
int getSize();
private:
int size; // The total number of items the queue can hold
int length; // The current number of items in the queue
int *queue;
};
Queue::Queue(int size)
{
this->size= size;
queue = new int[size];
clear();
}
Queue::~Queue()
{
size = -1;
length = -1;
delete [] queue;
}
void Queue::clear()
{
//
// TODO:
// This method deletes all of the items in the
// queue. It should also adjust the length variable
//
length = 0;
}
bool Queue::isEmpty()
{
//
// TODO:
// This method checks to see if the queue is empty
// return true if the queue is empty, false otherwise
//
if (isEmpty())
return 1;
else
return 0;
}
void Queue::enqueue(int element)
{
//
// TODO:
// This method adds the element to the end of
// the queue. You should first check if the new
// current length does not exceed the queue size.
//
if (length < size)
enqueue(element);
}
int Queue::dequeue()
{
//
// TODO:
// This method removes and returns the first element
// in the queue. You should first check that the
// queue is empty. Adjust the length of the queue
// after dequeuing the element.
//
if (&Queue::isEmpty == false)
dequeue();
if (&Queue::isEmpty == false)
length = length - 1;
return dequeue();
}
int Queue::getFirstElement()
{
//
// TODO:
// This method should only return the first element in the
// queue. This method should not dequeue the element.
// It should return -1 if the queue is empty.
if (Queue::isEmpty() == true)
return -1;
else
return getFirstElement();
}
int Queue::getLength()
{
return (length+1);
}
int Queue::getSize()
{
return size;
}
// Driver.cpp: This file contains the source code to test your
// queue class implementation.
#include <iostream>
#include "Queue.h"
using namespace std;
void printQueueProperties(Queue *queue);
void printQueue(Queue *queue);
int main(int argc, char **argv)
{
Queue *queue = new Queue(10);
printQueueProperties(queue);
// Add some nodes to the queue
queue->enqueue(10);
queue->enqueue(13);
queue->enqueue(32);
queue->enqueue(3);
queue->enqueue(16);
printQueueProperties(queue);
printQueue(queue);
// Clear the queue
queue->clear();
printQueueProperties(queue);
printQueue(queue);
// Add more nodes than the queue can hold
for (int i=0; i < 20; i++)
{
queue->enqueue(i*10);
}
printQueueProperties(queue);
printQueue(queue);
// Destroy the queue
delete queue;
return 0;
}
void printQueueProperties(Queue *queue)
{
cout << "Queue properties:" << endl;
cout << " isEmpty = " << queue->isEmpty() << endl;
cout << " length = " << queue->getLength() << endl;
cout << " size = " << queue->getSize() << endl;
cout << " first element = " << queue->getFirstElement();
cout << endl;
cout << endl;
return;
}
void printQueue(Queue *queue)
{
cout << "Queue elements:" << endl;
if (queue->isEmpty())
{
cout << " The queue is empty!" << endl;
cout << endl;
return;
}
while (queue->isEmpty() == false)
{
int element = queue->dequeue();
cout << " " << element << endl;
if (element == -1)
break;
}
cout << endl;
return;
}
And here is what I get when I try to build it:
1>------ Build started: Project: Queue, Configuration: Debug Win32 ------
1>Compiling...
1>Driver.cpp
1>c:\users\bj\school\data structures and algorithms\queue\queue\queue.h(52) : warning C4717: 'Queue::isEmpty' : recursive on all control paths, function will cause runtime stack overflow
1>c:\users\bj\school\data structures and algorithms\queue\queue\queue.h(78) : warning C4717: 'Queue::dequeue' : recursive on all control paths, function will cause runtime stack overflow
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\BJ\School\Data Structures and Algorithms\Queue\Queue\Debug\BuildLog.htm"
1>Queue - 0 error(s), 2 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Any ideas?