BDE 4.14.0 Production release
|
Provide utilities to maintain bidirectional list data structures.
This component provides a namespace, bslalg::BidirectionalLinkListUtil
, containing utility functions for operating on doubly linked lists with nodes of type bslalg::BidirectionalLink
. The operations assume that the linked lists are either 0 terminated (on both ends) or terminate with sentinel (valid) nodes. The main operations include insertion and removal of a node from a list of nodes, and the splicing of ranges from one list into another one. Splicing is the operation of moving a sub-list, or range, of elements from one linked list and into a second list, at a specified position.
This section illustrates intended usage of this component.
First, since Link
neither has a constructor nor is a POD (which would make aggregate initialization possible), we create a function makeLink
to assemble a link from two pointers:
Then, we create a function that will, passed two links that are endpoints of a linked list from the specified first
to last
though the nextLink
pointers, count the number of nodes in the list including both endpoints.
Next, in our main
, we declare a typedef
for the component name and a a constant invalid
garbage pointer we use when we want data to be garbage.
Then, we create a linked list of links and use isWellFormed
to verify that it is well formed, and call the length
method we just created to verify its length.
Next, we create two new links front
and back
, and initialize them with garbage:
Then, we use our component's insertLinkBeforeTarget
and insertLinkAfterTarget
to concatenate front
to the front of the list and back
to its rear:
Next, We examine the new list and verify we now have a well-formed list, 2 longer than the old list:
Then, we use our component's unlink
method to remove two nodes from our list. Note that the state of the removed nodes is undefined:
Next, we verify that the new list is well formed and 2 elements shorter than it was before we removed those two nodes:
Then, we weave the two discarded nodes into a new, second list of two nodes, and use isWellFormed
and length
to verify it is as we expect:
Next, we use our component's spliceListBeforeTarget
method to remove the middle nodes from the longer list and append them to the end of shorter list. Note that the splicing function not only adds the sequence to the new list, it also splices the list the sequence is removed from so that both are well-formed lists:
Then, we use isWellFormed
and length
to verify the state of our two lists:
Next, we call spliceListBeforeTarget
again to join our two lists into one:
Now, we use isWellFormed
and length
to verify the state of our one remaining list:
Finally, we traverse our list in both directions, examining each node to verify all the links and that the sequence is as expected: