通过尾指针打印节点的钥匙



我只是关于如何在队列中打印最后一个元素的快速问题。这是我到目前为止所拥有的:

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.

谢谢!

我认为您的节点结构带有键和指针,指向下一个节点

当您在队列中插入元素时,请检查队列是否为空。如果是这样,请将新元素作为尾部节点。当您想要队列尾巴的数据时,请执行(q.tail( ->数据。

最新更新