0

I am a novice programmer and this is my second question on Stack Overflow.

I am trying to implement a pushback function for my Linked List by using a tail pointer. It seems straightforward enough, but I have a nagging feeling that I am forgetting something or that my logic is screwy. Linked Lists are hard!

Here is my code:

template <typename T>
void LinkedList<T>::push_back(const T n)
{
Node *newNode;  // Points to a newly allocated node

// A new node is created and the value that was passed to the function is stored within.
newNode = new Node;
newNode->mData = n;
newNode->mNext = nullptr; 
newNode->mPrev = nullptr;

//If the list is empty, set head to point to the new node.
if (head == nullptr)
{
    head = newNode;
    if (tail == nullptr)
    {
        tail = head;
    }
}
else  // Else set tail to point to the new node.
    tail->mPrev = newNode;
}

Thank you for taking the time to read this.

4
  • 1
    First, if head is null, tail should also already be null, or something went horribly wrong. Second, if tail points to the last node in the list, shouldn't your newNode->mPrev point to that (tail), then set tail = newNode;? Commented Sep 21, 2016 at 3:22
  • 1
    Before writing any code, you should have drawn your linked list on paper, using boxes as the data and the lines between the boxes as links. Then translate what you see on paper to code -- if you did that, clearly what you're doing seems to be wrong, as WhozCraig pointed out. Commented Sep 21, 2016 at 3:24
  • WhozCraig, you're absolutely right. My else statement should have been newNode-mPrev = tail. I knew I was making a stupid error! Paul, I did write some of it out on paper. I should have written it all out! Thank you for the advice. Commented Sep 21, 2016 at 3:30
  • @RyanSwanson yeah, but you never set tail->mNext = newNode in the case of a non-null tail (which you can infer from a non-null head), which is critical for this to work. See answer below. Commented Sep 21, 2016 at 3:33

1 Answer 1

3

Your pointing the wrong mPrev to the wrong node. And you never set mNext of the prior tail node if it was non-null to continue the forward chain of your list.

template <typename T>
void LinkedList<T>::push_back(const T n)
{
    Node *newNode;  // Points to a newly allocated node

    // A new node is created and the value that was passed to the function is stored within.
    newNode = new Node;
    newNode->mData = n;
    newNode->mNext = nullptr;
    newNode->mPrev = tail; // may be null, but that's ok.

    //If the list is empty, set head to point to the new node.
    if (head == nullptr)
        head = newNode;
    else
        tail->mNext = newNode; // if head is non-null, tail should be too
    tail = newNode;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch, WhozCraig. Really helped me come to grips with this process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.