-1

I just have a quick question about how you would print the last element in a queue. Here is what I have so far:

struct queue {
node * head;
node * tail;
};

void printQ(queue & q) {
node * p = q.head;

cout << "QUEUE: ";

if (q.head == NULL)
    cout << "empty";

while (p != NULL)
{
    cout << p->key << " ";
    p = p->next;
}

cout << "    TAIL=" << ??????  // This is where I would like to
                                  get it to print the tail but I'm not 
                                  sure how.

Thanks!

Buffalo282
  • 29
  • 1
  • 1
  • 6

1 Answers1

0

I'm supposing you have node struct with key and pointer to next node

When you are inserting elements in queue, check if queue is empty. If so, make the new element the tail node. And when you want data of tail of queue q, do (q.tail)->data.

maigar
  • 197
  • 10