2017-04-19 58 views
-1

我只是有一个关于如何打印队列中最后一个元素的快速问题。这是我到目前为止:用尾指针打印节点的键

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. 

谢谢!

+0

如果您尚未打印所有具有该循环的元素,则必须断开您的链接。 – molbdnilo

回答

0

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

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