/* Questions about the linked list * A and B are two incremental and ordered Single-Chain tables, the number of elements is M and N, find * Set A-B, and save the results in, and keep increasing order. * Converge_ AB */# include <iostream. h> using namespace STD; typedef struct lnode {int data; struct lnode * Next;} lnode; int main () {lnode * create_chain (INT num, int interval, int start ); void converge_ AB (lnode * a, lnode * B); lnode * a, * B, * q, * P; int M = 4, N = 7; A = create_chain (4, 3, 2); // 2 5 8 11 B = create_chain (7,4, 1); // 1 5 9 13 17 21 25 p = A;/* While (p-> next! = NULL) {cout <p-> next-> data <""; P = p-> next;} cout <Endl; q = B; while (Q-> next! = NULL) {cout <q-> next-> data <""; q = Q-> next;} cout <Endl; converge_ AB (a, B, m, n); P = A; */converge_ AB (a, B); P = A; while (p-> next! = NULL) {cout <p-> next-> data <""; P = p-> next ;}} lnode * create_chain (INT num, int interval, int start) {lnode * head = (lnode *) malloc (sizeof (lnode), * P, * q = head; int I; for (I = 0; I <num; I ++) {P = (lnode *) malloc (sizeof (lnode); P-> DATA = start + (I * interval ); p-> next = NULL; q-> next = P; q = Q-> next;} return head;} void converge_ AB (lnode * a, lnode * B) {lnode * q = B-> next, * r = A, * temp; while (R-> next! = NULL & Q! = NULL) {If (R-> next-> data <q-> data) {r = r-> next ;} else if (R-> next-> DATA = Q-> data) {temp = r-> next; r-> next = temp-> next; free (temp) ;}else {q = Q-> next ;}}}
The previous mistake was to write the following three if statements instead of the else if statement. The difference between them is: if there are three if, these three if will be executed when the condition is true. If the first if has been executed, R is changed, in this way, once the changed R meets the subsequent standards, if the change is if else, if... else if .... else... in either of the three cases, the execution will exit after the execution and carry out the next round of loop. (The equivalent of the two is to add continue to each of the three if conditions)