Programmer account notes (16)

xiaoxiao2021-03-06  101

Today, continue to be sorted by the chain form. The day before yesterday, everyone has it. Don't understand it, it is going to come slowly, and you can't come. P = h-> next; h-> next = null; while (p) {if (P-> DATA

DATA)

{

Q = P-> next;

P-> next = H;

H = p; p = q;

}

Else

{

q = h; r = q-> next;

While (R && P-> Data> R-> DATA)

{

q = r; r = r-> next;

}

Q-> next = p; p = p-> next;

Q-> Next-> Next = R;

}

}

According to the idea of ​​this procedure, let's think about the entire process, this program is divided into two parts, part of the node value to be sorted is smaller than the node value of the head, put it in the first one. , Because if the one of the comparative head is already less than it, it should not be compared. If the node value to be inserted is not less than the current header node, then you should find the right position to insert the node, let's see what Q and R pointer are used to do, it points to the head Pointers h and r point to Q pointers, because we know that the shortcomings of the one-way linked list cannot know what it is in front of it, so it may be guided to the linked list. Our purpose is to use Q to save its previous node. There are two possibilities in the While cycle, one is R is empty, where R is empty, explaining that this linked list is already the last one, so the junction that is inserted directly is inserted. As for P-> Data> r-> DATA is to wait for P-> Data than r-> Data hours, you can continue to insert down the steps of the insertion. The While is that if these two conditions are true, the explanation has not found yet, then the two double-chain pointers will be moved afterwards, and then they can be inserted.

If you are still ambiguous, you don't want it, then look at the following procedure:

Struct node * li_sort (struct node * h)

{

Struct node * t, * s, * u, * v;

S = H-> next;

H-> next = NULL;

While (s! = NULL)

{

For (t = s, v = H; v! = null && v-> data data; u = v, v = v-> next);

S = S-> Next;

IF (v == h) h = t;

Else U-> next = T;

T-> next = V;

}

}

We can see that this program is very similar to it, but it is more simplified, and the entire judgment is in a for statement. Let's take a look at this program, I believe that if you think about it, everyone should understand. S = H-> Next and H-> Next = NULL These two sentences are the same, and they separate them into partned part and to be sorted. Following the main thing to see the for statement, because all judging conditions are here. Here T is the temporary variable generation S, the role of S is the node, V and U pointers that are currently inserted, and Q and R of the above programs are the disadvantages of the unidirectional linked list. The conditions here are also the same, and the above programs are the possibility of integrating two situations, followed by the following programs, which are inserted into the head or in the middle.

转载请注明原文地址:https://www.9cbs.com/read-124121.html

New Post(0)