Here's an example of a singly-linked-list i wrote up quickly the other day:
http://cboard.cprogramming.com/showt...=100265&page=2
Note how the "next" pointer is used to manipulate the list.
To understand more of how a queue works and what functionality it has check this out on the standard queue:
STL Queue
Now that I have some more time, let's analyze what you have here a little more in depth:
1. I'm guessing that your assignment is something like you are given this QueueItem class, and you need to make a Queue container of these items, yes?
2. You said:
Along those lines, the getdata() function was provided by the instructor, but it isn't being passed any values. That's why I have to use mpHead->getData() in the print function, right?
No. The reason you need to say mpHead->getData() is because getData() is how you "get the data". const char* data is a private member of the QueueItem class, therefore the only way to "get the data" is to use the public function getdata() that was provided for you. "mpHead->getdata()" is simply the syntax used for a pointer to access the getdata() function.
That's how classes work. They have private data associated with them, and a public interface which allows a user to use the class. Pointers to a class can also do the same things as objects.
Example:
Code:
MyObj object;
object.setData("I'm an object");
MyObj *object_pointer; // Declare pointer to MyObj.
object_pointer = new MyObj(); // Create an instance MyObj
object_pointer->setData("I'm a pointer to an object"); // Use a member function with pointer syntax
Note that the first two lines show the creation of an actual instance of the class MyObj, and the last three lines show how to use a pointer to MyObj objects.
A pointer does one thing, it points. What does it point to? Whatever type you declare it as. An int *ptr, points to integers, QueueItem *qptr, points to objects of type "QueueItem". The great thing about pointers, is we can tell them to point to something else (as long as it's the same type). This is important for "linkable" data structures such as linked-lists, stacks, queues, dequeues, etc.
These types of data structures require a pointer to which refers to the "next" item in the list, stack, queue, etc. The next pointer itself is a private member of the class. This means that each item in the queue, has a next pointer. And what does it point to? The next item.
To help you visualize this:
Code:
// Items in the queue:
[5]->[15]->[97]->[12]->[39]->NULL
So, just by looking at that we know that whatever object this is in the queue has private members that are an integer and a next pointer. The class for the above might look like:
Code:
class integerClass{
public:
// ...
private:
int data;
integerClass *next;
};
Now, let's review quickly how classes work. How can we "get the data" for this class? We simply provide the function in the public interface like this:
Code:
class integerClass{
public:
int get_integer_data(){ return data; };
private:
int data;
integerClass *next;
};
We can just as easily provide a "get" function for any private data. The only thing you need to do is write the function like you would any function, with the correct return type. So if we wanted to return the next pointer, we could say:
Code:
integerClass* get_next(){ return next; };
note, we don't need to pass anything to these functions. Because both the private and public sections of any class are in fact a part of that class, they automatically have access to their own data.
Now, mpHead and mpTail are both pointers to QueueItems the same way mpNext is a pointer to QueueItems. They're exactly the same in every respect, except that every QueueItem has a next pointer, and only the Queue itself has mpHead and mpTail. Consider this:
Code:
mpHead->getData();
mpHead->next->getData();
mpHead->next->next->next->getData();
mpTail->getData();
are all legal expressions, though I wouldn't recommend chaining like that.
Let's apply those above expression to the queue illustration i made earlier:
Code:
// Everything in the queue:
[5]->[15]->[97]->[12]->[39]->NULL
// Okay, the HEAD is 5 and the TAIL is 39
// so the above expressions return like this:
mpHead->getData(); // returns 5
mpHead->next->getData(); // returns 15
mpHead->next->next->next->getData(); // returns 12
mpTail->getData(); // returns 39
So the whole point of the next pointer is to refer to the next object. This is necessary to even construct a cohesive structure like queue.
As I said, check out my linked-list example that I provided the link for. Although it's not a queue, all the information you would need to construct a queue is there.