2017-04-19 39 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。