Originally Posted by
R.Stiltskin
Here's the output I get after making the corrections I suggested to you:
Code:
one
4
two
4
three
4
four
4
three
8
four
8
five
8
six
8
seven
8
eight
8
seven
8
eight
8
nine
11
ten
11
eleven
11
which is exactly what your main() calls for. The numbers don't correspond to the node labels because you aren't printing nodeIDs, you're printing mNodeCounter which is always the ID of the LAST node added to the queue. If you want each nodeID, change printList() to print temp->getID instead.
Your QueueItem constructor, and Queue::remove(), Queue::eraseList() are all OK. So your printList() must still be bad (unless you changed & messed up something else).
here's what mine look like:
Code:
//member function definitions for class Date
#include <iostream> //allows program to output data on screen
using std::cout;
using std::endl;
using namespace std;
#include <iomanip>
using std::setfill;
using std::setw;
#include <cstring>
using std::string;
using std::strcpy;
#include "Queue.h" //include definition of class Queue from Queue.h
//constructor
Queue::Queue()
{
mpHead = NULL;
mpTail = NULL;
mNodeCounter = 0;
}
void Queue::addItem(char *pData)
{
//create and initialize new QueueItem dynamically
QueueItem *pQI = new QueueItem(pData, ++mNodeCounter);
if ( mpHead == 0 ){ //check for empty queue
mpHead = mpTail = pQI;}
else{
// link new item onto tail of list using mpTail pointer
mpTail->setNext(pQI);
mpTail = pQI;
}
}//end addItem function
//function removeItem
void Queue::removeItem()
{
if (mpHead == NULL){
return;}
else
{
QueueItem* temp = mpHead;
mpHead = mpHead->getNext();
delete temp;}
}
//end removeItem function
//Erase List
void Queue::eraseList()
{
while (mpHead != NULL)
removeItem();
mpHead = mpTail = NULL;
}//end eraseList function
//prints contents of queue
void Queue::printList()const
{
QueueItem* temp = mpHead; //assign mpHead to temporary QueueItem
cout << mpHead->getData() <<"\t\t"<< mNodeCounter <<endl;
temp = temp->getNext();
}//end printList function
//destructor
Queue::~Queue()
{
eraseList();
}
Code:
//member function definitions for class QueueItem
#include <iostream> // allows program to output data
using namespace std;
#include <cstring>
#include "QueueItem.h" // include definition of class QueueItem from QueueItem.h
//QueueItem constructor
QueueItem::QueueItem(char *pData, int id)
{
strncpy_s(mData, pData, 30);
mNodeID = id;
mpNext = NULL;
}// end constructor
void QueueItem::setNext(QueueItem *pItem)
{
mpNext = pItem;
}// end setNext function
QueueItem* QueueItem::getNext()
{
return mpNext;
}//end getNext function
int QueueItem::getId()
{
return mNodeID;
}//end getId function
const char* QueueItem::getData()
{
return mData;
}//end getData function