【问题标题】:Printing the Key of Node by Tail Pointer通过尾指针打印节点的键
【发布时间】:2017-04-19 03:52:19
【问题描述】:

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

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.

谢谢!

【问题讨论】:

  • 如果您尚未使用该循环打印所有元素,则必须断开链接。

标签: c++ printing queue tail


【解决方案1】:

我假设你有一个带有键和指向下一个节点的指针的节点结构

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2020-08-17
    • 2021-02-19
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多