Need help with FIFO Queue using Singly Linked Lists
Let me preface this by saying that this IS a homework assignment but I'm not looking for someone to code this for me. I am just having a really difficult time sorting through all of this and I've been everywhere on the internet and I have 4 C++ books sitting in front of me and the lecture notes have not been particularly helpful. This program is supposed to use class Queue to manipulate data and class QueueItem to store the data. It is incomplete because the assignment is more complicated than this particluar problem, but I'm not concerned with all of that until I can get the basic structure down. The code below compiles, although it prints what appear so to be an octal memory address, rather than the char string "test". I'm trying to step through the code bit by bit so many fuctions are not included. My question is this: How do I pass "test" to function Queue::addItem(char *pData, ++mNodeCounter). I'm struggling because pData and the char mData[30] array belong to different classes and char mData[30] is private. Again, I'm not looking for someone to code the entire program for me, I just need a clarification on how to pass "test" to a function and store it and/or a little explanation of what is going on in the Queue implementation as far as storing data. I'm trying to learn how to do this correctly for future assignments, but again I've been through countless resources and I have found little in the way of instruction involving using this many pointers and classes to write a FIFO queue. Thank you in advance for your help.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class QueueItem {
public:
QueueItem(char *pData, int id); // ctor
void setNext(QueueItem *pItem);
QueueItem* getNext();
int getId();
const char* getData();
private:
char mData[30]; // or, use a char* if you want to dynamically alloc memory
int mNodeID;
QueueItem * mpNext;
};
QueueItem::QueueItem(char *pData, int id)
{
id = mNodeID;
}
class Queue {
public:
Queue(); // ctor inits a new Queue
~Queue(); // dtor erases any remaining QueueItems
void addItem(char *pData);
void removeItem();
void print();
void erase();
private:
QueueItem *mpHead; // always points to first QueueItem in the list
QueueItem *mpTail; // always points to the last QueueItem in the list
int mNodeCounter; // always increasing for a unique id to assign to each new QueueItem
};
Queue::Queue() {
mpHead = NULL;
mpTail = NULL;
}
void Queue::addItem(char *pData)
{
// dynamically create and init a new QueueItem
QueueItem *pQI = new QueueItem(pData, ++mNodeCounter);
if (0 == mpHead) // chk for empty queue
mpHead = mpTail = pQI;
else
{
// link new item onto tail of list using mpTail pointer
mpTail = pQI;
}
}
void Queue::removeItem()
{
delete mpHead;
}
void Queue::print()
{
cout << mpHead << endl;
cout << mNodeCounter << endl;
}
Queue::~Queue()
{}
int main() {
Queue myQueue;
myQueue.addItem("test");
myQueue.print();
getchar();
}