[Solved] what is the best way to interpret the following return statement in data structure?


Looks like

  • This is a doubly linked list.
  • iterator is a class.
  • Node is also a class.
  • x is the value to be inserted.
  • Node is an aggregate that has three data members in the order value, prev, next, or alternatively has a constructor that takes three parameters in this order.
  • p is a pointer to the Node that the new node should go before.

On these assumptions:

  • new Node{ x, p->prev, p } news up a new Node, setting its value to x, its previous node as p->prev, and its next node as p. It returns a pointer to this new node.
  • p->prev->next = /*...*/ sets next of p->prev – i.e., the node previously before p – to point to this freshly created node. The value of this expression is the value assigned (formally, it’s an lvalue referring to p->prev->next that is then converted to an rvalue, i.e., has the stored value read), which is the pointer to the new node.
  • p->prev = /* ... */ finally makes p->prev also point to this new node as well. The result of this expression is again the pointer to the new node. The end result is that the new node is inserted before p.
  • Finally, iterator(/* ... */) constructs a iterator pointing to the new node, and return returns it to the caller.

Code like this shouldn’t be used. It may look “cute”, but it’s difficult to read and understand, and my first reaction when I read it is “are you sure it’s not undefined behavior”? It took me a few moments to convince myself that the behavior is well-defined (in C++11, at least).

1

solved what is the best way to interpret the following return statement in data structure?