Hello
Why you are using a c-style struct why you are not using a c++ class
here you are a good example
There is
nothing wrong with using a struct for the queue nodes here. If all you're storing are a few pieces of information, there's no need for an overgrown class to handle the same data.
The queue template class posted, while it is
an example (I'm assuming it works as advertised), I wouldn't call it a
good example--it's a bloated gob of code handed down from on high. It looks like Abu here is more interested in saying "ooh, look at all the features i'm using! classes! enumerations! pointers! const methods! templates!" ...all of which are useful things, but not really necessary for the problem at hand.
If you haven't run across templates yet, have a look at
http://babbage.cs.qc.edu/STL_Docs/templates.htm for a start.
Now, on to your question--the solution provided by 'infamous' will work, as will doubly-linking the list and running through the display backward. Another approach is to alter the display method:
void queueL::display()
{
display_recursive(last);
}
void queueL::display_recursive(queue *n)
{
if(n == NULL) return;
display_recursive(n->next);
cout << endl << n->data;
}
Recursion can be a handy tool when dealing with linked lists. If it's not clear, display_recursive will continue to call itself, passing each successive queue node until it hits the end of the list,
i.e. it hits a null next pointer. Then it will display them in reverse order as each recursive call terminates.
This is a little more complicated than the other two solutions; I'm just offering it as an alternative. Now you have at least three ways to get your display method to show the nodes in the desired order.
--sg