I am working on this linked queue class program and it runs without any compiler errors but there is a break point in my dequeue function. It is pointing towards:
"item = qFront-> info;" line 83
in the dequeue function and I am not sure how to fix this error. Can anyone help? My code is posted below. Thanks.
#include <iostream>
#include <cstddef> // For NULL
#include <new> // For bad_alloc
using namespace std;
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType <ItemType> * next;
;};
template <class ItemType>
class QueType
{
public:
QueType (); // CONSTRUCTOR
~ QueType (); // DESTRUCTOR
bool IsEmpty () const;
bool IsFull () const;
void Enqueue (ItemType item);
void Dequeue (ItemType & item);
void MakeEmpty ();
void Print();
QueType (const QueType& anotherQue);
private:
NodeType <ItemType> * qFront;
NodeType <ItemType> * qRear;
;};
template <class ItemType> QueType <ItemType>:: QueType () // CONSTRUCTOR
{
qFront = NULL;
qRear = NULL;
}
template <class ItemType> QueType <ItemType>:: ~ QueType () // destructor
{
MakeEmpty ();
}
template <class ItemType> bool QueType <ItemType>:: IsEmpty () const
{
return (qFront == NULL);
}
template <class ItemType> void QueType <ItemType>:: Enqueue (ItemType newItem)
// Adds newItem to the rear of the queue.
// Pre: Queue has been initialized.
// Queue is not full.
// Post: newItem is at rear of queue.
{
NodeType <ItemType> * ptr;
ptr = new NodeType <ItemType>;
ptr-> info = newItem;
ptr-> next = NULL;
if (qRear == NULL)
qFront = ptr;
else qRear-> next = ptr;
qRear = ptr;
}
template <class ItemType> void QueType <ItemType>:: Dequeue (ItemType & item)
// Removes element from from front of queue
// And returns it in item.
// Pre: Queue has been initialized.
// Queue is not empty.
// Post: Front element has been removed from queue.
// Item is a copy of removed element.
{
NodeType <ItemType> * tempPtr;
tempPtr = qFront;
item = qFront-> info;
qFront = qFront-> next;
if (qFront == NULL) qRear = NULL;
delete tempPtr;
}
template <class ItemType> void QueType <ItemType>:: MakeEmpty ()
// Post: Queue is empty; all elements deallocated.
{
NodeType<ItemType>* tempPtr;
while(qFront != NULL) {
tempPtr = qFront;
qFront = qFront->next;
delete tempPtr;
}
qRear=NULL;
}
template <class ItemType> bool QueType <ItemType>:: IsFull () const
// Returns true if there is no room for another NodeType
// Node on the free store; false otherwise.
{
NodeType <ItemType> * location;
try
{
location = new NodeType <ItemType>;
delete location;
return false;
}
catch (bad_alloc exception)
{
return true;
}
}
template<class ItemType>
QueType<ItemType>::QueType (const QueType& anotherQue)
{
NodeType<ItemType>* ptr1;
NodeType<ItemType>* ptr2;
if (anotherQue.qFront == NULL)
qFront = NULL;
else
{
qFront = new NodeType<ItemType>;
qFront->info = anotherQue.qFront->info;
ptr1 = anotherQue.qFront->next;
ptr2 = qFront;
while (ptr1 != NULL)
{
ptr2->next = new NodeType<ItemType>;
ptr2 = ptr2->next;
ptr2->info = ptr1->info;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
qRear = ptr2;
}
}
template<class ItemType>
void QueType<ItemType>::Print()
{
NodeType<ItemType> *tempPtr;
if ( !IsEmpty() )
{
tempPtr = qRear->next;
cout << "The queue contains: ";
while ( tempPtr != qRear )
{
cout << tempPtr->info << " ";
tempPtr = tempPtr->next;
}
cout << tempPtr->info << endl << endl;
}
else
cout << "The queue is empty." << endl << endl;
}
int main()
{
QueType<int>IntQueue;
int x;
IntQueue.MakeEmpty();
IntQueue.Dequeue(x);
IntQueue.Enqueue(10);
IntQueue.Enqueue(20);
IntQueue.Enqueue(30);
IntQueue.Enqueue(40);
IntQueue.Dequeue(x);
cout << "The int queue contains: " << endl;
IntQueue.Print();
if(IntQueue.IsFull() == false)
cout << "The int queue is not full !" << endl;
else
cout << "The int queue is full !" << endl;
QueType<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);
FloatQueue.Enqueue(2.3);
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << "The float queue contains: " << endl;
FloatQueue.Print();
QueType<float> FloatQueue2 = FloatQueue;
cout << "The float queue 2 contains: " << endl;
FloatQueue2.Print();
FloatQueue.MakeEmpty();
cout << "The float queue 3 contains: " << endl;
FloatQueue2.Print();
system("pause");
return 0;
}