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?

You're calling the functions within themselves so it's going to proceed through those calls until the stack space runs out. isEmpty() will call isEmpty() which will call isEmpty()...etc. ad nauseum. You have a variable that keeps track of the number of items on your stack, use that in your method instead.

Same idea with dequeue. EDIT: You do it with enqueue also but the compiler doesn't complain about it because it's in an if statement, but that should certainly be addressed as well. Going back over it it seems like you are having a hard time with the concepts. Sit down and go through your instructors TODOs step by step and plot out on paper what you need to do for each.

Edited 6 Years Ago by jonsca: n/a

You're calling the functions within themselves so it's going to proceed through those calls until the stack space runs out. isEmpty() will call isEmpty() which will call isEmpty()...etc. ad nauseum. You have a variable that keeps track of the number of items on your stack, use that in your method instead.

Same idea with dequeue. EDIT: You do it with enqueue also but the compiler doesn't complain about it because it's in an if statement, but that should certainly be addressed as well. Going back over it it seems like you are having a hard time with the concepts. Sit down and go through your instructors TODOs step by step and plot out on paper what you need to do for each.

Well, you're definitely right about me having trouble with the concepts. I was pretty sure that was the root of my problem. For this class, I feel like my book doesn't explain things in great detail. I know I'm not stupid but I am already having trouble making sense of what is explained in this textbook as opposed to other CS textbooks for other CS classes. Anyway, I'll try to do what you said, which sounds like I need to write out pseudocode for each TODO. I'm just certain that I may do that but still not know how to implement the statements within that pseudocode. Oh well, I'll give it a try and see what I can come up with. Thanks for responding.

Hi..

Understand that every recursive function needs atleast one limiting condition ( something that will stop the recursion).. as Jonsca said go through the basics again..

The changes are not going to be drastic. I would eliminate all the recursive parts as that's probably not what you intended anyway. Just think in terms of, ok, how do I determine if the queue is empty. Check the length and return true if the length is zero. Your instructor has taken the time to delineate the steps for you, so just flesh them out one at a time.

The changes are not going to be drastic. I would eliminate all the recursive parts as that's probably not what you intended anyway. Just think in terms of, ok, how do I determine if the queue is empty. Check the length and return true if the length is zero. Your instructor has taken the time to delineate the steps for you, so just flesh them out one at a time.

This is in response to Jonsca and Crak:

I'm sorry guys but, the classes I've taken so far haven't really even talked much about recursive things. All I know about that is from a class called Applied Discrete Math and if I remember correctly, it means that something is recursive if it uses itself to define itself... Not sure if I put that quite right but anyway, I have no real experience applying it to computer science/programming. I think I have an idea of what you guys mean but I've been up all night trying to figure this out. I need to get some sleep and give it a fresh try after I wake up. I'll see what I can come up with after some sleep but I may be back on later with more questions. Sorry for being so lost with this stuff and asking (what are probably) stupid questions. I REALLY do appreciate you guys taking the time to try to point me in the right direction. Thanks again.

BobbieJean

This question has already been answered. Start a new discussion instead.