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 ordervalue
,prev
,next
, or alternatively has a constructor that takes three parameters in this order.p
is a pointer to theNode
that the new node should go before.
On these assumptions:
new Node{ x, p->prev, p }
new
s up a new Node, setting its value tox
, its previous node asp->prev
, and its next node asp
. It returns a pointer to this new node.p->prev->next = /*...*/
setsnext
ofp->prev
– i.e., the node previously beforep
– to point to this freshly created node. The value of this expression is the value assigned (formally, it’s an lvalue referring top->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 makesp->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 beforep
.- Finally,
iterator(/* ... */)
constructs a iterator pointing to the new node, andreturn
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?