Table 2-15 orderly after-school exercise in reverse merger (single-chain focus algorithm)

topic:

Ha and hb are provided with two single-head pointer list ordered by non-decreasing additional head node. The two tables are combined into a single list of non-incremental and orderly. The results can not take up extra storage space in addition to two lists.

Linklist.h

#pragma once
#include<iostream>
using namespace std;

class LNode {
public:
    int data;
    LNode* next;
};

class LinkList {
public:
    LNode* first;

    LinkList() {
        first = new LNode();
        first->data = 666;
        first->next = nullptr;
    }
    void creatE(int arr[], int n) {
        LNode* p;
        LNode* s;
        p = first;
        for (int i = 0; i < n; i++) {
            s = new LNode();
            s->data = arr[i];
            p->next = s;
            p = s;
        }
    }
    void show() {
        LNode* p;
        p = first->next;
        while (p != nullptr) {
            cout << p->data << " ";
            p = p->next;
        }
        cout << endl;
    }

    void unite(LinkList B) {
        //先合并
        LNode* ha = first;
        LNode* hb = B.first;  //题目要求头指针为ha、hb
        LNode* p = ha->next;
        LNode* q = hb->next;
        LNode* s;
        while (q->next != nullptr && p->next != nullptr) {
            if (q->data >= p->data && q->data <= p->next->Data) { 
                S = Q; 
                Q = Q-> Next; 
                S -> p-Next => Next; 
                P -> Next = S; 
            } 
            the else { 
                P = p-> Next; 
            } 
        } 
        IF (p-> Next = ! && nullptr a = Q = nullptr a) { 
            P -> Next = Q; 
        } 

        // start reverse 
        / * * 
        algorithm idea of a single list in reverse order: 
        (head) -> (. 1) -> (2) -> (. 3) -> (. 4) -> NULL 
        STEP. 1: 
        Create the node 3 pointers. Since uni-linked list, a pointer Less 3
        * = P1 head- LNode> Next, * P2 = P1-> Next, P3 * = P2-> Next; 
        (head) -> (. 1 / P1) -> (2 / P2) -> (. 3 / P3) - > (. 4) -> nULL 

        STEP 2: 
        P1 to point to null 
        P1-> Next = nullptr a; 
        nULL <- (. 1 / P1) (2 / P2) -> (. 3 / P3) -> (. 4) -> nULL ( head) -> (. 1) 

        STEP. 3: 
        circulation, changing the pointer to 
        the while (P3 = nullptr a) {! 
            P2-> Next = P1; 
            P1 = P2; 
            P2 = P3; 
            P3 = P3-> Next; 
        } 
            of the first cycle after: 
            NULL <- (. 1) <- (2 / P1) (. 3 / P2) -> (. 4 / P3) -> NULL (head) -> (. 1) 
            after the second cycle: 
            NULL <- (. 1) <- (2) <- ( 3 / p1) (4 / p2) -> NULL / p3 (head) ->(. 1) 

        STEP. 4: 
        process the last group node pointer and address header
        p2->next=p1;
        head=p2;
        NULL<-(1)<-(2)<-(3/p1)<-(4/p2)<-(head)  NULL/p3
        */

        LNode* p1;
        LNode* p2;
        LNode* p3;
        p1 = ha->next;
        p2 = p1->next;
        p3 = p2->next;
        p1->next = nullptr;
        while (p3 != nullptr) {
            p2->next = p1;
            p1 = p2;
            p2 = p3;
            p3 = p3->next;
        }
        p2->next = p1;
        ha->next = p2;
    }

};

main.cpp

#include"LinkList.h"
int main() {
    int a[] = { 1,3,5,7,9 };
    int b[] = { 2,2,4,4,6,6,8,8,10,10 };
    LinkList A, B;
    A.creatE(a, 5);
    B.creatE(b, 10);
    A.unite(B);
    A.show();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/SlowIsFast/p/12530174.html