1

I am trying to figure out the following code to implement a push_back function for a linked list but I'm not quite sure why we need back_ptr->next and back_ptr to both point to p. I believe back_ptr->next could just point to NULL for it work, is there any advantage of implementing it as such that I'm missing?

void LinkedList::push_back(int element) {
    Node *p = new Node;
    p->element = elememt;
    p->next = 0;
    if (empty()) {
        front_ptr = back_ptr = p;
    } else {
        back_ptr->next = p;
        back_ptr = p;
    }
}

The following is the LinkedList class prototype. The back_ptr is being used to point to the end of the list for implementing the copy constructor (push_back makes it a lot easier to copy the list).

class LinkedList {
    void push_back(int element);
    // other member functions

    private:
    struct Node {
        Node *next;
        int element;
    };
    Node *front_ptr;
    Node *back_ptr;
};
1

2 Answers 2

1
push_back(1);
push_back(2);
Node *p = new Node;
p->element = 3;
p->next = nullptr;
 front_ptr      back_ptr         p
     ↓             ↓             ↓
┌────┬────┐   ┌────┬────┐   ┌────┬────┐
| #1 |next| → | #2 |next|   | #3 |next| → nullptr
└────┴────┘   └────┴────┘↘  └────┴────┘
                          nullptr
back_ptr->next = p;
 front_ptr      back_ptr         p
     ↓             ↓             ↓
┌────┬────┐   ┌────┬────┐   ┌────┬────┐
| #1 |next| → | #2 |next| → | #3 |next| → nullptr
└────┴────┘   └────┴────┘   └────┴────┘
back_ptr = p;
 front_ptr             back_ptr  p
     ↓                         ↘ ↓
┌────┬────┐   ┌────┬────┐   ┌────┬────┐
| #1 |next| → | #2 |next| → | #3 |next| → nullptr
└────┴────┘   └────┴────┘   └────┴────┘
Sign up to request clarification or add additional context in comments.

4 Comments

Just curious , how did you come up with the graphic
@AmitKumar I arranged Unicode block drawing characters and arrows manually. You can see the source in the revision history. (It's not very interesting.)
Wow the visuals make it a lot easier to understand. I guess I was missing the ordering of back_ptr->next and back_ptr updates (I thought they could have been written in any order). Thanks!
Next time, please use an actual graphic made in an image editor, do not use text art. It doesn't always look good on everyone's screen. A simple bitmap created in something like MSPAINT would look much cleaner than what you have shown.
0

Let me explain if the list is not empty at the time of push back, the node which is currently tail should point its next to new node and finally tail should point to new node.

     Before push back 
     tail-> node x // tail points to node x
        x->next = null // as it is tail
     After push back new node y
       tail->next = y 
     As x was earlier pointed by tail ,this means x->next = p,

This step ensures the list remains connected.

     Finally , point the tail to the new node  
     tail -> y

Comments

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.