Hey everyone,
I've finally gotten my program to build with no errors or warnings and I can run it but it doesn't give all of the correct output. The Instructor provided my class with what the correct output should be and my program gets it partly right just not all of it. Here's the correct output:
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue properties:
isEmpty = 0
length = 5
size = 10
first element = 10
Queue elements:
10
13
32
3
16
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue elements:
The queue is empty!
Queue properties:
isEmpty = 0
length = 10
size = 10
first element = 0
Queue elements:
0
10
20
30
40
50
60
70
80
90
And here's the output that I get from my program:
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue properties:
isEmpty = 0
length = 5
size = 10
first element = 10
Queue elements:
10
13
32
3
16
-1
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue elements:
The queue is empty!
ErrorErrorErrorErrorErrorErrorErrorErrorErrorErrorQueue properties:
isEmpty = 0
length = 10
size = 10
first element = 0
Queue elements:
-1
(I tried to put them side by side for easier comparison but it wouldn't keep the formatting.)
Ok, and here is my code:
Header file:
// Queue.h: This file contains the definition and implementation
// for the queue class.
#include <iostream>
using namespace std;
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
//
for (int i=0; i < size; i++)
{
queue[i] = -1;
}
length = -1;
}
bool Queue::isEmpty()
{
//
// TODO:
// This method checks to see if the queue is empty
// return true if the queue is empty, false otherwise
//
if (length == -1)
return true;
else
return false;
}
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 + 1 >= size)
cout << "Error";
else
length++;
queue[length] = 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.
//
int element = -1;
if (length + 1 >= size)
{
return -1;
}
else
{
element = queue[0];
}
for (int i = 0; i < size; i++)
{
queue[i] = queue[i+1];
}
return element;
}
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 queue[0];
}
int Queue::getLength()
{
return (length+1);
}
int Queue::getSize()
{
return size;
}
Driver
// Driver.cpp: This file contains the source code to test your
// queue class implementation.
#include <iostream>
#include "Queue_test.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;
}
Anyway, I'm really at a loss on what I should do in this code to give the correct output. Does anyone have any suggestions?