2.1 Table order to achieve a set of "intersection" operation

SeqList.h 

#pragma Once 
#include <the iostream>            // definition "seqList.h" in 
#include <stdlib.h> the using namespace STD;
 const int defaultSize and = 100 ; 
Template < class E>
 class SeqList {
 protected : 
    E * Data;              / / storage array int the maxSize;          // number of entries that can accommodate the maximum entry int last;              // last position currently stored entries
     // void Resize (int newSize);     // change the array space public
  
    
    
 :
    SeqList(int sz = defaultSize) {
        maxSize = sz;
        data = new E[maxSize];
        last = -1;
    }
    //构造函数
    SeqList(SeqList<E>& L) {
        this->maxSize = L.maxSize;
        this->last = L.last;
        this->data = new E[maxSize];
        for (int i = 0; i <= last; i++) {
            this->data[i] = L.data[i];
        }
    } 
    // Copy constructor 

    int Size () const {
         return the maxSize; 
    } 
    // find the maximum capacity table 
    int the Length () const {
         return Last + . 1 ; 
    } 
    // calculation table length 
    int Search (E X) const 
    { 
        E E = X;
         int RET = - . 1 ;
         for ( int I = 0 ; I <= Last; I ++ ) 
        { 
            IF (Data [I] == E) {
                RET = I;
                 BREAK ; 
            } 
        } 
        return RET; 
    } 
    // search position x, the function returns the serial number in the table entry 
    int the Locate ( int I) const {
         IF (I> = . 1 && I <= Last + . 1 ) {
             return - i; 
        } 
        the else {
             return - 2 ; 
        } 
    } 
    // positioning the i-th entry, entry number function returns 
    BOOL the getData ( int i, X & E)const { 
        X = Data [i];
         return  to true ; 
    } 
    // get the i-th entry value 
    BOOL the Insert ( int i, E X) {
         for ( int J = Last; J> = i; J, ) { 
            Data [J + . 1 ] = Data [J]; 
        } 
        Data [I] = X; 
        Last ++ ;
         return  to true ; 
    } 
    // inserted 
    BOOL the Remove ( int I, E &  x) {
        X = data[i];
        for (int j = i; j < last; j++) {
            data[j] = data[j + 1];
        }
        last--;
        return true;
    }
    //删除
    void show() {
        for (int i = 0; i <= last; i++) {
            cout << data[i] << "  ";
        }
        putchar('\n');
    }
};

MyKit.h

#pragma once
#include"SeqList.h"
template<class T>
class MyKit
{
public:
    static void intersect(SeqList<T>& La, SeqList<T>& Lb) {
        int lastA = La.Length() - 1;
        int lastB = Lb.Length() - 1;
        T e;
        for (int i = 0; i <= lastA; i++) {
            La.getData(i, e);
            if (Lb.Search(e) < 0) {
                La.Remove(i, e);
            }
        }
    }
};

main.cpp

#include"MyKit.h"

int main()
{
    SeqList<int> L1, L2;
    L1.Insert(0, 9);
    L1.Insert(0, 6);
    L1.Insert(1, 3);
    cout << "L1:" << endl;
    L1.show();
    for (int i = 0; i <= 9; i++) {
        L2.Insert(0, i);
    }
    cout << "L2:" << endl;
    L2.show();
    MyKit<int>::intersect(L1, L2);
    cout << "Interscet:" << endl;
    L1.show();

}

 

Guess you like

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