1. Assume that there are two linear tables listed in ascending order of element values, all of which are stored as single-chain tables. Compile an algorithm to merge the two single-chain tables into a single-chain table in descending order of element values, and use the nodes of the original two single-chain tables to store the merged single-chain table.
Analysis: The difference between adding a node to the head of the linked list and adding a node to the end of the linked list
List Union (list la, list lb) // la and lB are headers of two single-chain tables at the leading node, the element values in the linked list are arranged in ascending order // This algorithm combines La and lb into a single-chain table in descending order of elements {lnode Pa, Pb, pTMP; pa = La-> next; Pb = LB-> next; La-> next = NULL; // la is the head pointer of the result linked list, the initialization result linked list is empty while (PA & Pb) {If (Pa-> data <= Pb-> data) {pTMP = pa-> next; pa-> next = La-> next; La-> next = PA; Pa = pTMP;} else {pTMP = Pb-> next; pb-> next = La-> next; La-> next = Pb; Pb = pTMP; }}if (PA) {Pb = PA; // no matter whether PA or PB is not empty, only Pb is processed.} while (PB) {pTMP = Pb-> next; Pb-> next = La-> next; la-> next = Pb; Pb = pTMP;} return la ;}
// Algorithm time complexity: 6-10l is initialization, and the time spent is constant C; 12-25l is the main part of the algorithm, if the value of the node referred to by PB is smaller, it is added to the result linked list. The running time is LA, and the length of LB is smaller. 29-34l adds the remaining unoperated nodes in LA and lb to the result linked list. The total running time is O (max (N1, N2 ))
2. A single-chain table with two headless nodes. the header pointers are ha and HB respectively. Data in the chain includes the data field, the Chain Domain next, and the data in the two linked lists are stored incrementally, now we need to classify the Hb table into the HA table, and the HA continues to increase and order after the merger. If the existing data in the HA table in the merger also exists in the Hb table, the data in the Hb is not merged into the HA table, the Hb linked list cannot be destroyed in the algorithm.
Analysis: similar to 1. For ease of processing, add the header node and delete it. The hidden ha is the result linked list.
List Union (list ha, list Hb) // ha, hb is a single-chain table with incremental and ordered data domain values of two headless nodes. // This algorithm combines data that are not included in the HA into the HA, at the same time, the Hb linked list {list head = (list) malloc (sizeof (lnode); head-> next = ha; lnode Pa, Pb, pTMP; Pa = ha; PB = Hb; pTMP = head; // pTMP points to the front of the current node to be merged while (PA & Pb) {If (Pa-> data <Pb-> data) {pTMP-> next = PA; pTMP = PA; Pa = pa-> next;} else if (Pa-> DATA> Pb-> data) {lnode LR = (lnode) malloc (sizeof (lnode); LR-> DATA = Pb-> data; pTMP-> next = LR; Pb = Pb-> next ;} else {// pa-> DATA = Pb-> dataptmp-> next = PA; pTMP = PA; Pa = pa-> next; Pb = Pb-> next ;}} if (PA) {pTMP-> next = PA;} else {While (PB) {lnode LR = (lnode) malloc (sizeof (lnode )); LR-> DATA = Pb-> data; pTMP-> next = LR; pTMP = LR; Pb = Pb-> next;} pTMP-> next = NULL ;} free (head); Return ha ;}
This solution combines ha and HB into the head linked list instead of determining the relationship between the elements indicated by the current pointer in the HA linked list and the elements indicated by the current pointer in the Hb.
2nd Question solution 2: equivalent to inserting elements in Hb into ha
List Union( List ha, List hb ){List head = (Lnode)malloc(sizeof(Lnode));head->next = ha;Lnode pa,pa,pTmp;pa = ha;pb = hb;pTmp = ha;while ( pa&&pb ) {if ( pa->data < pb->data ) {pTmp = pa;pa = pa->next;}else if ( pa->data > pb->data ) {Lnode Lr = (Lnode)malloc(sizeof(Lnode));Lr->data = pb->data;Lr->next = ha;pTmp->next = Lr;pTmp = Lr;pb = pb->next;}else {pTmp = pa;pa = pa->next;pb = pb->next;}}if ( pa ) {pTmp->next = pa;}else {while ( pb ) {Lnode Lr = (Lnode)malloc(sizeof(Lnode));Lr->data = pb->data;pTmp->next = Lr;pTmp = Lr;pb = pb->next;}pTmp->next = NULL;}free(head);return ha;}
3. The two linear tables A and B with the pointers of HA and HB represent two sets respectively. The elements in both tables are incremental and ordered. Write an algorithm to calculate the Union of A and B. It is required that the elements in the Union remain in ascending order. The original Node space of A and B should be used.
List Union (list ha, list Hb) {lnode Pa = ha-> next; lnode Pb = HB-> next; lnode pre = ha; // pre indicates the front pointer of the current node of the result linked list while (PA & Pb) {If (Pa-> data <Pb-> data) {pre-> next = PA; pre = PA; Pa = pa-> next;} else if (Pa-> DATA> Pb-> data) {pre-> next = Pb; Pre = Pb; PB = Pb-> next;} else {// pa-> DATA = Pb-> datalnode pTMP = Pb; pre-> next = PA; Pre = PA; pa = pa-> next; Pb = Pb-> next; free (pTMP) ;}}if (PA) {pre-> next = PA ;} else {pre-> next = Pb;} return ha ;}
It should be the simplest of the three questions.