Hacker News new | ask | show | jobs
by iano 4422 days ago
Can someone explain to me how the first queue example's (using singly link lists) dequeue method gives the expected result? Isn't head->m_pNext always NULL, so the new head will always be NULL and tail will be set to NULL losing the rest of the queue?

Seems like you should need to traverse the list to dequeue.

Where did I go wrong?

2 comments

You're right. Enqueue is wrong, it should be:

    if ( !m_pHead )
        m_pHead = p ;
    else
        m_pTail->next = p;
    m_pTail = p;
For some reason, the author is inserting the node in front of the tail leading to the error.
The complex version doesn't even alter m_pHead in enqueue().