Hello,
I am reading "Programming Principles and practice Using C++" by Stroustrup. Now I'm on chapter 17.9.3(pointer and Linked list). I understand how to use pointer.
But I can't understand the following notation.
/*in the book Stroustrup represented a short List of Norse gods.
The purpose of this example to show how working linking List
*/
#include "../../std_lib_facilities.h" //library comes with book
struct Link
{
public:
string value;
Link* prev; // pointer to predecessor
Link* succ; // pointer to successor
Link(const string& v, Link* p = 0, Link* s = 0)
: value(v), prev(p), succ(s) { }
};
int main()
{
Link* norse_gods = new Link("Thor", nullptr, nullptr);
norse_gods = new Link{ "Odin", nullptr, norse_gods };
norse_gods->succ->prev = norse_gods; // I don't understand how is
//working this notation
norse_gods = new Link{ "Freia", nullptr, norse_gods };
norse_gods->succ->prev = norse_gods; // and this
keep_window_open();
return 0;
}
Please explain me this notation:
norse_gods->succ->prev = norse_gods;
Once you've created a new Link, for the following Link you want it to became the 'previous'.
In different words, you want the 'previous' pointer of the following Link to point to this new Link.
Again: once you have get to the following Link, you want be able, following "prev", to get back here.
To do so: starting from 'here' (the newly created Link), get hold of the following Link by means of "succ"; and tell that Link that 'here' must become its "prev".
- - -
Well, re-reading what I wrote, I'm afraid my English is far worse than the C++ code.
Hope someone else comes to explain better.