A data structure (c ++) wording

The establishment of a single list

 

 


 

 

 This problem required to achieve a function, the incremented sequence of integers represented by two lists combined into a non-decreasing sequence of integers.

To -1 as an input end

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef struct LNode *List;
 5 struct LNode
 6 {
 7   int data;
 8   LNode *next;    
 9  };
10  
11 void InitList(List &L)
12 {
13     L = new LNode;
14     L->next = NULL;
15 }
16 
17 void CreatList(List &L)
18 is  {
 . 19      List R & lt, S;
 20 is      S = L;
 21 is      int A;
 22 is      the while ( . 1 )
 23 is      {
 24          CIN >> A;            // output ends -1 as 
25          IF (A == - . 1 )          // End interpolation 
26 is          BREAK ;
 27          R & lt = new new LNode;
 28          R-> Data = A;
 29          R-> Next = NULL;
 30          S-> Next = R & lt;
 31 is          S = r;
32     }
33     s->next = NULL;
34 }
35 
36 void PrintList(List &L)
37 {
38   List p;
39   p = L->next;
40   while(p)
41   {
42       cout << p->data << " ";
43       p = p->next;
44   }
45 }
46 
47 void mergeList(List &L1,List &L2)
48 {
49     List pa,pb,pc,lc;
50     pa = L1->next;
51     pb = L2->next;
52     lc = new LNode; //新建头结点 
53     pc = lc;        // pc指向 lc 
54     while(pa&&pb)
55     {
56         if(pa->data <= pb->data) 
57         {
58             pc->next = pa;
59             pc = pa;
60             pa = pa->next;
61         }
62         else
63         {
64-              pc-> the Next = pb;
 65              PC = pb;
 66              pb = pb-> the Next;    
 67          }
 68      }
 69      pc-> = pa pa the Next:? Pb; // will pa or pb no strings into a complete traversal PC 
70      Free (Ll);   // freed Ll, L2 of 
71 is      Free (L2 of);   // if you want to output the NULL Ll-> NULL 
72      printlist (LC); 
 73 is  }
 74  
75  int main ()
 76  {
 77      List A , B;
 78      InitList (A);
 79     CreatList(a);
80     InitList(b);
81     CreatList(b);
82     mergeList(a,b);
83     return 0; 
84     
85 }
Merge single list

 

operation result

 

 to sum up

1 . Each list has a header node, the head node meaningless data, only the valid pointer pointing to the next node.
2 .List L = new new LNode represents a list, L is a pointer pointing to the head node
3 . Typically the operation of the list to find the list through the operation deletions by creating a new pointer p = L (representing a list head pointer, its value can not be changed).

 


 

Guess you like

Origin www.cnblogs.com/invokerrr/p/11503098.html