线性表的顺序表示

#include 
#include 
#include 

#define MaxSize 50

using namespace std;

typedef struct{ //静态分配
    int length;
    int data[MaxSize];
}SqList;

typedef struct{ //动态分配
    int length;
    int maxsize;
    int *data;
}SeqList;

bool ListInsert(SqList &L,int i,int val){ //insert
    if(i < 1 || i > L.length+1) return false;
    if(L.length >= MaxSize) return false;
    for(int j=L.length;j>=i;--j){
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = val;
    ++L.length;
    return true;
}

bool ListDelete(SqList &L,int i,int &val){ //delete
    if(i < 1 || i > L.length) return false;
    val = L.data[i-1];
    for(int j=i;j= t) return false;
    int i,j;
    for(i=0;L.data[i]<=s && i= t) return false;
    int k = 0;
    for(int i=0;i s && L.data[i] < t)){
            L.data[k] = L.data[i];
            ++k;
        }
    }
    L.length = k;
    return true;
}

//p18 二.6
bool Del_same(SqList &L){
    if(L.length == 0) return false;
    int i,j;
    for(i=0,j=1;j C.maxsize) return false;
    int i,j,k;
    i = j = k = 0;
    while(i < A.length && j < B.length){
        if(A.data[i] <= B.data[j]){
            C.data[k++] = A.data[i++];
        }
        else {
            C.data[k++] = B.data[j++];
        }
    }
    while(i < A.length){
        C.data[k++] = A.data[i++];
    }
    while(j < B.length){
        C.data[k++] = B.data[j++];
    }
    C.length = k;
    return true;
}

//p18 二.8
void Reverse(SqList &L,int left,int right){
    int mid = (left+right)/2;
    if(left >= right) return;
    for(int i=left;i<=mid;++i){
        int tmp = L.data[i];
        L.data[i] = L.data[right-(i-left)];
        L.data[right-(i-left)] = tmp;
    }
}

void Exchange(SqList &L,int m,int n){
    Reverse(L,0,m+n-1);
    Reverse(L,0,n-1);
    Reverse(L,n,n+m-1);
}

//p18 二.9

void SearchExchangeInsert(SqList &L,int x){
    int left = 0,right = L.length-1;
    int mid;
    while(left <= right){
        mid = left + (right-left)/2;
        if(L.data[mid] == x) break;
        else if(L.data[mid] < x) left = mid + 1;
        else if(L.data[mid] > x) right = mid - 1;
    }
    if(L.data[mid] == x && mid != L.length-1){
        int t = L.data[mid];
        L.data[mid] = L.data[mid+1];
        L.data[mid+1] = t;
    }
    else{
        for(int i=L.length-1;i>right;--i){
            L.data[i+1] = L.data[i];
        }
        L.data[right-1] = x;
    }
}

//p18 二.10

void Reverse1(SqList &L,int left ,int right){
    int mid = (left + right)/2;
    for(int i=left;i<=mid;++i){
        int tmp = L.data[i];
        L.data[i] = L.data[right-(i-left)];
        L.data[right-(i-left)] = tmp;
    }

}

void Converse(SqList &L,int p){
    int n = L.length;
    Reverse1(L,0,n-1);
    Reverse1(L,0,n-p-1);
    Reverse1(L,n-p,n-1);
}

//p19 二.11
int M_Search(SqList &A,SqList &B){
    if(A.length != B.length) return -1;
    int n = A.length;
    int s1 = 0,d1 = n-1;
    int s2 = 0,d2 = n-1;
    int m1,m2;
    while(s1 != d1 || s2 != d2){
        m1 = (s1 + d1)/2;
        m2 = (s2 + d2)/2;
        if(A.data[m1] == B.data[m2]) return A.data[m1]; //中位数相同
        else if(A.data[m1] < B.data[m2]){ //若A的中位数小于B的中位数,则舍弃A的前半段和B的后半段
            if((s1 + d1)%2 == 0){ //奇数个,保留中间数
                s1 = m1;
                d2 = m2;
            }
            else{ //偶数个,A不保留中间数,B保留中间数
                s1 = m1 + 1;
                d2 = m2;
            }
        }
        else if(A.data[m1] > B.data[m2]){ //若A的中位数大于B的中位数,则舍弃A的后半段和B的前半段
            if((s2 + d2)%2 == 0){ 
                d1 = m1;
                s2 = m2;
            }
            else{
                d1 = m1;
                s2 = m2 + 1;
            }
        }
    }
    return A.data[s1] < B.data[s2] ? A.data[s1] : B.data[s2];
}


//p19 二.12
int Majority(SqList &L){
    int cnt = 0; //计数,记录当前候选主元素的生命值
    int m; //候选主元素
    for(int i=0;i L.length/2) return m; 
    return -1;
}

// p19 二.13
int FindMissMin(SqList &L){ //空间换时间
    /*
    L中存储元素的个数为n
    分配一个用于标记的数组B[n]
    当L中出现的元素恰好为1~n时,B[]全部被标记,结果为n+1
    当L中出现了 <0 或 >n 的元素中,B[]中存在没有被标记的元素,这时返回最小的未被标记的元素即可
     */
    int *B = (int *)malloc(sizeof(int)*(L.length+5));
    memset(B,0,sizeof(B));
    int n = L.length;
    for(int i=0;i 0 && L.data[i] <= n){
            B[L.data[i]] = 1;
        }
    }
    int ans;
    for(int i=1;i<=n;++i){
        if(B[i] == 0) return i; 
    }
    return n+1;
}

int main(){
    SqList A,B;
    SqList L;
    A.length = B.length = 0;
    L.length = 0;
    int cnt,val; //元素个数和元素值
    while(cin >> cnt){
        for(int i=1;i<=cnt;++i){
            cin >> val;
            ListInsert(L,i,val);
        }
        cout << FindMissMin(L) << endl;
    }
    return 0;
}

 

你可能感兴趣的:(数据结构)