Data Structure> linear table [note] --> linked list for A-B (original A and B are increasing, the A-B does not change the original order of)

Source: Internet
Author: User
/* 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.