You can not do this so easily.
I posted some code in response to one of your questions that I realize was somewhat of an overkill and used templates (
http://www.cplusplus.com/forum/general/33947/), but it gives you the starting point for a node based list that can insert at the beginning and end.
You can not modify the
this pointer to point to another location. First, because it is not an lvalue (variable) and second, because the client code does not necessarily call the method through a pointer. It may call the node's method from an object allocated on the stack (using the dot notation.) Even though you may know that the client holds a pointer to the object (the node) and calls your methods through the pointer, you can not alter the pointer variable that the client holds using the
this value on the left side of some assignment expression in the called method.
Let me again explain the dummy/sentinel node approach.
Your problem is:
Someone holds reference/pointer to the first and/or last nodes and you wish that their references will never be invalidated. Even when you insert at the beginning/end.
Possible solution:
Make the first and last nodes dummies.
Solution details:
- Whenever someone asks you to give them the previous/last node, first check if it is not a dummy node. You will know, because the dummy nodes are at the ends and therefore have no preceding/following node. If you see that the client has reached one of the two dummy nodes in the list during traversal, then pretend that there is nothing there and behave as if you reached the end of the list already. Basically, you hide the dummy nodes, as if they are some kind of spies or something.
- You should probably offer a procedure for creating an empty list. The empty list contains only the two dummy nodes linked to each other. In the code I posted previously (the one with templates) this role is played by the List class. I know this makes the architecture appear as not entirely node-based, but it is simply a class that captures the logic of creating an empty list with the two dummy nodes and offers access to the first and last actual nodes in the list (to skip over the dummies - see below). Nothing else. You don't want to involve the user with the construction of dummy nodes, the obligation to link them manually, and the knowledge of how to use them to acquire the first actual node during traversal.
- You should have a procedure, which given the starting dummy node, returns pointer to the first actual node. This is simply the node that follows the starting dummy node in your internal chain of nodes. I have provided this function as method in the List class which I posted, because thus the logic is best encapsulated.
- You should have a procedure, which given the ending dummy node, returns pointer to the last actual node. This is simply the node that precedes the ending dummy node. This I have also implemented in the List class.
Note: Whether you use a special List class to hold the two dummy nodes and manage their lifetime or you make this responsibility of the user code, you still have a node-based solution. The idea of a node based solution is that you can insert/remove from the collection without ever referring to container objects. You don't need the List class in order to insert and remove nodes. You need it only to acquire the first and last nodes from the list in the event that you want to traverse the nodes end to end.
Solution advantages:
When you insert at the beginning/end of the list as the client sees it, you insert at the second/next to last position of your internal chain. The new node will be linked in between the former first/last actual node in the list and the corresponding dummy node (for that end). The clients can keep their references to the dummy nodes, because they are never relocated by your code. Not until the list is destroyed.
Solution deficiencies:
You can not safely keep references to arbitrary positions in this "augmented" list. Only to the dummy nodes, and indirectly through them to the first and last position. References to intermediate positions are invalidated as always.
Regards