concept middle in category c++

appears as: middle
Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

If you need to move selected items to the middle of the list, you can split the list into the part above the destination point and the part below. Then, you have one list in which the selected items should go to the bottom and one list in which they should go to the top.

A list is a collection of nodes; each node contains a single value and a pointer to the next node in the list (nullptr if it’s the last element of the list), as shown in figure 8.1. The basic operations on a list are adding and removing elements to the start or the end of the list, inserting or removing elements from the middle, or changing the stored value in a specific node.

8.1.3 Adding elements to and removing them from the middle of a list

To add an element to or remove it from the middle of a list, you need to create a copy of all elements that come before it in the original list—just as when changing the end of the list (see figure 8.6). More specifically, you need to remove all the elements that come before the location you want changed. This will give you the part of the original list that you can reuse in the resulting list. Than you insert the desired element and re-add all the elements from the old list that should go in front of it.

Figure 8.6 Inserting an element into the middle of a list is equivalent to removing all the items from the original list that should come before the element you’re inserting and then prepending that element and all the previously removed items. The only nodes you share with the original list are nodes that come after the item you’ve inserted.

c08_06.png