C + + deep understanding of complex linked list construction replication __c++

Source: Internet
Author: User
#include <iostream> using namespace std;
    #define DEFAULT-1 struct Node {int data;
    Node *next;
    Node *other;//This is an extra pointer to a complex list that points to a node at a specified location.

Node (int d = int ()):d ATA (d), Next (null), other (null) {}}; Class Contexlist {public:contexlist (): A (NULL) {} void Insertnext (int a[], int n) {int i =
        0;
        Node *p = NULL;
            for (; i < n; i++) {Node *s = new Node (A[i]);
                if (A/= NULL) {a = s;
            p = s;
                else {S->next = p->next;
                P->next = s;
            p = s;
        }} p = i;
        Node *q = NULL;
                while (P!= null) {if (P->next!= null) {q = p->next->next;
                P->other = q;
                Connect with the other pointer once every two pointers.
            Constructs a connection to a complex list.
       }     p = p->next;
    } void Printf ()//print linked list.
        {Node *p = i; while (P!= null) {if (P->other!= null) {cout << p->data &L
            t;< "->other:" << p->other->data << Endl; else {cout << p->data << "->other:" << "NULL" <<
                Endl
            Prints the value of other.
        } p = p->next;
        } friend void Copy (contexlist cl1, contexlist &cl2) {Node *oldptr = Cl1.first;
        if (oldptr = NULL) return;
        Node *newptr = Cl2.first;
        Node *p = newptr;


        Node *m = NULL; Well, in order to prevent the subsequent connection duplication and confusion of the problem, I choose//First copy and then assign value, why do this.
        In order to do this well//more clearly, I consider a forward copy from behind, this does not work, because//this is not an array, can not immediately determine the previous position of the value, if from the front//back process and assignment, will result in duplicate copies, copies of multiple
        Situation while (oldptr!= NULL) {m = Oldptr->next;
            Node *S1 = new node ();
            S1->next = m;

            Oldptr->next = S1;
        Oldptr = m;
        //Assigns a value and identifies the other point.
        Oldptr = Cl1.first;
            while (oldptr!= NULL) {m = oldptr->next;
            if (oldptr->other!= NULL) M->other = oldptr->other->next;
            M->data = oldptr->data;
        Oldptr = m->next; //Here is the real split reorganization, the CL1 chain table//Copy of a part of the node extracted, and then connected to the Cl2.first//, the implementation of the split and reorganization, without affecting the previous linked list CL1,//and create
        A new copy of the linked list with the original linked list.
        Oldptr = Cl1.first;
        Node *q = NULL;
            while (oldptr!= NULL) {m = oldptr->next->next;
            Q = m;
                if (p = = NULL) {p = oldptr->next;
                Newptr = p;
            Cl2.first = newptr;
                else {P->next = oldptr->next; p = P-> next;
            } oldptr->next = m;
        Oldptr = m;

}} Private:node *first;

};
    int main () {int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    Contexlist CL1; CL1.

    Insertnext (A, sizeof (a)/sizeof (int));
    Contexlist Cl2;
    Copy (CL1, Cl2);
    cout << "linked list 1->cl1:" << Endl; CL1.
    Printf ();
    cout << "After the copy of the list 2->cl2:" << Endl; Cl2.

    Printf ();
return 0; }

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.