"Programming topic" Please modify the Append function, use this function to implement two non-descending list of the set

Source: Internet
Author: User

42. Modify the Append function to implement (linked list) using this function:
Two sets of non-descending lists, 1->2->3 and 2->3->5, and 1->2->3->5
You can only output results, and you cannot modify the data of two linked lists.

Feel the online understanding test instructions slightly different, I understand the test instructions:

Just output The final result, do not create a new linked list, nor modify the original linked list.

The data given is not in descending order 1 1 2 2 3 This can have a repeating number increment

When output, no duplicate numbers are displayed.

Based on these understandings, the problem is not difficult, the code is as follows:

/*42. Please modify the Append function, using this function (linked list): Two non-descending list of the set, 1->2->3 and 2->3->5 and for 1->2->3->5 can only output the results, Data from two linked lists cannot be modified. Start time = 19:29end time = 19:57*/#include<iostream>using namespaceStd;typedefstructlist{intm_value; List*P_next;} List;voidAppend (list * L1, list *L2) {List* P1 =L1; List* P2 =L2; //Two of the lists are not empty.     while(P1! = NULL && P2! =NULL) {        intv; if(P1->m_value > p2->m_value) {v= p2->m_value; cout<< p2->m_value;  while(P2! = NULL && P2->m_value = =v) P2= p2->P_next; }        Else if(P1->m_value < p2->m_value) {v= p1->m_value; cout<< p1->m_value;  while(P1! = NULL && P1->m_value = =v) P1= p1->P_next; }        Else{v= p1->m_value; cout<< p1->m_value<<" ";  while(P1! = NULL && P1->m_value = =v) P1= p1->P_next;  while(P2! = NULL && P2->m_value = =v) P2= p2->P_next; }        if(P1 = = NULL && P2 = =NULL) cout<<Endl; Elsecout<<" -"; }    //if P1 is not left empty     while(P1! =NULL) {        intv = p1->m_value; cout<< p1->m_value;  while(P1! = NULL && P1->m_value = =v) P1= p1->P_next; if(P1 = =NULL) cout<<Endl; Elsecout<<" -"; }    //if P2 is not left empty     while(P2! =NULL) {        intv = p2->m_value; cout<< p2->m_value;  while(P2! = NULL && P2->m_value = =v) P2= p2->P_next; if(P2 = =NULL) cout<<Endl; Elsecout<<" -"; }}voidCreateList (List * &Head) {    intdata; CIN>>data; if(Data! =0) {Head=NewList; Head->m_value =data; Head->p_next =NULL; CreateList (Head-p_next); }}intMain () {List* L1 =NULL; List* L2 =NULL;    CreateList (L1);    CreateList (L2);    Append (L1, L2); return 0;}

"Programming topic" Please modify the Append function, use this function to implement two non-descending list of the set

Related Article

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.