The basics of programming in c++ for beginners

List bidirectional. Sorting on fields and conditions

In this article, I will describe the technique works with a bidirectional list in its classical form. Entry list, O and sort by various conditions and fields of the list elements. I will not go into the theory of lists – I move on to a description of, what needs to be done to solve this problem, and along the way I will describe why so I arranged a job.

To build a list we need two structures: one – field list item. Second – he list item with anchors, which will bind the elements together.

The first structure may look like this:

This is a list of the data structure. It must contain the field, directly to the list itself (or rather to its structure ) have no relationship, but store information. This entry – container (let's call it so).

The second entry:

She had intended to store a container element, in which we will pack data, and a field for communication with neighboring elements in the list,.

What is needed such a turn on two structures? It will be easier to sort. Under normal circumstances, the list is sorted by redirecting its field-anchors on other elements (Next and Prev in this example). That is. The data itself, as they were in the memory as in the same cell (cell) and remain, but only change the pointers to the neighbors. And it certainly is good and right, but difficult. The higher complexity, the more likely to run into a bug in the program. Therefore it is necessary to simplify the program, so you can not change the anchors, and places data (as it is usually done in sorting arrays for example). Therefore appropriate data in a separate block structure, to drag them one operator, rather than drag each field separately. Below you will see what is meant.

To work with the list of required variables and the head of the list, of necessity, tail:

And so it will have to look the list filling procedure:

It can also be divided into two: The first element would be created, the second would have filled him with a container useful data, but it is to taste.

Do not forget about the release of the list procedure. Leaving garbage in memory – bad taste, Even if intelligent Operating System itself can be cleaned:

O procedure:

So much it is due “orderliness”. Beautiful display – important enough optionality program. Therefore, the width of the elements, and table view should comply.

Now on to the fun – sort Lists. The procedure of the sort I've broken into two parts, removing it from the test conditions, who analyze whether the sortable elements should be promoted by, depending on the conditions of the list. The very same sorting procedure is as follows:

It will agree to transfer the sorting options: field Name, which is to be sorted, and the sort direction (asc – ascending or desc – descending).

In the procedure used bubble sort, as the most chassis. In a double loop is a call-function analyzer, which must respond sorting, do you need to rearrange the elements to be sorted or not. view, removing sort conditions, I simplified the code itself sorting procedures. Even if you need to add some more conditions or fields, sort itself will not have to touch.

Analyzer function looks like this:

It performs 4 conditions:

  1. If asc – Ascending, and the field being sorted – х:
  2. If desc – descending, and the field of the same
  3. If asc – Ascending, but sorted second field, strokovoe
  4. If desc – Descending from a string field

In each of the conditions, respectively, are compared “more” or “less” depending on the set of collations. Thus, this function is responsible sorting procedure, how she deal with the elements. In general, it is bi-directional full list sorting algorithm.

We are putting it into a single program described above and the function appends main():

Note: I point out the words, how to sort a list of, for which field and in which order the.

Result:

Sorting bidirectional list in C ++

On the second list, the sorting method by perezatsepleniya anchors can read on programmersforum

At the request of colleagues on site (which of course have noticed correctly) worth at least a brief mention of C ++ and the virtues STL. I mean the class list, which actually represents Lists (bi-directional). The promise of a simple: It is all, you need to work with a list of already done. On good, programmer, working with lists, of course, it is in domestic cases, elect to work STL.

I'll describe a small code using the container, as an alternative to, as described above:

Sorting bidirectional list in C ++

If the task can be used STL – use. The solution is reliable and will not be spending time on their lists of the invention the storage mechanism.

The choice of these two methods should do depending on the quantity of useful data, that are entered into a list item. If it's a couple of dozen fields with numbers or strings, the easiest way, just – moving the container with useful data (like here). If the list items have a huge size, the faster technique anchors reshuffle will be carried out (changes Last and Next for perezatsepleniya element to another element). In any case, the choice of the programmer.

3 thoughts on “List bidirectional. Sorting on fields and conditions

  1. It’s cool but this your algorithm replaced only data fields in nodes. If you gotta do great job you need replace pointers:
    void Sort(Node *&Top, Node *&End) {
    if (Top && Top->next) {
    Node *current = Top;
    Node *prev = nullptr;
    Node *tmp = nullptr;
    bool flag = false;
    for (int i = 0; i next) {
    tmp = current->next;
    if (current->data > tmp->data) {
    flag = true;
    current->next = tmp->next;
    tmp->next = current;
    if (prev)
    prev->next = tmp;
    tmp->prev = prev;
    prev = tmp;
    if (current == Top)
    Top = tmp;
    if (!current->next)
    End = current;

    }// The if end
    else {
    prev = current;
    current = current->next;
    }
    }// The while end
    if (!flag)
    break;
    else {
    current = Top;
    prev = nullptr;
    flag = false;
    }
    }// The for end
    }
    else
    cout << "\nNo entries for sorting\n";
    }

    1. Azizjan, Read carefully the article. The reference to the method, where the data remains in its place, I gave the sentence
      “On the second list, the sorting method by perezatsepleniya anchors can read on programmersforum”
      I am sure that beginners and useful to know such and such a method.

Leave a Reply

Your email address will not be published. Required fields are marked *