A and B are merged into the list (the list merge) C chain

A and B are merged into the list (the list merge) C chain

A list of known two ordered increment and B list (non-null), merge the two lists is C list (with increments ordered), we need to have some grasp of the concept of the list

Between nodes required conditions and nodes to establish relations have mastered.

The focus of this question is, did not specify the length of the two lists, namely two lists may not be as long, so, in dealing with the need to pay attention, when a certain list

Operation is completed, the remaining portion may be directly connected to another chain C lists them.

. 1  void Merge (LNode * A, * B LNode, LNode * C)
 2  {
 . 3      LNode * P = A-> Next;         // P A minimum tracking node, 
. 4      LNode the B-* Q => Next;         / / Q B is the minimum tracked node 
. 5      LNode * R & lt;                 // define a pointer as a cursor C chain 
. 6      C = a;                     // C a list of points to the first node 
. 7      the C-> Next = NULL;             / / this can be removed, if necessary because of the following two have an execution 
. 8      Free (B);                 // freed B head node 
. 9      R & lt = C;                     // pointer on the head node C 
10      the while(!! = NULL && P Q = NULL)         // when two lists does not reach the last one 
. 11      {
 12 is          IF (p-> Data <Q- => Data)
 13 is          {
 14              R-> Next = P;
 15              P p-=> Next;
 16              R & lt = R-> Next;
 . 17          }
 18 is          the else {
 . 19              R-> Next = Q;
 20 is              Q = Q-> Next;
 21 is              R & lt = R-> Next;
 22 is          }
 23 is      }
 24      R- > next = NULL;            // Finally, the next pointer points to an empty list C 
25      
26 is      IF (P =! NULL) {
 27          R-> next = P;
 28      }
 29      IF (! Q = NULL)
 30          R-> next = Q;
 31 is }

 

Guess you like

Origin www.cnblogs.com/whtmomo/p/11515106.html